2012-07-13 16 views
6

हाइलाइट करना मैं Google दस्तावेज़ में किसी शब्द के सभी उदाहरण ढूंढना चाहता हूं और उन्हें हाइलाइट करना चाहता हूं (या टिप्पणी - जो कुछ भी खड़ा है)। मैंने निम्नलिखित फ़ंक्शन बनाया है, लेकिन इसे केवल शब्द की पहली उपस्थिति ("इस मामले में") मिलती है। शब्द के सभी उदाहरणों को कैसे ढूंढें इस पर कोई विचार सराहना की जाएगी!टेक्स्ट ढूंढना (एकाधिक बार) और

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

ठीक है, सरल जावास्क्रिप्ट पर्याप्त है,

var search = searchtext; 
var index = -1; 

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

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

आशा इस मदद करता है!

+0

आपकी मदद के लिए धन्यवाद, बालाजिबॉस। दुर्भाग्य से यह अनुक्रमणिका = text.indexOf (खोज, अनुक्रमणिका + 1) पर त्रुटियां; त्रुटि के साथ: ऑब्जेक्ट टेक्स्ट में फ़ंक्शन इंडेक्स नहीं मिल रहा है। – user1523207

+0

अनुक्रमणिका स्ट्रिंग पर काम करता है। आप स्ट्रिंग के रूप में दस्तावेज़ के पाठ को प्राप्त करने के लिए 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 में से एक से संशोधित की गई थी, और यूआई (बिना पैरामीटर के) या एक स्क्रिप्ट से कहा जा सकता है।

/** 
* 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 स्क्रिप्ट में टेक्स्ट में प्रभाव कैसे जोड़ता हूं। नीचे दिया गया उदाहरण विशेष रूप से किसी दस्तावेज़ में किसी विशेष स्ट्रिंग की सभी घटनाओं को हाइलाइट करने के लिए है।

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); 
    } 
} 
संबंधित मुद्दे