7

我想在谷歌文档中找到一个单词的所有实例并突出显示它们(或评论——任何让它突出的东西)。我创建了以下函数,但它只找到单词的第一次出现(在本例中为“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");
};
4

4 回答 4

12

我知道这是一个老歌,但这是我在 Google Script 中为文本添加效果的方法。下面的示例专门用于为文档中特定字符串的所有出现添加突出显示。

function highlightText(findMe) {
    var body = DocumentApp.getActiveDocument().getBody();
    var foundElement = body.findText(findMe);

    while (foundElement != null) {
        // Get the text object from the element
        var foundText = foundElement.getElement().asText();

        // Where in the Element is the found text?
        var start = foundElement.getStartOffset();
        var end = foundElement.getEndOffsetInclusive();

        // Change the background color to yellow
        foundText.setBackgroundColor(start, end, "#FCFC00");

        // Find the next match
        foundElement = body.findText(findMe, foundElement);
    }
}
于 2014-10-17T07:05:52.117 回答
1

好的,链接你的代码可以像这样完成:

function findWordsAndHighlight() {
var doc = DocumentApp.openById("DocID");
var text = doc.editAsText();
var search = "searchTerm";
var index = -1;
var color ="#2577ba";
var textLength = search.length-1;

while(true)
 {
   index = text.getText().indexOf(search,index+1);
   if(index == -1)
     break;
   else text.setForegroundColor(index, index+textLength,color );
 }

};

我还有一个疑问。这段代码很好用,但为什么我必须使用 search.length-1?

于 2012-12-20T01:19:48.430 回答
1

随着文档绑定脚本的引入,现在可以制作从自定义菜单调用的文本突出显示功能。

此脚本是根据此答案中的脚本修改的,可以从 UI(不带参数)或脚本调用。

/**
 * Find all matches of target text in current document, and highlight them.
 *
 * @param {String} target     (Optional) The text or regex to search for. 
 *                            See Body.findText() for details.
 * @param {String} background (Optional) The desired highlight color.
 *                            A default orange is provided.
 */
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';  // default color is light orangish.
  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);
  }
}

/**
 * Create custom menu when document is opened.
 */
function onOpen() {
  DocumentApp.getUi().createMenu('Custom')
      .addItem('Text Highlighter', 'highlightText')

      .addToUi();
}
于 2013-06-04T18:12:07.243 回答
0

好吧,简单的javascript就足够了,

var search = searchtext;
var index = -1;

while(true)
 {
   index = text.indexOf(search,index+1);

   if(index == -1)
     break;
   else
     /** do the required operation **/
 }

希望这可以帮助!

于 2012-07-14T06:40:13.273 回答