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).