Home > OS >  Script to auto highlight certain words in Google Docs?
Script to auto highlight certain words in Google Docs?

Time:10-03

I'm looking for a simple script for a Google Docs add-on that will highlight words held in an array. I want the script to auto-highlight the words as I type. Is this simple enough?

Thanks!

CodePudding user response:

Based on Can I color certain words in Google Document using Google Apps Script?

function highlight_words() {
  var doc   = DocumentApp.getActiveDocument();
  var words = ['one','two','three'];
  var style = { [DocumentApp.Attribute.BACKGROUND_COLOR]:'#FFFF00' };
  var pgfs  = doc.getParagraphs();

  for (var word of words) for (var pgf of pgfs) {
    var location = pgf.findText(word);
    if (!location) continue;
    var start = location.getStartOffset();
    var end = location.getEndOffsetInclusive();
    location.getElement().setAttributes(start, end, style);
  }
}
  • Related