How to detect real-time changes or auto-save events in Google Docs?

I’m working on a Google Docs Add-on and I’m stuck. My add-on should highlight specific words in the document based on sidebar input. I’ve got it working when the user clicks a button in the sidebar. But I want it to work automatically as the user types or when the doc auto-saves.

I’ve looked into Google App Script’s onEdit trigger, but it only works for Spreadsheets. Does anyone know how to make this work for Docs? Is there a way to listen for document changes or auto-save events?

Here’s a basic example of what I’ve got so far:

function highlightWords() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var wordToHighlight = 'example';

  var foundElement = body.findText(wordToHighlight);
  while (foundElement != null) {
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();
    foundElement.getElement().asText().setBackgroundColor(start, end, '#FFFF00');
    foundElement = body.findText(wordToHighlight, foundElement);
  }
}

Any tips or documentation pointers would be super helpful!

I’ve been down this road before, and it’s a tricky one. While there’s no perfect solution, I found a workaround that might help. Try using Google Apps Script’s time-driven triggers. Set up a function to run every few minutes that compares the current document content with a stored version. If there’s a difference, trigger your highlighting function.

Here’s a rough idea:

function checkForChanges() {
  var doc = DocumentApp.getActiveDocument();
  var currentContent = doc.getBody().getText();
  var properties = PropertiesService.getDocumentProperties();
  var storedContent = properties.getProperty('lastContent');

  if (currentContent !== storedContent) {
    highlightWords();
    properties.setProperty('lastContent', currentContent);
  }
}

Set this to run every 3-5 minutes. It’s not real-time, but it’s a decent compromise. Just be mindful of quota limits and performance impact on larger docs.

Unfortunately, there’s no built-in way to detect real-time changes or auto-save events in Google Docs using Google Apps Script. The onEdit trigger you mentioned is indeed limited to Spreadsheets.

For Docs, you’ll need to implement a workaround. One approach is to use time-based triggers to periodically check for changes. Set up a trigger to run your highlightWords function every minute or so. This isn’t real-time, but it’s the closest you can get.

Another option is to add a listener to the document’s body that calls your function whenever the user types. However, this method can be resource-intensive and may slow down the document for users.

Remember that frequent API calls can hit quota limits, so be mindful of how often you’re running your function. You might want to implement some optimization to only highlight new or changed content rather than re-processing the entire document each time.

hey there, i’ve had similar issues. one hack i found is using the DocumentApp.getActiveDocument().getBody().getText() method to grab the whole doc content. then compare it to a stored version. if different, run ur highlighting. not perfect, but works ok for most cases. good luck!