Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathExcelCommentCollection.cs
More file actions
353 lines (340 loc) · 13.9 KB
/
Copy pathExcelCommentCollection.cs
File metadata and controls
353 lines (340 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections;
using OfficeOpenXml.Core.CellStore;
using System.Linq;
using OfficeOpenXml.Core;
using System.Threading;
using OfficeOpenXml.Utils.FileUtils;
namespace OfficeOpenXml
{
/// <summary>
/// Collection of Excel Comment objects
/// </summary>
public class ExcelCommentCollection : IEnumerable, IDisposable
{
//internal RangeCollection _comments;
List<ExcelComment> _list=new List<ExcelComment>();
List<int> _listIndex = new List<int>();
internal ExcelCommentCollection(ExcelPackage pck, ExcelWorksheet ws, XmlNamespaceManager ns)
{
CommentXml = new XmlDocument();
CommentXml.PreserveWhitespace = false;
NameSpaceManager=ns;
Worksheet=ws;
CreateXml(pck);
AddCommentsFromXml();
}
private void CreateXml(ExcelPackage pck)
{
var commentRels = Worksheet.Part.GetRelationshipsByType(ExcelPackage.schemaComment);
bool isLoaded=false;
CommentXml=new XmlDocument();
foreach(var commentPart in commentRels)
{
Uri = UriHelper.ResolvePartUri(commentPart.SourceUri, commentPart.TargetUri);
Part = pck.ZipPackage.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 ExcelRangeBase(Worksheet, node.GetAttribute("ref")));
_listIndex.Add(_list.Count);
Worksheet._commentsStore.SetValue(comment.Range._fromRow, comment.Range._fromCol, _list.Count);
_list.Add(comment);
}
//_comments = new RangeCollection(lst);
}
/// <summary>
/// Access to the comment xml document
/// </summary>
public XmlDocument CommentXml { get; set; }
internal Uri Uri { get; set; }
internal string RelId { get; set; }
internal XmlNamespaceManager NameSpaceManager { get; set; }
internal Packaging.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
{
get
{
return _listIndex.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 >= _listIndex.Count)
{
throw(new ArgumentOutOfRangeException("Comment index out of range"));
}
return _list[_listIndex[Index]];
}
}
/// <summary>
/// Indexer for the comments collection
/// </summary>
/// <param name="cell">The cell</param>
/// <returns>The comment</returns>
public ExcelComment this[ExcelCellAddress cell]
{
get
{
int i=-1;
if (Worksheet._commentsStore.Exists(cell.Row, cell.Column, ref i))
{
return _list[i];
}
else
{
return null;
}
}
}
/// <summary>
/// Indexer for the comments collection
/// </summary>
/// <param name="cellAddress">The cell address</param>
/// <returns>The comment</returns>
public ExcelComment this[string cellAddress]
{
get
{
return this[new ExcelCellAddress(cellAddress)];
}
}
/// <summary>
/// Adds a comment to the top left cell of the range
/// </summary>
/// <param name="cell">The cell</param>
/// <param name="Text">The comment text</param>
/// <param name="author">The author for the comment. If this property is null or blank EPPlus will set it to the identity of the ClaimsPrincipal if available otherwise to "Anonymous"</param>
/// <returns>The comment</returns>
public ExcelComment Add(ExcelRangeBase cell, string Text, string author = null)
{
//Check no other base comment already on the cell
if (cell._worksheet._commentsStore.Exists(cell._fromRow, cell._fromCol))
{
throw (new InvalidOperationException(string.Format("Cell {0} already contains a comment.", new ExcelCellAddress(cell._fromRow, cell._fromCol).Address)));
}
if (string.IsNullOrEmpty(author))
{
#if Core
author = System.Security.Claims.ClaimsPrincipal.Current?.Identity?.Name;
#else
author = Thread.CurrentPrincipal?.Identity?.Name;
#endif
if (string.IsNullOrEmpty(author))
{
author = "Anonymous";
}
}
var elem = CommentXml.CreateElement("comment", ExcelPackage.schemaMain);
//int ix=_comments.IndexOf(ExcelAddress.GetCellID(Worksheet.SheetID, cell._fromRow, cell._fromCol));
//Make sure the nodes come on order.
int row=cell.Start.Row, column= cell.Start.Column;
ExcelComment nextComment = null;
if (Worksheet._commentsStore.NextCell(ref row, ref column))
{
nextComment = _list[Worksheet._commentsStore.GetValue(row, column)];
}
if(nextComment==null)
{
CommentXml.SelectSingleNode("d:comments/d:commentList", NameSpaceManager).AppendChild(elem);
}
else
{
nextComment._commentHelper.TopNode.ParentNode.InsertBefore(elem, nextComment._commentHelper.TopNode);
}
elem.SetAttribute("ref", cell.Start.Address);
ExcelComment comment = new ExcelComment(NameSpaceManager, elem , cell);
comment.RichText.Add(Text);
comment.Author = author;
_listIndex.Add(_list.Count);
Worksheet._commentsStore.SetValue(cell.Start.Row, cell.Start.Column, _list.Count);
_list.Add(comment);
//Check if a value exists otherwise add one so it is saved when the cells collection is iterated
if (!Worksheet.ExistsValueInner(cell._fromRow, cell._fromCol))
{
Worksheet.SetValueInner(cell._fromRow, cell._fromCol, null);
}
return comment;
}
/// <summary>
/// Removes the comment
/// </summary>
/// <param name="comment">The comment to remove</param>
public void Remove(ExcelComment comment)
{
Remove(comment, false);
}
internal void Remove(ExcelComment comment, bool shift)
{
int i = -1;
ExcelComment c=null;
if (Worksheet._commentsStore.Exists(comment.Range._fromRow, comment.Range._fromCol, ref i))
{
c = _list[i];
}
if (comment==c)
{
comment.TopNode.ParentNode.RemoveChild(comment.TopNode); //Remove VML
comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment
Worksheet.VmlDrawings._drawingsCellStore.Delete(comment.Range._fromRow, comment.Range._fromCol, 1, 1, shift);
Worksheet._commentsStore.Delete(comment.Range._fromRow, comment.Range._fromCol, 1, 1, shift);
_list[i]=null;
_listIndex.Remove(i);
//if(_listIndex.Count==0)
//{
// _list.Clear();
//}
}
else
{
throw (new ArgumentException("Comment does not exist in the worksheet"));
}
}
/// <summary>
/// Shifts all comments based on their address and the location of inserted rows and columns.
/// </summary>
/// <param name="fromRow">The start row.</param>
/// <param name="fromCol">The start column.</param>
/// <param name="rows">The number of rows to insert.</param>
/// <param name="columns">The number of columns to insert.</param>
/// <param name="toRow">If the delete is in a range, this is the end row</param>
/// <param name="toCol">If the delete is in a range, this the end column</param>
internal void Delete(int fromRow, int fromCol, int rows, int columns, int toRow = ExcelPackage.MaxRows, int toCol = ExcelPackage.MaxColumns)
{
List<ExcelComment> deletedComments = new List<ExcelComment>();
ExcelAddressBase address = null;
foreach (ExcelComment comment in _list.Where(x=>x!=null))
{
address = new ExcelAddressBase(comment.Address);
if (columns > 0 && address._fromCol >= fromCol &&
address._fromRow >= fromRow && address._toRow <= toRow)
{
address = address.DeleteColumn(fromCol, columns);
}
if (rows > 0 && address._fromRow >= fromRow &&
address._fromCol >= fromCol && address._toCol <= toCol)
{
address = address.DeleteRow(fromRow, rows);
}
if(address==null || address.Address=="#REF!")
{
deletedComments.Add(comment);
}
else
{
comment.Reference = address.Address;
}
}
foreach(var comment in deletedComments)
{
comment.TopNode.ParentNode.RemoveChild(comment.TopNode); //Remove VML
comment._commentHelper.TopNode.ParentNode.RemoveChild(comment._commentHelper.TopNode); //Remove Comment
var ix = _list.IndexOf(comment);
_list[ix] = null;
_listIndex.Remove(ix);
}
}
/// <summary>
/// Shifts all comments based on their address and the location of inserted rows and columns.
/// </summary>
/// <param name="fromRow">The start row</param>
/// <param name="fromCol">The start column</param>
/// <param name="rows">The number of rows to insert</param>
/// <param name="columns">The number of columns to insert</param>
/// <param name="toRow">If the insert is in a range, this is the end row</param>
/// <param name="toCol">If the insert is in a range, this the end column</param>
internal void Insert(int fromRow, int fromCol, int rows, int columns, int toRow = ExcelPackage.MaxRows, int toCol=ExcelPackage.MaxColumns)
{
foreach (ExcelComment comment in _list.Where(x => x != null))
{
var address = new ExcelAddressBase(comment.Address);
if (rows > 0 && address._fromRow >= fromRow &&
address._fromCol >= fromCol && address._toCol <= toCol)
{
comment.Reference = comment.Range.AddRow(fromRow, rows).Address;
}
if(columns>0 && address._fromCol >= fromCol &&
address._fromRow >= fromRow && address._toRow <= toRow)
{
comment.Reference = comment.Range.AddColumn(fromCol, columns).Address;
}
}
}
void IDisposable.Dispose()
{
}
/// <summary>
/// Removes the comment at the specified position
/// </summary>
/// <param name="Index">The index</param>
public void RemoveAt(int Index)
{
Remove(this[Index]);
}
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return _list.Where(x=>x!=null).GetEnumerator();
}
#endregion
internal void Clear()
{
while(Count>0)
{
RemoveAt(0);
}
}
internal ExcelComment GetByListIndex(int ix)
{
return _list[ix];
}
}
}
You can’t perform that action at this time.
