How to: Create, Edit and Copy Simple Notes in Spreadsheet for WPF
- 4 minutes to read
This example demonstrates how to create a new simple note (legacy comment in Excel documents), edit its text and copy the existing note to another cell.
Create a New Simple Note
To create a new note and associate it with a cell, access the worksheet’s collection of cell comments from the Worksheet.Comments property and call the CommentCollection.Add method.
To make the added note visible in the cell, set the Comment.Visible property to true
.
Edit Note Text
To edit the note text, modify the CommentRunCollection collection obtained by the Comment.Runs property. This collection stores the CommentRun objects that define separate regions of the note text. After a note is created, its text is defined by a single run contained in the CommentRunCollection collection. You can add more runs to the note, edit the text of the existing runs or completely remove them from the note.
Copy Simple Notes
To copy the existing simple note to another cell, call the CellRange.CopyFrom method of the Cell object corresponding to the cell into which you wish to insert the copied note. Pass the source cell and the PasteSpecial.Comments enumerator value to insert only the note and ignore cell content. The copied note replaces any existing note in the destination area.
The copied note preserves the structure of the source note and contains the same number of runs. To edit the text of the copied note, call the CommentCollection.GetComments method to obtain the inserted comment, and then modify the CommentRun.Text property of the second run.
Remove Simple Notes
Use the following method to remove simple notes:
Example: Create and Edit a Simple Note
The code sample below creates a new simple note, edits its text, and copies the note to another cell.
// Get the system username.
string author = workbook.CurrentAuthor;
// Add a comment to the "A2" cell.
Cell commentedCell = worksheet.Cells["A2"];
Comment commentA2 = worksheet.Comments.Add(commentedCell, author, "This is a comment");
commentA2.Visible = true;
// Insert the author's name at the beginning of the comment.
CommentRunCollection commentRunsA2 = commentA2.Runs;
commentRunsA2.Insert(0, author + ": \r\n");
// Copy the comment to the "E2" cell.
worksheet.Cells["E2"].CopyFrom(commentedCell, PasteSpecial.Comments);
// Get the added comment and make it visible.
Comment commentE2 = worksheet.Comments.GetComments(worksheet["E2"])[0];
commentE2.Visible = true;
// Modify text of the copied comment.
CommentRunCollection commentRunsE2 = commentE2.Runs;
commentRunsE2[1].Text = "This comment is copied from the cell " + commentedCell.GetReferenceA1();
Manage Comments in the UI
End-users can insert, edit, hide or delete notes via the ribbon command UI (commands located in the Comments group of the Review tab) or context menu.
Use the sizing handles (small rectangles at the corners and sides of the comment box) visible for a selected comment to resize a comment or move it to a new location.
End-User Restrictions
To restrict end-user actions over simple notes, use the SpreadsheetBehaviorOptions.Comment property. The table below lists possible restrictions you can set to prevent modifications of comments in the SpreadsheetControl UI.
Restriction | Description |
---|---|
Insert | Gets or sets whether a user can create new comments. |
Edit | Gets or sets whether a user can edit the existing comments. |
Delete | Gets or sets whether a user can delete comments. |
ShowHide | Gets or sets whether a user can display or hide comments. |
ShowOnHover | Gets or sets whether to show comments when a user hovers over a cell with a comment indicator. |
Resize | Gets or sets whether a user can change the size of the comment box. |
Move | Gets or sets whether a user can move comments to a new location. |
Set the required property to DocumentCapability.Disabled or DocumentCapability.Hidden to disable or hide the corresponding command in the ribbon UI and the context menu.
Manage Threaded Comments
The SpreadsheetControl allows you to manage threaded comments in code. Refer to the following article for more information: Threaded Comments in Spreadsheet for WPF