您好,我尝试检测我的 google 文档中的特定字符串并将其设置为“粗体”。 我试过这个:
function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("Câbles");
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Change the weight
foundText.setFontWeight("bold");
// Find the next match
foundElement = body.findText("Câbles", foundElement);
}
}
脚本返回一个错误:TypeError: Fonction setFontWeight not found in Text object。
你能帮帮我吗?谢谢。
编辑>
我试过使用它,但现在不仅是文本字符串还应用了粗体样式到整个段落...
function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("test");
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Set Bold
foundText.setBold(true);
// Find the next match
foundElement = body.findText("test", foundElement);
}
}
最佳答案
好的,我这样做了:
function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("test");
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
// Set Bold
foundText.setBold(start, end, true);
// Find the next match
foundElement = body.findText("test", foundElement);
}
}
关于google-apps-script - 如何使用谷歌脚本替换字体粗细?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41721333/