Apply Comments

Spread.Services Documentation
Apply Comments
Collapse All Expand All

Spread.Services Documentation > Developer's Guide > Customize User Interaction > Apply Comments

You can add comments in cells to annotate a worksheet with additional data related to the information entered in that particular cell. The inserted comments can be edited, formatted, resized, moved and deleted.

The following tasks can be performed while applying comments in cells of a spreadsheet:

- Add comment to a cell
- Set comment layout
- Show/Hide comment
- Author comments
- Set rich text for comment
- Delete comment

### Add comment to a cell

In Spread.Services, a cell comment instance is represented by the IComment interface. You can insert comment to a cell or a range of cells using the AddComment method of the IRange interface. You can also set the text of the comment using the Text property of the IComment interface.

Refer to the following example code to add comment to a cell.

C# | Copy Code
--- | ---

    //Change the text of the comment.
    commentC3.Text = "Range C3's new comment.";

### Set comment layout

You can configure the layout of the comment added to an individual cell or a range of cells using Shape property of the IComment interface.

Refer to the following example code to set comment layout.

C# | Copy Code
--- | ---

    //Configure comment layout

    commentC3.Shape.Line.Color.RGB = Color.Green;
    commentC3.Shape.Line.Weight = 7;

    commentC3.Shape.Fill.Color.RGB = Color.Gray;
    commentC3.Shape.Width = 100;
    commentC3.Shape.Height = 200;
    commentC3.Shape.TextFrame.TextRange.Font.Bold = true;
    commentC3.Visible = true;

### Show/Hide comment

You can choose to keep comments hidden or visible by using the Visible property of the IComment interface.

Refer to the following example code to show/hide comment added to a cell.

C# | Copy Code
--- | ---

    //Hide Comment
    worksheet.Range["C3"].Comment.Visible = true;
    worksheet.Range["C:C"].EntireColumn.Hidden = true;

    //Show Comment
    var commentVisible = worksheet.Range["C3"].Comment.Visible;
    //Range C3's comment container shape's visible is false, because column hidden.
    var shapeVisible = worksheet.Range["C3"].Comment.Shape.Visible;

### Author comments

You can represent the author of the comment by using the Author property of the IComment interface. Also, you can use this property to change the author of an existing comment.

Refer to the following example code to set comment author for a cell.

C# | Copy Code
--- | ---

    // Set comment author
    workbook.Author = "joneshan";
    worksheet.Range["H6"].AddComment("H6's comment.");
    //H6's comment author is "joneshan".
    var authorH6 = worksheet.Range["H6"].Comment.Author;

### Set rich text for comment

You can set the rich text for the comment using the properties and methods of the ITextFrame Interface that control the text style.
Refer to the following example code to set rich text for the comment.

C# | Copy Code
--- | ---

    //Rich Text and TextFrame
    commentC3.Shape.TextFrame.TextRange.Paragraphs.Add("aaa");
    commentC3.Shape.TextFrame.TextRange.Paragraphs.Add("bbb", 0);

    commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Font.Bold = true;
    commentC3.Shape.TextFrame.TextRange.Paragraphs[1].Font.Size = 30;

    commentC3.Shape.TextFrame.TextRange.Paragraphs[1].Runs.Add("ccc", 0);
    commentC3.Shape.TextFrame.TextRange.Paragraphs[1].Runs.Add("ddd");
    commentC3.Shape.TextFrame.TextRange.Paragraphs[1].Runs.Add("eee", 1);

    commentC3.Shape.TextFrame.TextRange.Paragraphs[1].Runs[2].Font.Italic = true;

    commentC3.Visible = true;

    var text = commentC3.Text;

### Delete comment

You can delete the comment added to a cell or to a cell range using the Delete method of the IComment interface and the IRange interface respectively.

Refer to the following example code to delete comment from a cell.

C# | Copy Code
--- | ---

    // Delete Comment instance
    commentC3.Delete();

Copyright © GrapeCity, inc. All rights reserved.