我正在尝试将具有特定颜色的文本的背景色设置为Google Doc文件中的另一种颜色。
本质上,我想做的就是解析它,当我发现具有“ x”背景色的文本时,我想通过Apps Script将其更改为“ y”背景色。
这是我一直在使用的代码
function onOpen() {
DocumentApp.getUi()
.createMenu('Utilities')
.addItem('Auto-Replace', 'replaceSuits')
.addToUi();
};
function replaceSuits() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.editAsText();
var found = text.getBackgroundColor() === '#ff8c82';
var apple = body.findText(found);
while (apple) {
var elem = apple.getElement();
if (apple.isPartial()) {
var start = apple.getStartOffset();
var end = apple.getEndOffsetInclusive();
elem.setBackgroundColor(start, end, "#000000");
}
else {
elem.setBackgroundColor("#000000");
}
apple = body.findText(found, found);
}
};
我知道我的变量是重复的并且有点荒谬,但这是由于我一直在尝试各种测试以弄清楚为什么这段代码无法正常工作的缘故。提前致歉。
关于如何有效进行此操作的任何想法?
#ff8c82
到#000000
在谷歌文档。如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。
body.editAsText()
。function replaceSuits() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.editAsText();
// I modified below script.
for (var i = 0; i < text.getText().length; i++)
if (text.getBackgroundColor(i) == "#ff8c82") text.setBackgroundColor(i, i, "#000000");
}
如果我误解了您的问题,而这不是您想要的方向,我深表歉意。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句