दस्तावेज़-बाध्य स्क्रिप्ट के परिचय के साथ, अब एक कस्टम मेनू से एक टेक्स्ट हाइलाइटिंग फ़ंक्शन बनाना संभव है।
यह स्क्रिप्ट this answer में से एक से संशोधित की गई थी, और यूआई (बिना पैरामीटर के) या एक स्क्रिप्ट से कहा जा सकता है।
/**
* 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();
}
आपकी मदद के लिए धन्यवाद, बालाजिबॉस। दुर्भाग्य से यह अनुक्रमणिका = text.indexOf (खोज, अनुक्रमणिका + 1) पर त्रुटियां; त्रुटि के साथ: ऑब्जेक्ट टेक्स्ट में फ़ंक्शन इंडेक्स नहीं मिल रहा है। – user1523207
अनुक्रमणिका स्ट्रिंग पर काम करता है। आप स्ट्रिंग के रूप में दस्तावेज़ के पाठ को प्राप्त करने के लिए getText() विधि का उपयोग कर सकते हैं। – balajiboss