Web applications 在谷歌文档中查找文本并使用谷歌应用程序脚本突出显示

Web applications 在谷歌文档中查找文本并使用谷歌应用程序脚本突出显示,web-applications,replace,google-apps,Web Applications,Replace,Google Apps,我想找到一个文本(甚至是该文本的所有实例),并使用脚本突出显示它。我创建了以下函数,但每当我运行该函数时 很抱歉,发生了服务器错误。请稍等,然后重试。 有没有办法解决这个问题?非常感谢,谢谢 function onOpen() { var ui = DocumentApp.getUi(); ui.createMenu('Custom Menu') .addItem('Search And Navigate', 'highlightText').addToUi(); } function h

我想找到一个文本(甚至是该文本的所有实例),并使用脚本突出显示它。我创建了以下函数,但每当我运行该函数时 很抱歉,发生了服务器错误。请稍等,然后重试。

有没有办法解决这个问题?非常感谢,谢谢

function onOpen() {
var ui = DocumentApp.getUi();

ui.createMenu('Custom Menu')
  .addItem('Search And Navigate', 'highlightText').addToUi();
}
function highlightText(target,background) {
// If no search parameter was provided, ask for one
if (arguments.length == 0) {
var ui = DocumentApp.getUi();
var result = ui.prompt('Text Highlighter','Enter text to highlight:',   ui.ButtonSet.OK_CANCEL);

// Exit if user hit Cancel.
if (result.getSelectedButton() !== ui.Button.OK) return;
// else

target = result.getResponseText();
}

var background = background || '#F3E2A9';
var doc = DocumentApp.getActiveDocument();
var bodyElement = DocumentApp.getActiveDocument().getBody();
var searchResult = bodyElement.findText(target);

while (searchResult !== null) {
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText();

//Logger.log(url);
thisElementText.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),background);

// search for next match
searchResult = bodyElement.findText(target, searchResult);
}
}