I would like to programmatically edit my Google Doc externally via a Google Chrome extension or a simple JavaScript and see the changes live (in real-time) in the Google Doc. When this question came up, I was looking for Chrome Extensions that edit a Google Doc and save the changes programmatically. During my research, I came across Grammarly. I am impressed by how they manage to apply their spelling corrections to the Google Doc in near real-time. You can reproduce it like this:
- Install Grammarly Chrome Extension
- Open/create a Google Doc
- Let Grammarly check your text (words that contain errors are highlighted)
- Left click on a highlighted word
- Click on the suggested correction
Grammarly will then update the Google Doc. What I noticed thereby:
- The Google Doc doesn't seem to be updated via the Google Docs API or Google AppScript
- The saving process seems to behave the same as the official autosave mechanism of Google Docs itself when the user manually edits the doc. This can be explained as follows:
- Autosave indicator is triggered
- Google Docs
save
HTTP request is executed
The HTTP FormData also contains the appropriate parameter combination (an existing word within the index rangesi
(startIndex) andei
(endIndex) defined in the 1st command of the array is replaced by the new word in the 2nd command)
- Autosave indicator is triggered
[{"commands":[{"ty":"ds","si":229,"ei":232}, {"ty":"is","ibi":229,"s":"Test"}]}]
I have already tried the following solutions:
- Use the Google Docs API.
Result: ✓ works but with a noticeable delay of up to 5 seconds
gapi.client.docs.documents.batchUpdate({
documented: <docId>,
requests: [
{
deleteContentRange: {
range: {
startIndex: 1,
endIndex: 10,
},
},
},
{
insertText: {
location: {
index: 1,
},
text: 'Lorem ipsum',
},
},
],
})
- Use the Google Script API to execute AppScript function.
Result: ✓ works but with a noticeable delay of up to 5 seconds
// API call
await gapi.client.script.scripts.run({
scriptId: <scriptId>,
resource: {
function: 'myFunction'
}
})
// AppScript function from Google Script Editor
function myFunction() {
var body = DocumentApp.openById(<docId>).getBody()
body.appendParagraph("Lorem ipsum")
}
- Manipulate the DOM directly in the Google Doc (here I tried to edit the text of the Google Doc with JavaScript and then save it).
Result: ✗ I couldn't find a way to trigger the autosave mechanism - Manual execution of the internal Google Docs (auto-) save method.
Result: ✗ led to an Internal Server Error
Unfortunately, all attempts so far have been unsuccessful or have not delivered the desired result.
save
function is executed - exactly with the parameters that are responsible for replacing one word with another. – Sophoclessave
request is executed – Sophoclessave
didn't trigger either. History only consists of my changes I manually made in docs. So I doubt it triggers in docs. Btw. are we talking about AppScript or gapi? – Sophoclessave
function should be triggered? Not sure if that really solves my problem, though. – Sophocles