google-apps-script - Apps 脚本中的 Google Docs API
问题描述
我正在尝试将 Apps Script 与 Google Docs API 一起使用,以便在个人创建的子菜单中进行设置以更改文本颜色,具体取决于子菜单中选择的选项。有人可以给我一个大致的想法,了解如何获取特定段落的当前颜色并将其设置为不同的颜色。
解决方案
要获取段落的当前前景色,请使用Method:Paragraph.getAttribute和 Method:Paragraph.setAttribute来设置前景色。
例子:
代码:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.getParagraphs().forEach(par => {
var style = {};
if(par.getAttributes().FOREGROUND_COLOR == '#ff0000'){
style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000ff';
}else if(par.getAttributes().FOREGROUND_COLOR == '#ff9900'){
style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#008000';
}
par.setAttributes(style);
})
}
输出:
以下是您可以使用的属性列表。