CommentRangeStart class

CommentRangeStart class

Denotes the start of a region of text that has a comment associated with it. To learn more, visit the Working with Comments documentation article.

Remarks

To create a comment anchored to a region of text, you need to create a Comment and then create CommentRangeStart and CommentRangeEnd and set their identifiers to the same Comment.id value.

CommentRangeStart is an inline-level node and can only be a child of Paragraph.

Inheritance: CommentRangeStartNode

Constructors

NameDescription
CommentRangeStart(doc, id)Initializes a new instance of this class.

Properties

NameDescription
customNodeIdSpecifies custom node identifier.
(Inherited from Node)
documentGets the document to which this node belongs.
(Inherited from Node)
idSpecifies the identifier of the comment to which this region is linked.
isCompositeReturns true if this node can contain other nodes.
(Inherited from Node)
nextSiblingGets the node immediately following this node.
(Inherited from Node)
nodeTypeReturns NodeType.CommentRangeStart.
parentNodeGets the immediate parent of this node.
(Inherited from Node)
previousSiblingGets the node immediately preceding this node.
(Inherited from Node)
rangeReturns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node)

Methods

NameDescription
asBody()Cast node to Body.
(Inherited from Node)
asBookmarkEnd()Cast node to BookmarkEnd.
(Inherited from Node)
asBookmarkStart()Cast node to BookmarkStart.
(Inherited from Node)
asBuildingBlock()Cast node to BuildingBlock.
(Inherited from Node)
asCell()Cast node to Cell.
(Inherited from Node)
asComment()Cast node to Comment.
(Inherited from Node)
asCommentRangeEnd()Cast node to CommentRangeEnd.
(Inherited from Node)
asCommentRangeStart()Cast node to CommentRangeStart.
(Inherited from Node)
asCompositeNode()Cast node to CompositeNode.
(Inherited from Node)
asDocument()Cast node to Node.document.
(Inherited from Node)
asEditableRangeEnd()Cast node to EditableRangeEnd.
(Inherited from Node)
asEditableRangeStart()Cast node to EditableRangeStart.
(Inherited from Node)
asFieldEnd()Cast node to FieldEnd.
(Inherited from Node)
asFieldSeparator()Cast node to FieldSeparator.
(Inherited from Node)
asFieldStart()Cast node to FieldStart.
(Inherited from Node)
asFootnote()Cast node to Footnote.
(Inherited from Node)
asFormField()Cast node to FormField.
(Inherited from Node)
asGlossaryDocument()Cast node to GlossaryDocument.
(Inherited from Node)
asGroupShape()Cast node to GroupShape.
(Inherited from Node)
asHeaderFooter()Cast node to HeaderFooter.
(Inherited from Node)
asOfficeMath()Cast node to OfficeMath.
(Inherited from Node)
asParagraph()Cast node to Paragraph.
(Inherited from Node)
asRow()Cast node to Row.
(Inherited from Node)
asRun()Cast node to Run.
(Inherited from Node)
asSection()Cast node to Section.
(Inherited from Node)
asShape()Cast node to Shape.
(Inherited from Node)
asSmartTag()Cast node to SmartTag.
(Inherited from Node)
asSpecialChar()Cast node to SpecialChar.
(Inherited from Node)
asStructuredDocumentTag()Cast node to StructuredDocumentTag.
(Inherited from Node)
asStructuredDocumentTagRangeEnd()Cast node to StructuredDocumentTagRangeEnd.
(Inherited from Node)
asStructuredDocumentTagRangeStart()Cast node to StructuredDocumentTagRangeStart.
(Inherited from Node)
asSubDocument()Cast node to SubDocument.
(Inherited from Node)
asTable()Cast node to Table.
(Inherited from Node)
clone(isCloneChildren)Creates a duplicate of the node.
(Inherited from Node)
getAncestor(ancestorType)Gets the first ancestor of the specified NodeType.
(Inherited from Node)
getText()Gets the text of this node and of all its children.
(Inherited from Node)
nextPreOrder(rootNode)Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node)
nodeTypeToString(nodeType)A utility method that converts a node type enum value into a user friendly string.
(Inherited from Node)
previousPreOrder(rootNode)Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node)
referenceEquals(other)
(Inherited from Node)
remove()Removes itself from the parent.
(Inherited from Node)
toString(saveFormat)Exports the content of the node into a string in the specified format.
(Inherited from Node)
toString(saveOptions)Exports the content of the node into a string using the specified save options.
(Inherited from Node)

Examples

Shows how print the contents of all comments and their comment ranges using a document visitor.

