为 Google 文档中的当前选择设置字体的 Apps 脚本

Apps Script to set font for current selection in Google doc

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

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

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

当我通过给出“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 文档的脚本编辑器,并在正文和 运行 脚本中选择一个文本。

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