评论
从R2 2022开始,SpreadProcessing库支持使用注释。注释用于标记关于单元格数据的信息,可以有一个或多个注释回复.所有评论都可以在评论属性,该属性的类型为CommentCollection.这个集合包含SpreadsheetComment对象,它们表示注释。每个注释都有以下成员:
属性:
- CellIndex:获取或设置注释分配给的单元格索引。
- 文本:获取或设置注释的文本。
- 创建日期:获取或设置创建注释的日期。可以为空。
- 作者:获取或设置分配给注释的作者。
- 回答:获取注释回复。该列表按CreationDate排序。
IsResolved:获取或设置一个值,该值指示注释是否已解析。
上述所有属性在修改时都会将更改推入撤销堆栈。
方法:
- AddReply:添加一个SpreadsheetCommentReply到ReplySortedCollection。在添加对象后,集合将由SpreadsheetReply的CreationDate按升序重新排序。
- RemoveReply:从集合中删除指定的应答。
使用CommentCollection
添加注释
要添加注释,您需要指定与注释相关的单元格索引、作者、文本内容和创建日期。指定创建日期是可选的,默认情况下,它的值是当前日期和时间。
例1:添加注释
CellIndex relatedCellIndex = new CellIndex(1,1);string作者= "John Doe";string text = "评论内容";DateTime creationDate = DateTime. now;worksheet.Comments。添加(relatedCellIndex, author, text, creationDate);
删除评论
若要删除注释,应指定注释实例。该实例可以从CommentCollection.
例2:删除注释
SpreadsheetComment comment =工作表。comments [0];worksheet.Comments.Remove(评论);
回复
每条评论都可以被回复,形成一条信息的线索。所有回复可在回复属性的注释,该注释的类型为ReplySortedCollection.这个集合包含SpreadsheetCommentReply表示应答的对象。的ReplySortedCollection具有以下成员:
属性:
- 数:获取ReplySortedCollection中包含的元素数。
方法:
- 添加:添加一个SpreadsheetCommentReply到ReplySortedCollection.集合将被重新排序SpreadsheetReply的CreationDate在添加对象后按升序排列。需要类型的对象SpreadsheetCommentReply并可用于添加现有的回复。对于添加新的回复,最好使用SpreadsheetComment.AddReply ()方法。
- 删除:删除指定的SpreadsheetCommentReply对象从ReplySortedCollection。
- RemoveAt:元素的指定索引处的元素ReplySortedCollection.
- 明确:元素中的所有元素ReplySortedCollection.
- 包含:元素中是否存在元素ReplySortedCollection.
- CopyTo:复制整个ReplySortedCollection到兼容的一维数组,从目标数组的指定索引开始。
例3:使用答复
void MyProgram(){工作簿工作簿=新工作簿();工作表工作表= workbook.Worksheets.Add();string text = "第一条评论";CellIndex relatedCellIndex = new CellIndex(0,0);// Cell A1 AddCommentWithRepliesToWorksheet(worksheet, relatedCellIndex, text, 2);//第一条评论将有2个回复relatedCellIndex = new CellIndex(1,1);// Cell B2 text = "Second Comment";AddCommentWithRepliesToWorksheet(worksheet, relatedCellIndex, text, 0);//第二个注释将有0个回复//使用Add()方法添加现有的回复SpreadsheetCommentReplyvar firstComment =工作表。comments [0];var secondComment =工作表。comments [1];SpreadsheetCommentReply reply = firstComment.Replies[0];secondComment.Replies.Add(回复);//从firstComment复制回复#1到secondComment //删除firstComment. replies .Remove(reply);/ /清楚firstComment.Replies.Clear ();/ /包含firstComment.Replies.Contains(回复);//返回false //CopyTo SpreadsheetCommentReply[] replyArray = new SpreadsheetCommentReply[1];secondComment.Replies。CopyTo (replyArray 0); } void AddCommentWithRepliesToWorksheet(Worksheet worksheet, CellIndex relatedCellIndex, string commentText, int repliesCount) { string authorName = "Jane Doe"; DateTime creationDate = DateTime.Now; SpreadsheetComment comment = worksheet.Comments.Add(relatedCellIndex, authorName, commentText, creationDate); for (int i = 0; i < repliesCount; i++) { string replyText = "Reply #" + (i + 1); comment.AddReply(authorName, replyText, creationDate); // Add new reply using the SpreadsheetComment.AddReply() method } }
事件
这两个CommentCollection而且ReplySortedCollection暴露以下事件,这两种类型的工作方式相同:
- 改变:在更改集合之前发生。
- 改变:在集合更改后发生。
两个集合的两个事件使用类似的枚举类型作为事件参数,有两个可能的值:
- 添加:添加评论或回复时使用
- 删除:用于删除注释或回复
例4:在将注释添加到CommentCollection时,使用Changing事件更改注释的作者
void Comments_Changing(对象发送者,ShapeCollectionChangingEventArgs e) {SpreadsheetComment comment = e. shape;if (e.ChangeType == ShapeCollectionChangeType.Add){注释。作者= "评论作者";}}
示例5:在使用Changing事件将应答添加到ReplySortedCollection时更改应答的作者
void Replies_Changing(对象发件人,ReplySortedCollectionChangingEventArgs e) {if (e. changetype == replysortedcollectionchangettype . add) {e.Reply.Author = "回复作者";}}