I'm building a Chrome Extension that adds functionality on Google Docs pages. I'm looking to programmatically add a comment anchored to a specific piece of text in a Google Doc for my users. I've reached dead ends with many different approaches, but wanted to see if there is any other approach I can consider.
These are the approaches I've tried:
- Drive v3 Comments Create API: This allows me to add comments, but everything I've tried with the anchor parameter results in an unanchored comment. My conclusion is the anchor parameter is solely for not Google Docs, though I'm surprised the documentation doesn't make that more explicit. Things I've tried for anchor:
- a. Creating a named range with createNamedRange and passing that named range name in as anchor (also tried prepending it with "r-"
- a. Passing in range as JSON in this format: {'segmentId': '', 'startIndex': 2, 'endIndex': 4}
- a. Passing in range as JSON in this format: {'r': 'HEAD', 'a': [{'line': {'n': 2, 'l': 2}}]}
- Running remote Apps Script: A Apps Script bound to the current document can select text in a the document, but it can't add comments. So my hope was to trigger the apps script that selects text and then run JS that manually enters the comment in the UI. The problem is that I don't see any way to run the highlighting Apps Script as a Container Bound script on Google Docs which seems necessary to use functions like getActiveDocument and setSelection. I can run it through the JS or python APIs, but that approach doesn't affect the onscreen document.
- Use chrome.scripting APIs to execute the code. This seems to have the same limitations where I can't access Container Bound functionality.
- Change the selection via the DOM by forcing an annotated HTML canvas: It seems Google still supports this for Legacy extensions through whitelisting, though this approach seems unlikely to be approved for new extensions applying to the open web (instead of just inside a company for internal tools).
I'd love any advice anyone can offer, though so far it seems the few people I've seen in forums who have pursued this haven't reached a solution. The most similar question is here: Creating anchored comments programmatically in Google Docs though I was hoping in the eight years since it was posted, new options might be available (and that question only considered API side solutions).