2012-07-13 35 views
6

Tôi muốn tìm tất cả các trường hợp của một từ trong tài liệu Google và đánh dấu chúng (hoặc nhận xét - bất kỳ thứ gì để nó nổi bật). Tôi đã tạo ra các chức năng sau đây, nhưng nó chỉ tìm thấy sự xuất hiện đầu tiên của từ ("the" trong trường hợp này). Bất kỳ ý tưởng về cách tìm tất cả các trường hợp của từ sẽ được đánh giá cao!Tìm văn bản (nhiều lần) và tô sáng

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

Trả lời

0

Vâng, javascript đơn giản là đủ,

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

Cảm ơn sự giúp đỡ của bạn, balajiboss. Rất tiếc, lỗi trên index = text.indexOf (tìm kiếm, chỉ mục + 1); có lỗi: Không thể tìm thấy hàm indexOf trong đối tượng Văn bản. – user1523207

+0

indexOf hoạt động trên chuỗi. Bạn có thể sử dụng phương thức getText() để lấy văn bản của tài liệu dưới dạng một chuỗi. – balajiboss

1

Ok vậy, dí mã của bạn nó có thể kết thúc như thế này:

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

}; 

tôi vẫn có một nghi ngờ. Mã này hoạt động tốt, nhưng tại sao tôi phải sử dụng search.length-1?

1

Với việc giới thiệu các tập lệnh bị ràng buộc bởi tài liệu, giờ đây bạn có thể tạo hàm đánh dấu văn bản được gọi từ trình đơn tùy chỉnh.

Tập lệnh này đã được sửa đổi từ một trong số this answer và có thể được gọi từ giao diện người dùng (không có tham số) hoặc tập lệnh.

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

Câu trả lời trùng lặp cho một câu hỏi rất giống nhau. Xem [tại đây] (http://stackoverflow.com/questions/12064972/can-i-color-certain-words-in-google-document-using-google-script/16924466#16924466). – Mogsdad

8

Tôi biết đây là một oldie, nhưng dưới đây là cách tôi thêm hiệu ứng vào văn bản trong Google Script. Ví dụ bên dưới là đặc biệt để thêm làm nổi bật cho tất cả các lần xuất hiện của một chuỗi cụ thể trong tài liệu.

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); 
    } 
} 
Các vấn đề liên quan