2014-02-26 51 views
0

我想知道是否有方法突出顯示特定單詞或文本正文並通過某些Google API插入評論。我意識到驅動器API有一個插入註釋方法,但是,生成的註釋是針對整個文檔,而不是針對文本的特定部分。突出顯示文本並將註釋插入特定的Google文檔文本

非常感謝。

+0

你知道你在找什麼文字..? – Drewness

+0

我正在尋找在文檔中說出位置的名稱,並突出顯示該單詞並創建評論。 – dacardzmasta

回答

0

到目前爲止,我已經能夠匹配並突出顯示文檔(搜索目標字符串)中文本「地球」的所有實例,但我仍在努力添加註釋。下面的代碼做到這一點:

function myFunction() { 
    var fileId = 'Id of your Document goes here'; 
    var background = background || '#FFFF00'; // highlight yellow. 
    var target = 'Earth'; 
    var doc = DocumentApp.openById(fileId); 
    var bodyElement = doc.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); 
    } 
} 

適於/從這裏複製:Can I color certain words in Google Document using Google Apps Script?(技術上我沒有太多適應,因爲它是晚了,我很懶)。仍在努力添加評論。由於這個關鍵是隔離與目標相匹配的「元素」,我懷疑只是找到一種方法來爲元素添加註釋並將其標記到背景着色步驟的末尾即可。