我想在Google文档中找到一个单词的所有实例并突出显示它们(或评论 - 任何事情都如此突出).我创建了以下函数,但它只找到了单词的第一个外观(在本例中为"the").任何关于如何找到该单词的所有实例的想法将不胜感激!
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");
};
Run Code Online (Sandbox Code Playgroud)