test('CreateCommentsAndPrintAllInfo', () => {
  let doc = new aw.Document();

  let newComment = new aw.Comment(doc);
  newComment.author = "VDeryushev";
  newComment.initial = "VD",
  newComment.dateTime = Date.now();

  newComment.setText("Comment regarding text.");

  // Add text to the document, warp it in a comment range, and then add your comment.
  let para = doc.firstSection.body.firstParagraph;
  para.appendChild(new aw.CommentRangeStart(doc, newComment.id));
  para.appendChild(new aw.Run(doc, "Commented text."));
  para.appendChild(new aw.CommentRangeEnd(doc, newComment.id));
  para.appendChild(newComment); 

  // Add two replies to the comment.
  newComment.addReply("John Doe", "JD", Date.now(), "New reply.");
  newComment.addReply("John Doe", "JD", Date.now(), "Another reply.");

  printAllCommentInfo(doc.getChildNodes(aw.NodeType.Comment, true));
});


/// <summary>
/// Iterates over every top-level comment and prints its comment range, contents, and replies.
/// </summary>
function printAllCommentInfo(comments)
{
  let commentVisitor = new CommentInfoPrinter();

    // Iterate over all top-level comments. Unlike reply-type comments, top-level comments have no ancestor.
  foreach (Comment comment in comments.Where(c => ((Comment)c).Ancestor == null).ToList())
  {
      // First, visit the start of the comment range.
    let commentRangeStart = (CommentRangeStart)comment.previousSibling.previousSibling.previousSibling;
    commentRangeStart.accept(commentVisitor);

      // Then, visit the comment, and any replies that it may have.
    comment.accept(commentVisitor);
      // Visit only start of the comment.
    comment.acceptStart(commentVisitor);
      // Visit only end of the comment.
    comment.acceptEnd(commentVisitor);

    for (let reply of comment.replies)
      reply.accept(commentVisitor);

      // Finally, visit the end of the comment range, and then print the visitor's text contents.
    let commentRangeEnd = (CommentRangeEnd)comment.previousSibling;
    commentRangeEnd.accept(commentVisitor);

    console.log(commentVisitor.getText());
  }
}

  /// <summary>
  /// Prints information and contents of all comments and comment ranges encountered in the document.
  /// </summary>
public class CommentInfoPrinter : DocumentVisitor
{
  public CommentInfoPrinter()
  {
    mBuilder = new StringBuilder();
    mVisitorIsInsideComment = false;
  }

    /// <summary>
    /// Gets the plain text of the document that was accumulated by the visitor.
    /// </summary>
  public string GetText()
  {
    return mBuilder.toString();
  }

    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
  public override VisitorAction VisitRun(Run run)
  {
    if (mVisitorIsInsideComment) IndentAndAppendLine("[Run] \"" + run.text + "\"");

    return aw.VisitorAction.Continue;
  }

    /// <summary>
    /// Called when a CommentRangeStart node is encountered in the document.
    /// </summary>
  public override VisitorAction VisitCommentRangeStart(CommentRangeStart commentRangeStart)
  {
    IndentAndAppendLine("[Comment range start] ID: " + commentRangeStart.id);
    mDocTraversalDepth++;
    mVisitorIsInsideComment = true;

    return aw.VisitorAction.Continue;
  }

    /// <summary>
    /// Called when a CommentRangeEnd node is encountered in the document.
    /// </summary>
  public override VisitorAction VisitCommentRangeEnd(CommentRangeEnd commentRangeEnd)
  {
    mDocTraversalDepth--;
    IndentAndAppendLine("[Comment range end] ID: " + commentRangeEnd.id + "\n");
    mVisitorIsInsideComment = false;

    return aw.VisitorAction.Continue;
  }

    /// <summary>
    /// Called when a Comment node is encountered in the document.
    /// </summary>
  public override VisitorAction VisitCommentStart(Comment comment)
  {
    IndentAndAppendLine(
      `[Comment start] For comment range ID ${comment.id}, By ${comment.author} on ${comment.dateTime}`);
    mDocTraversalDepth++;
    mVisitorIsInsideComment = true;


    return aw.VisitorAction.Continue;
  }

    /// <summary>
    /// Called when the visiting of a Comment node is ended in the document.
    /// </summary>
  public override VisitorAction VisitCommentEnd(Comment comment)
  {
    mDocTraversalDepth--;
    IndentAndAppendLine("[Comment end]");
    mVisitorIsInsideComment = false;

    return aw.VisitorAction.Continue;
  }

    /// <summary>
    /// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree.
    /// </summary>
    /// <param name="text"></param>
  private void IndentAndAppendLine(string text)
  {
    for (let i = 0; i < mDocTraversalDepth; i++)
    {
      mBuilder.append("|  ");
    }

    mBuilder.AppendLine(text);
  }

  private bool mVisitorIsInsideComment;
  private int mDocTraversalDepth;
  private readonly StringBuilder mBuilder;
}

See Also