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