如何使用谷歌脚本替换字体重量?

时间:2017-01-18 13:56:13

标签: google-apps-script google-docs

您好我尝试在我的Google文档中检测特定字符串并将其设置为" bold"。 我试过这个:

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:在Text对象中找不到Fonction setFontWeight。

你能帮帮我吗?感谢。

编辑>

我已尝试过使用此功能,但大胆的样式现在应用于整个段落,而不仅仅是文本字符串......

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);
}
}

2 个答案:

答案 0 :(得分:1)

看起来这是通过工作表支持 - 而不是文档

在文档中你可以尝试更改字体大小吗?

 // Change the weight
   foundText.setFontSize(99);

答案 1 :(得分:1)

好的,我这样做了:

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);
   }
}