| /******************************************************************************* | |
| * You may amend and distribute as you like, but don't remove this header! | |
| * | |
| * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets. | |
| * See http://www.codeplex.com/EPPlus for details. | |
| * | |
| * Copyright (C) 2011 Jan Källman | |
| * | |
| * This library is free software; you can redistribute it and/or | |
| * modify it under the terms of the GNU Lesser General Public | |
| * License as published by the Free Software Foundation; either | |
| * version 2.1 of the License, or (at your option) any later version. | |
| * This library is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| * See the GNU Lesser General Public License for more details. | |
| * | |
| * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php | |
| * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html | |
| * | |
| * All code and executables are provided "as is" with no warranty either express or implied. | |
| * The author accepts no liability for any damage or loss of business that this product may cause. | |
| * | |
| * Code change notes: | |
| * | |
| * Author Change Date | |
| ******************************************************************************* | |
| * Jan Källman License changed GPL-->LGPL 2011-12-27 | |
| *******************************************************************************/ | |
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Xml; | |
| namespace AppsheetEpplus; | |
| /// <summary> | |
| /// Collection of Excelcomment objects | |
| /// </summary> | |
| public class ExcelCommentCollection : IEnumerable, IDisposable { | |
| internal RangeCollection _comments; | |
| internal ExcelCommentCollection(ExcelPackage pck, ExcelWorksheet ws, XmlNamespaceManager ns) { | |
| CommentXml.PreserveWhitespace = false; | |
| NameSpaceManager = ns; | |
| Worksheet = ws; | |
| CreateXml(pck); | |
| AddCommentsFromXml(); | |
| } | |
| private void CreateXml(ExcelPackage pck) { | |
| var commentParts = Worksheet.Part.GetRelationshipsByType(ExcelPackage._schemaComment); | |
| bool isLoaded = false; | |
| CommentXml = new(); | |
| foreach (var commentPart in commentParts) { | |
| Uri = UriHelper.ResolvePartUri(commentPart.SourceUri, commentPart.TargetUri); | |
| Part = pck.Package.GetPart(Uri); | |
| XmlHelper.LoadXmlSafe(CommentXml, Part.GetStream()); | |
| RelId = commentPart.Id; | |
| isLoaded = true; | |
| } | |
| //Create a new document | |
| if (!isLoaded) { | |
| CommentXml.LoadXml( | |
| "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><comments xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><authors /><commentList /></comments>"); | |
| Uri = null; | |
| } | |
| } | |
| private void AddCommentsFromXml() { | |
| var lst = new List<IRangeId>(); | |
| foreach (XmlElement node in CommentXml.SelectNodes( | |
| "//d:commentList/d:comment", | |
| NameSpaceManager)) { | |
| var comment = new ExcelComment( | |
| NameSpaceManager, | |
| node, | |
| new(Worksheet, node.GetAttribute("ref"))); | |
| lst.Add(comment); | |
| } | |
| _comments = new(lst); | |
| } | |
| /// <summary> | |
| /// Access to the comment xml document | |
| /// </summary> | |
| public XmlDocument CommentXml { get; set; } = new(); | |
| internal Uri Uri { get; set; } | |
| internal string RelId { get; set; } | |
| internal XmlNamespaceManager NameSpaceManager { get; set; } | |
| internal ZipPackagePart Part { get; set; } | |
| /// <summary> | |
| /// A reference to the worksheet object | |
| /// </summary> | |
| public ExcelWorksheet Worksheet { get; set; } | |
| /// <summary> | |
| /// Number of comments in the collection | |
| /// </summary> | |
| public int Count => _comments.Count; | |
| /// <summary> | |
| /// Indexer for the comments collection | |
| /// </summary> | |
| /// <param name="index">The index</param> | |
| /// <returns>The comment</returns> | |
| public ExcelComment this[int index] { | |
| get { | |
| if (index < 0 || index >= _comments.Count) { | |
| throw (new ArgumentOutOfRangeException("Comment index out of range")); | |
| } | |
| return _comments[index] as ExcelComment; | |
| } | |
| } | |
| /// <summary> | |
| /// Indexer for the comments collection | |
| /// </summary> | |
| /// <param name="cell">The cell</param> | |
| /// <returns>The comment</returns> | |
| public ExcelComment this[ExcelCellAddress cell] { | |
| get { | |
| ulong cellId = ExcelCellBase.GetCellId(Worksheet.SheetID, cell.Row, cell.Column); | |
| if (_comments.IndexOf(cellId) >= 0) { | |
| return _comments[cellId] as ExcelComment; | |
| } | |
| return null; | |
| } | |
| } | |
| void IDisposable.Dispose() {} | |
| IEnumerator IEnumerable.GetEnumerator() { | |
| return _comments; | |
| } | |
| } |