google-apps-script - 如何使用谷歌脚本替换字体粗细?

标签 google-apps-script google-docs

您好,我尝试检测我的 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/

相关文章:

javascript - Twitter 库出错

emacs - 使用 Emacs 编辑 Google 文档?

google-docs - WOPI 相当于 Google 文档

google-apps-script - 如何在 Apps 脚本中获取 Google 文档中下拉菜单的值?

google-apps-script - 如何将页面(模式对话框)与其同级页面(侧边栏)进行通信?

google-apps-script - 谷歌幻灯片自动更新链接/从谷歌表链接的表格

javascript - 特定收件箱文件夹的 getInboxThreads()。 Google Apps 脚本 gmail

google-apps-script - 如何使库与调用者脚本 PropertiesService 一起工作?

javascript - 应用脚本 : creating 'dynamic' pages in web apps?

javascript - 为什么只打印一个属性