Google脚本:如何突出显示一组单词? [英] Google Script: How to highlight a group of words?
本文介绍了Google脚本:如何突出显示一组单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个词我可以用这样的脚本:
p>
function myFunction(){var doc = DocumentApp.openById('ID'); var textToHighlight =TESTvar highlightStyle = {}; highlightStyle [DocumentApp.Attribute.FOREGROUND_COLOR] ='#FF0000'; var paras = doc.getParagraphs(); var textLocation = {}; var i; for(i = 0; i< paras.length; ++ i){textLocation = para [i] .findText(textToHighlight); (textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(),highlightStyle);如果(textLocation! }}}
但我需要在文本中搜索一组更多的单词和突出显示它们。 (这是列表: https://conterest.de/fuellwoerter-liste-worte/ )
如何为更多单词编写脚本?
这似乎有点太复杂:
function myFunction(){var doc = DocumentApp.openById('ID'); var textToHighlight =TESTvar textToHighlight1 =TEST1var highlightStyle = {}; highlightStyle [DocumentApp.Attribute.FOREGROUND_COLOR] ='#FF0000'; var paras = doc.getParagraphs(); var textLocation = {}; var i; for(i = 0; i< paras.length; ++ i){textLocation = para [i] .findText(textToHighlight); (textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(),highlightStyle);如果(textLocation! }} for(i = 0; i< paras.length; ++ i){textLocation = para [i] .findText(textToHighlight1); (textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(),highlightStyle);如果(textLocation!
感谢您的帮助!
$ / $> $ / $> b $ b 解决方案您可以使用嵌套的for循环:
var words = ['TEST','TEST1'];
//对于单词中的每个单词:
for(w = 0; w
//获取当前字:
var textToHighlight = words [w];
//这是你的代码:
for(i = 0; i< paras.length; ++ i){
textLocation = paras [ I] .findText(textToHighlight); (textLocation!= null&& textLocation.getStartOffset()!= -1){
textLocation.getElement()。setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(),highlightStyle );
code
$ b 这样你就可以很容易地扩展数组 words
带有更多单词。
I'd like to write a script for google docs to automatically highlight a set of words.
For one word I could use a script like this:
function myFunction() {
var doc = DocumentApp.openById('ID');
var textToHighlight = "TEST"
var highlightStyle = {};
highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
var paras = doc.getParagraphs();
var textLocation = {};
var i;
for (i=0; i<paras.length; ++i) {
textLocation = paras[i].findText(textToHighlight);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
}
}
}
But I need to search the text for a set of many more words and highlight them. (this is the list: https://conterest.de/fuellwoerter-liste-worte/)
How do I write a script for more words?
This seems to be a little bit too complicated:
function myFunction() {
var doc = DocumentApp.openById('ID');
var textToHighlight = "TEST"
var textToHighlight1 = "TEST1"
var highlightStyle = {};
highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
var paras = doc.getParagraphs();
var textLocation = {};
var i;
for (i=0; i<paras.length; ++i) {
textLocation = paras[i].findText(textToHighlight);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
}
}
for (i=0; i<paras.length; ++i) {
textLocation = paras[i].findText(textToHighlight1);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
}
}
}
Thanks for your help!
解决方案 You could use a nested for loop:
var words = ['TEST', 'TEST1'];
// For every word in words:
for (w = 0; w < words.length; ++w) {
// Get the current word:
var textToHighlight = words[w];
// Here is your code again:
for (i = 0; i < paras.length; ++i) {
textLocation = paras[i].findText(textToHighlight);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(), textLocation.getEndOffsetInclusive(), highlightStyle);
}
}
}
This way you can easily extend the array words
with more words.
这篇关于Google脚本:如何突出显示一组单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文