如何使用脚本在Google文档中设置文本格式 [英] How to format text in Google Docs using script
本文介绍了如何使用脚本在Google文档中设置文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个脚本来查找所有特定单词并设置其颜色格式.
I would to like to write a script to find all specific words and format their color.
我尝试了以下操作,但似乎不起作用:
I tryied the following, but it seems it is not working:
function ChangeColor() {
var body = DocumentApp.getActiveDocument().getBody();
var myword = body.findText("var");
while (myword !== null){
var mysearch = myword.getElement().asText().setForegroundColor('#DC64DC');
}
}
请问有人可以帮我吗?
推荐答案
您想要更改文档中"var"的字体颜色.如果我的理解是正确的,那么该修改如何?
You want to change the font color of "var" in the document. If my understanding is correct, how about this modification?
- 为了搜索下一个单词,它使用
findText(searchPattern, from)
. - 为了更改"var"的颜色,它使用
setForegroundColor(startOffset, endOffsetInclusive, color)
.- 在脚本中,开始和结束的偏移量可以分别通过
myword.getStartOffset()
和myword.getEndOffsetInclusive()
检索.
- In order to search next word, it uses
findText(searchPattern, from)
. - In order to change the color of "var", it uses
setForegroundColor(startOffset, endOffsetInclusive, color)
.- In your script, the offset of start and end can be retrieved by
myword.getStartOffset()
andmyword.getEndOffsetInclusive()
, respectively.
function ChangeColor() { var searchValue = "var"; var body = DocumentApp.getActiveDocument().getBody(); var myword = body.findText(searchValue); while (myword) { var mysearch = myword.getElement().asText() mysearch.setForegroundColor( myword.getStartOffset(), // startOffset myword.getEndOffsetInclusive(), // endOffsetInclusive '#DC64DC' // color ); var myword = body.findText(searchValue, myword); // Search next word } }
参考文献:
- findText(searchPattern,来自)
- setForegroundColor(startOffset,endOffsetInclusive,color)
- findText(searchPattern, from)
- setForegroundColor(startOffset, endOffsetInclusive, color)
References :
如果我误解了你的问题,对不起.
If I misunderstand your question, I'm sorry.
这篇关于如何使用脚本在Google文档中设置文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In your script, the offset of start and end can be retrieved by
- 在脚本中,开始和结束的偏移量可以分别通过
查看全文