Search for text (several times) and highlight

I would like to find all instances of a word in a Google document and highlight them (or a comment - everything that stands out). I created the following function, but it only finds the first occurrence of the word ("in this case"). Any ideas on how to find all instances of this 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"); }; 
+6
source share
4 answers

I know this is old, but here's how I add effects to text in Google Script. The following example is specifically designed to add highlighting to all occurrences of a specific line in a document.

 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); } } 
+8
source

So, the chain of your codes can be completed as follows:

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

I still have doubts. This code works well, but why should I use search.length-1?

+1
source

With the introduction of scripts associated with documents, you can now make a function for selecting text, which is called from the user menu.

This script has been modified with this answer and can be called from the user interface (without parameters) or script.

 /** * 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(); } 
+1
source

Well, simple javascript is enough,

 var search = searchtext; var index = -1; while(true) { index = text.indexOf(search,index+1); if(index == -1) break; else /** do the required operation **/ } 

Hope this helps!

0
source

Source: https://habr.com/ru/post/920460/


All Articles