title: Creating anchored comments programmatically in Google Docs
body: <p>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.</p>
  
  <p>Google documentation about how to create an anchored comment using the Google Drive API:
  <a href="https://developers.google.com/drive/web/manage-comments" rel="noreferrer">https://developers.google.com/drive/web/manage-comments</a></p>
  
  <p>Here is the code I have to create the comment in the doc:</p>
  
  <pre class="lang-js prettyprint-override"><code>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  */
  </code></pre>
  
  <p>In this case, I am trying to create a text anchor to specific characters in the doc which correspond to the <code>STARTING_OFFSET</code> and <code>OFFSET_LENGTH</code>.</p>
  
  <p>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.</p>
  
  <p>I have made sure that the current revision id is used (as this could cause issues, as mentioned in the Google documentation).</p>
  
  <p>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.</p>
  
  <p>Thanks in advance!</p>