2012-07-13 16 views
6

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"); 
}; 

答えて

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 **/ 
} 

・ホープ、このことができます!それは、このように終えることができました、あなたのコードを連鎖

ので、[OK]を
+0

あなたのおかげで、balajiboss。残念ながら、それはエラー の の上でエラーです。index = text.indexOf(search、index + 1); エラー:オブジェクトtextに関数indexOfが見つかりません。 – user1523207

+0

indexOfは文字列で機能します。 getText()メソッドを使用して、文書のテキストを文字列として取得できます。 – balajiboss

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を使用する必要がありますか?

1

ドキュメントバインドスクリプトの導入により、カスタムメニューから呼び出されるテキストハイライト機能を作成できるようになりました。

このスクリプトはthis answerのものから変更されており、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(); 
} 
+0

非常によく似た質問に対する重複した回答。 [こちら](http://stackoverflow.com/questions/12064972/can-i-color-certain-words-in-google-document-using-google-script/16924466#16924466)をご覧ください。 – Mogsdad

8

これは古風なことですが、ここでは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); 
    } 
} 
関連する問題