Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/ExcelCommentCollection.cs @ 14905

Last change on this file since 14905 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 9.0 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 *******************************************************************************
29 * Jan Källman    License changed GPL-->LGPL 2011-12-27
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Text;
34using System.Xml;
35using System.Collections;
36using OfficeOpenXml.Utils;
37namespace OfficeOpenXml
38{
39    /// <summary>
40    /// Collection of Excelcomment objects
41    /// </summary> 
42    public class ExcelCommentCollection : IEnumerable, IDisposable
43    {
44        internal RangeCollection _comments;
45        internal ExcelCommentCollection(ExcelPackage pck, ExcelWorksheet ws, XmlNamespaceManager ns)
46        {
47            CommentXml = new XmlDocument();
48            CommentXml.PreserveWhitespace = false;
49            NameSpaceManager=ns;
50            Worksheet=ws;
51            CreateXml(pck);
52            AddCommentsFromXml();
53        }
54        private void CreateXml(ExcelPackage pck)
55        {
56            var commentParts = Worksheet.Part.GetRelationshipsByType(ExcelPackage.schemaComment);
57            bool isLoaded=false;
58            CommentXml=new XmlDocument();
59            foreach(var commentPart in commentParts)
60            {
61                Uri = UriHelper.ResolvePartUri(commentPart.SourceUri, commentPart.TargetUri);
62                Part = pck.Package.GetPart(Uri);
63                XmlHelper.LoadXmlSafe(CommentXml, Part.GetStream());
64                RelId = commentPart.Id;
65                isLoaded=true;
66            }
67            //Create a new document
68            if(!isLoaded)
69            {
70                CommentXml.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><comments xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><authors /><commentList /></comments>");
71                Uri = null;
72            }
73        }
74        private void AddCommentsFromXml()
75        {
76            var lst = new List<IRangeID>();
77            foreach (XmlElement node in CommentXml.SelectNodes("//d:commentList/d:comment", NameSpaceManager))
78            {
79                var comment = new ExcelComment(NameSpaceManager, node, new ExcelRangeBase(Worksheet, node.GetAttribute("ref")));
80                lst.Add(comment);
81            }
82            _comments = new RangeCollection(lst);
83        }
84        /// <summary>
85        /// Access to the comment xml document
86        /// </summary>
87        public XmlDocument CommentXml { get; set; }
88        internal Uri Uri { get; set; }
89        internal string RelId { get; set; }
90        internal XmlNamespaceManager NameSpaceManager { get; set; }
91        internal Packaging.ZipPackagePart Part
92        {
93            get;
94            set;
95        }
96        /// <summary>
97        /// A reference to the worksheet object
98        /// </summary>
99        public ExcelWorksheet Worksheet
100        {
101            get;
102            set;
103        }
104        /// <summary>
105        /// Number of comments in the collection
106        /// </summary>
107        public int Count
108        {
109            get
110            {
111                return _comments.Count;
112            }
113        }
114        /// <summary>
115        /// Indexer for the comments collection
116        /// </summary>
117        /// <param name="Index">The index</param>
118        /// <returns>The comment</returns>
119        public ExcelComment this[int Index]
120        {
121            get
122            {
123                if (Index < 0 || Index >= _comments.Count)
124                {
125                    throw(new ArgumentOutOfRangeException("Comment index out of range"));
126                }
127                return _comments[Index] as ExcelComment;
128            }
129        }
130        /// <summary>
131        /// Indexer for the comments collection
132        /// </summary>
133        /// <param name="cell">The cell</param>
134        /// <returns>The comment</returns>
135        public ExcelComment this[ExcelCellAddress cell]
136        {
137            get
138            {
139                ulong cellID=ExcelCellBase.GetCellID(Worksheet.SheetID, cell.Row, cell.Column);
140                if (_comments.IndexOf(cellID) >= 0)
141                {
142                    return _comments[cellID] as ExcelComment;
143                }
144                else
145                {
146                    return null;
147                }
148            }
149        }
150        /// <summary>
151        /// Adds a comment to the top left cell of the range
152        /// </summary>
153        /// <param name="cell">The cell</param>
154        /// <param name="Text">The comment text</param>
155        /// <param name="author">Author</param>
156        /// <returns>The comment</returns>
157        public ExcelComment Add(ExcelRangeBase cell, string Text, string author)
158        {           
159            var elem = CommentXml.CreateElement("comment", ExcelPackage.schemaMain);
160            int ix=_comments.IndexOf(ExcelAddress.GetCellID(Worksheet.SheetID, cell._fromRow, cell._fromCol));
161            //Make sure the nodes come on order.
162            if (ix < 0 && (~ix < _comments.Count))
163            {
164                ix = ~ix;
165                var preComment = _comments[ix] as ExcelComment;
166                preComment._commentHelper.TopNode.ParentNode.InsertBefore(elem, preComment._commentHelper.TopNode);
167            }
168            else
169            {
170                CommentXml.SelectSingleNode("d:comments/d:commentList", NameSpaceManager).AppendChild(elem);
171            }
172            elem.SetAttribute("ref", cell.Start.Address);
173            ExcelComment comment = new ExcelComment(NameSpaceManager, elem , cell);
174            comment.RichText.Add(Text);
175            if(author!="")
176            {
177                comment.Author=author;
178            }
179            _comments.Add(comment);
180            //Check if a value exists otherwise add one so it is saved when the cells collection is iterated
181            if (!Worksheet._values.Exists(cell._fromRow, cell._fromCol))
182            {
183                Worksheet._values.SetValue(cell._fromRow, cell._fromCol, null);
184            }
185            return comment;
186        }
187        /// <summary>
188        /// Removes the comment
189        /// </summary>
190        /// <param name="comment">The comment to remove</param>
191        public void Remove(ExcelComment comment)
192        {
193            ulong id = ExcelAddress.GetCellID(Worksheet.SheetID, comment.Range._fromRow, comment.Range._fromCol);
194            int ix=_comments.IndexOf(id);
195            if (ix >= 0 && comment == _comments[ix])
196            {
197                comment.TopNode.ParentNode.RemoveChild(comment.TopNode); //Remove VML
198                comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment
199
200                Worksheet.VmlDrawingsComments._drawings.Delete(id);
201                _comments.Delete(id);
202            }
203            else
204            {
205                throw (new ArgumentException("Comment does not exist in the worksheet"));
206            }
207        }
208
209        void IDisposable.Dispose()
210        {
211            if (_comments != null)
212                ((IDisposable)_comments).Dispose();
213        }
214        /// <summary>
215        /// Removes the comment at the specified position
216        /// </summary>
217        /// <param name="Index">The index</param>
218        public void RemoveAt(int Index)
219        {
220            Remove(this[Index]);
221        }
222        #region IEnumerable Members
223
224        IEnumerator IEnumerable.GetEnumerator()
225        {
226            return _comments;
227        }
228        #endregion
229
230        internal void Clear()
231        {
232            while(Count>0)
233            {
234                RemoveAt(0);
235            }
236        }
237    }
238}
Note: See TracBrowser for help on using the repository browser.