我正在尝试使用Google Docs API的应用程序脚本在个人创建的子菜单中有一个设置来更改文本颜色,这取决于在子菜单中选择了哪个选项。谁能给我一个大概的想法,我如何获得一个特定段落的当前颜色并将其设置为不同的颜色。
发布于 2021-01-21 04:13:30
若要获取段落的当前前景色,请使用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);
})
}
输出:
以下是您可以使用的Attributes列表。
https://stackoverflow.com/questions/65816346
复制相似问题