I would like to find all the instances of a word in a Google doc and highlight them (or comment - anything so it stands out). I have created the following function, but it only finds the first appearance of the word ("the" in this case). Any ideas on how to find all instances of the word would be appreciated!
function findWordsAndHighlight() {
var doc = DocumentApp.openById(Id);
var text = doc.editAsText();
//find word "the"
var result = text.findText("the");
//change background color to yellow
result.getElement().asText().setBackgroundColor(result.getStartOffset(), result.getEndOffsetInclusive(), "#FFFF00");
};
I know this is an oldie, but here's how I add effects to text in Google Script. The example below is specifically for adding highlighting to all occurrences of a particular string in a document.
With the introduction of document-bound scripts, it's now possible to make a text highlighting function that's invoked from a custom menu.
This script was modified from the one in this answer, and may be called from the UI (with no parameters) or a script.
Ok so, chaining your codes it could finish like this :
I still have a doubt. This code works nice, but why I have to use search.length-1?
Well, simple javascript is enough,
Hope this helps!