首页 > 解决方案 > 用于在 Google doc 中为当前选择设置字体的 Apps 脚本

问题描述

我有一些 Google Apps 脚本代码来设置 Google 文档中某些文本的字体。

          text.setFontFamily(element.getStartOffset(), element.getEndOffsetInclusive(), "Roboto Mono");

但我想要 Roboto Mono Medium。我该怎么做呢?我不确定是什么样的实体媒体。我认为这是一个重量。但我没有看到任何记录的方式来设置权重。

标签: javascriptgoogle-apps-scripttextfontsgoogle-docs

解决方案


当我通过给出“Roboto Mono Medium”检查字体名称时,它是“Roboto Mono;500”。那么如何使用Roboto Mono;500如下的字体名称呢?

从:

text.setFontFamily(element.getStartOffset(), element.getEndOffsetInclusive(), "Roboto Mono");

到:

text.setFontFamily(element.getStartOffset(), element.getEndOffsetInclusive(), "Roboto Mono;500");

笔记:

  • 检查字体名称的简单示例脚本如下。当您使用它时,请将脚本复制并粘贴到 Google Document 的脚本编辑器中,并在正文中选择一个文本并运行该脚本。

      const selection = DocumentApp.getActiveDocument().getSelection();
      const text = selection.getSelectedElements()[0].getElement().asText();
      console.log(text.getFontFamily())  // Check the font name.
    

推荐阅读