Manage comments and replies

Comments are user-provided feedback on a file, such as a reader of a word processing document suggesting how to rephrase a sentence. There are two types of comments: anchored comments and unanchored comments. An anchored comment is associated with a specific location, such as a sentence in a word processing document, within a specific version of a document. Conversely, an unanchored comment is just associated with the document.

Replies are attached to comments and represent a user's response to the comment. The Drive API enables you to allow your users to add comments and replies to documents created by your app. Collectively, a comment with replies is known as a discussion.

Add an unanchored comment

To add an unanchored comment to a document, call comments.create with the fileId and a comments resource containing the comment.

The comment is inserted as plain text, but the response body provides an htmlContent field containing content formatted for display.

Add a reply to a comment

To add a reply to a comment, call replies.create with the comment, fileId, and a reply resource containing the reply.

The reply is inserted as plain text, but the response body provides an htmlContent field containing content formatted for display.

Add an anchored comment to the latest revision of a document

When you add a comment, you might want to anchor it to a region in the file. An anchor defines the file revision and region in a file to which a comment refers. The Comments resource defines the anchor field as a JSON string.

To add an anchored comment:

  1. (Optional). Call revisions.list to list all of the revisionIDs for a document. Only follow this step if you want to anchor a comment to any revision other than the latest revision. If you want to use the latest revision, use head for the revisionID.

  2. Call comments.create with the fileID, a comments resource containing the comment, and a JSON anchor string containing the revisionID (r) and region (a).

How you define a region depends on the type of document content you are working with. For further information, refer to Define a region.

Define a region

As mentioned earlier, the JSON anchor string contains a revisionID (r) and region (a). The region (a) is a JSON array containing region classifiers specifying the format and location to which a comment is anchored. A classifier might be a as a two-dimensional rectangle for an image, a line of text in a document, a time duration in a video, and so on. To define a region, select the region classifier that matches the type of content you are trying to anchor to. For example, if your content is text, you are likely going to use either txt or line region classifier.

For a list of region classifiers provided in the Google Drive API, refer to Region classifiers.

The following example shows a JSON anchor string that anchors comments to lines in two separate areas of a document:

  • The first area starts at line 12 ('n':12) and extends for 3 lines ('l':3).
  • The second area only covers line 18 ('n':12, 'l':`).
    {
      'r': revisionId,
      'a': [
      {
        'line':
        {
          'n': 12,
          'l': 3,
        }
      },
      {
        'line':
        {
          'n': 18,
          'l': 1,
        }
      }]
    }

Resolve a comment

Use the comment.update method to set the resolved property in the comments resource to true when a comment has been addressed.

When your app sets the resolved property to true, your UI should indicate that the comment has been addressed. For example, your app might:

  • disallow further replies and greying out all previous replies and the original comment.
  • hide resolved comments.

Delete a comment

Use the comments.delete method to delete comments. When a comment is deleted, Google Drive marks the comment resource as "deleted": "true".

List comments

Use the comments.list method to list comments. If you want to include deleted comments in the results, set the includedDeleted field to true.