Creating anchored comments programmatically in Google Docs

I have been unable to programmatically create a comment that is anchored to specific text in a Google Doc using an app script with the document.

Google documentation about how to create an anchored comment using the Google Drive API: https://developers.google.com/drive/web/manage-comments

Here is the code I have to create the comment in the doc:

Drive.Comments.insert({
  "kind": "drive#comment",
  "author": {
    "kind": "drive#user",
    "displayName": USER_EMAIL,
    "isAuthenticatedUser": true,
  },
  "content": CONTENT,
  "status": "open",
  "anchor": "{'r':"
             + REVISION_ID
             + ",'a':[{'txt':{'o':"
             + STARTING_OFFSET
             + ",'l':"
             + OFFSET_LENGTH
             + ",'ml':"
             + TOTAL_CHARS
             + "}}]}", 
  "fileId": FILE_ID
}, FILE_ID);

 /* USER_EMAIL, CONTENT, REVISION_ID, FILE_ID: string,
    STARTING_OFFSET, OFFSET_LENGTH, TOTAL_CHARS: int  */

In this case, I am trying to create a text anchor to specific characters in the doc which correspond to the STARTING_OFFSET and OFFSET_LENGTH.

Currently this code creates a comment accessible in Google Docs; however, it is not anchored to any text in the document, which is necessary for the intended purpose.

I have made sure that the current revision id is used (as this could cause issues, as mentioned in the Google documentation).

Any suggestions are greatly appreciated and if any one could post or point to an example of creating anchored comments in Google Docs programmatically, I would be extremely grateful.

Thanks in advance!


Solution 1:

The Anchoring Comments feature from the Google Drive API is intended for non-Google Docs editors files, not for Google Documents. See https://youtu.be/ZBU52nacbLw?t=5m26s (credit to Bryan P who shared this URL through a comment)

Unfortunatelly at this time the Document Service from Google Apps Script doesn't include a Class Comment to handle comments and discussions.

At the beginning of 2016 a feature request was posted through the Google Apps Scripts Issues and features requests official site. By starring this feature request you could help to make that Googlers put attention to it:

Issue 5650: Provide ability to create a Drive API Comment anchor resource as method on DocumentApp selection class

An older reference, posted on 2012, very similar but broader, referred through a comment by Alexander:
Issue 1618: Provide read/write access to comments in Google Docs

References

  • Manage Comments and Discussions - Google Drive APIs - REST