Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/ExcelComment.cs @ 13329

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.7 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    Initial Release       
30 * Jan Källman    License changed GPL-->LGPL 2011-12-27
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Linq;
35using System.Text;
36using OfficeOpenXml.Style;
37using System.Xml;
38using OfficeOpenXml.Drawing;
39using OfficeOpenXml.Drawing.Vml;
40
41namespace OfficeOpenXml
42{
43    /// <summary>
44    /// An Excel Cell Comment
45    /// </summary>
46    public class ExcelComment : ExcelVmlDrawingComment
47    {
48        internal XmlHelper _commentHelper;
49        private string _text;
50        internal ExcelComment(XmlNamespaceManager ns, XmlNode commentTopNode, ExcelRangeBase cell)
51            : base(null, cell, cell.Worksheet.VmlDrawingsComments.NameSpaceManager)
52        {
53            //_commentHelper = new XmlHelper(ns, commentTopNode);
54            _commentHelper = XmlHelperFactory.Create(ns, commentTopNode);
55            var textElem=commentTopNode.SelectSingleNode("d:text", ns);
56            if (textElem == null)
57            {
58                textElem = commentTopNode.OwnerDocument.CreateElement("text", ExcelPackage.schemaMain);
59                commentTopNode.AppendChild(textElem);
60            }
61            if (!cell.Worksheet._vmlDrawings.ContainsKey(ExcelAddress.GetCellID(cell.Worksheet.SheetID, cell.Start.Row, cell.Start.Column)))
62            {
63                cell.Worksheet._vmlDrawings.Add(cell);
64            }
65
66            TopNode = cell.Worksheet.VmlDrawingsComments[ExcelCellBase.GetCellID(cell.Worksheet.SheetID, cell.Start.Row, cell.Start.Column)].TopNode;
67            RichText = new ExcelRichTextCollection(ns,textElem);
68            var tNode = textElem.SelectSingleNode("d:t", ns);
69            if (tNode != null)
70            {
71                _text = tNode.InnerText;
72            }
73        }
74        const string AUTHORS_PATH = "d:comments/d:authors";
75        const string AUTHOR_PATH = "d:comments/d:authors/d:author";
76        /// <summary>
77        /// Author
78        /// </summary>
79        public string Author
80        {
81            get
82            {
83                int authorRef = _commentHelper.GetXmlNodeInt("@authorId");
84                return _commentHelper.TopNode.OwnerDocument.SelectSingleNode(string.Format("{0}[{1}]", AUTHOR_PATH, authorRef+1), _commentHelper.NameSpaceManager).InnerText;
85            }
86            set
87            {
88                int authorRef = GetAuthor(value);
89                _commentHelper.SetXmlNodeString("@authorId", authorRef.ToString());
90            }
91        }
92        private int GetAuthor(string value)
93        {
94            int authorRef = 0;
95            bool found = false;
96            foreach (XmlElement node in _commentHelper.TopNode.OwnerDocument.SelectNodes(AUTHOR_PATH, _commentHelper.NameSpaceManager))
97            {
98                if (node.InnerText == value)
99                {
100                    found = true;
101                    break;
102                }
103                authorRef++;
104            }
105            if (!found)
106            {
107                var elem = _commentHelper.TopNode.OwnerDocument.CreateElement("d", "author", ExcelPackage.schemaMain);
108                _commentHelper.TopNode.OwnerDocument.SelectSingleNode(AUTHORS_PATH, _commentHelper.NameSpaceManager).AppendChild(elem);
109                elem.InnerText = value;
110            }
111            return authorRef;
112        }
113        /// <summary>
114        /// The comment text
115        /// </summary>
116        public string Text
117        {
118            get
119            {
120                if(!string.IsNullOrEmpty(RichText.Text)) return RichText.Text;
121                return _text;
122            }
123            set
124            {
125                RichText.Text = value;
126            }
127        }
128        /// <summary>
129        /// Sets the font of the first richtext item.
130        /// </summary>
131        public ExcelRichText Font
132        {
133            get
134            {
135                if (RichText.Count > 0)
136                {
137                    return RichText[0];
138                }
139                return null;
140            }
141        }
142        /// <summary>
143        /// Richtext collection
144        /// </summary>
145        public ExcelRichTextCollection RichText
146        {
147           get;
148           set;
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.