Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/3.1.3/EPPlus-3.1.3/ExcelCommentCollection.cs @ 11733

Last change on this file since 11733 was 9580, checked in by sforsten, 12 years ago

#1730:

  • added SymbolicDataAnalysisExpressionExcelFormatter
  • changed modifiers in SymbolicExpressionTreeChart of methods SaveImageAsBitmap and SaveImageAsEmf to public
  • added menu item ExportSymbolicSolutionToExcelMenuItem to export a symbolic solution to an excel file
  • added EPPlus-3.1.3 to ExtLibs
File size: 8.4 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.IO.Packaging;
36using System.Collections;
37
38namespace OfficeOpenXml
39{
40    /// <summary>
41    /// Collection of Excelcomment objects
42    /// </summary> 
43    public class ExcelCommentCollection : IEnumerable
44    {
45        internal RangeCollection _comments;
46        internal ExcelCommentCollection(ExcelPackage pck, ExcelWorksheet ws, XmlNamespaceManager ns)
47        {
48            CommentXml = new XmlDocument();
49            CommentXml.PreserveWhitespace = false;
50            NameSpaceManager=ns;
51            Worksheet=ws;
52            CreateXml(pck);
53            AddCommentsFromXml();
54        }
55        private void CreateXml(ExcelPackage pck)
56        {
57            var commentParts = Worksheet.Part.GetRelationshipsByType(ExcelPackage.schemaComment);
58            bool isLoaded=false;
59            CommentXml=new XmlDocument();
60            foreach(var commentPart in commentParts)
61            {
62                Uri = PackUriHelper.ResolvePartUri(commentPart.SourceUri, commentPart.TargetUri);
63                Part = pck.Package.GetPart(Uri);
64                XmlHelper.LoadXmlSafe(CommentXml, Part.GetStream());
65                RelId = commentPart.Id;
66                isLoaded=true;
67            }
68            //Create a new document
69            if(!isLoaded)
70            {
71                CommentXml.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><comments xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><authors /><commentList /></comments>");
72                Uri = null;
73            }
74        }
75        private void AddCommentsFromXml()
76        {
77            var lst = new List<IRangeID>();
78            foreach (XmlElement node in CommentXml.SelectNodes("//d:commentList/d:comment", NameSpaceManager))
79            {
80                var comment = new ExcelComment(NameSpaceManager, node, new ExcelRangeBase(Worksheet, node.GetAttribute("ref")));
81                lst.Add(comment);
82            }
83            _comments = new RangeCollection(lst);
84        }
85        /// <summary>
86        /// Access to the comment xml document
87        /// </summary>
88        public XmlDocument CommentXml { get; set; }
89        internal Uri Uri { get; set; }
90        internal string RelId { get; set; }
91        internal XmlNamespaceManager NameSpaceManager { get; set; }
92        internal PackagePart Part
93        {
94            get;
95            set;
96        }
97        /// <summary>
98        /// A reference to the worksheet object
99        /// </summary>
100        public ExcelWorksheet Worksheet
101        {
102            get;
103            set;
104        }
105        /// <summary>
106        /// Number of comments in the collection
107        /// </summary>
108        public int Count
109        {
110            get
111            {
112                return _comments.Count;
113            }
114        }
115        /// <summary>
116        /// Indexer for the comments collection
117        /// </summary>
118        /// <param name="Index">The index</param>
119        /// <returns>The comment</returns>
120        public ExcelComment this[int Index]
121        {
122            get
123            {
124                if (Index < 0 || Index >= _comments.Count)
125                {
126                    throw(new ArgumentOutOfRangeException("Comment index out of range"));
127                }
128                return _comments[Index] as ExcelComment;
129            }
130        }
131        /// <summary>
132        /// Indexer for the comments collection
133        /// </summary>
134        /// <param name="cell">The cell</param>
135        /// <returns>The comment</returns>
136        public ExcelComment this[ExcelCellAddress cell]
137        {
138            get
139            {
140                ulong cellID=ExcelCellBase.GetCellID(Worksheet.SheetID, cell.Row, cell.Column);
141                if (_comments.IndexOf(cellID) >= 0)
142                {
143                    return _comments[cellID] as ExcelComment;
144                }
145                else
146                {
147                    return null;
148                }
149            }
150        }
151        /// <summary>
152        /// Adds a comment to the top left cell of the range
153        /// </summary>
154        /// <param name="cell">The cell</param>
155        /// <param name="Text">The comment text</param>
156        /// <param name="author">Author</param>
157        /// <returns>The comment</returns>
158        public ExcelComment Add(ExcelRangeBase cell, string Text, string author)
159        {           
160            var elem = CommentXml.CreateElement("comment", ExcelPackage.schemaMain);
161            int ix=_comments.IndexOf(ExcelAddress.GetCellID(Worksheet.SheetID, cell._fromRow, cell._fromCol));
162            //Make sure the nodes come on order.
163            if (ix < 0 && (~ix < _comments.Count))
164            {
165                ix = ~ix;
166                var preComment = _comments[ix] as ExcelComment;
167                preComment._commentHelper.TopNode.ParentNode.InsertBefore(elem, preComment._commentHelper.TopNode);
168            }
169            else
170            {
171                CommentXml.SelectSingleNode("d:comments/d:commentList", NameSpaceManager).AppendChild(elem);
172            }
173            elem.SetAttribute("ref", cell.Start.Address);
174            ExcelComment comment = new ExcelComment(NameSpaceManager, elem , cell);
175            comment.RichText.Add(Text);
176            if(author!="")
177            {
178                comment.Author=author;
179            }
180            _comments.Add(comment);
181            return comment;
182        }
183        /// <summary>
184        /// Removes the comment
185        /// </summary>
186        /// <param name="comment">The comment to remove</param>
187        public void Remove(ExcelComment comment)
188        {
189            ulong id = ExcelAddress.GetCellID(Worksheet.SheetID, comment.Range._fromRow, comment.Range._fromCol);
190            int ix=_comments.IndexOf(id);
191            if (ix >= 0 && comment == _comments[ix])
192            {
193                comment.TopNode.ParentNode.RemoveChild(comment.TopNode); //Remove VML
194                comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment
195
196                Worksheet.VmlDrawingsComments._drawings.Delete(id);
197                _comments.Delete(id);
198            }
199            else
200            {
201                throw (new ArgumentException("Comment does not exist in the worksheet"));
202            }
203        }
204        /// <summary>
205        /// Removes the comment at the specified position
206        /// </summary>
207        /// <param name="Index">The index</param>
208        public void RemoveAt(int Index)
209        {
210            Remove(this[Index]);
211        }
212        #region IEnumerable Members
213
214        IEnumerator IEnumerable.GetEnumerator()
215        {
216            return _comments;
217        }
218        #endregion
219
220
221    }
222}
Note: See TracBrowser for help on using the repository browser.