【问题标题】:Google docs setHeadingAttributes() not working properlyGoogle 文档 setHeadingAttributes() 无法正常工作
【发布时间】:2019-12-24 23:50:16
【问题描述】:

我正在尝试使用 setHeadingAttributes() 函数更改 Google 文档上的标题。

  function setHeadings() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var myNormal_Style = {};
  myNormal_Style[DocumentApp.Attribute.FONT_FAMILY] = 'PT Serif';
  myNormal_Style[DocumentApp.Attribute.FONT_SIZE] = 12;
  body.setHeadingAttributes(DocumentApp.ParagraphHeading.NORMAL, myNormal_Style);

  var myTitle_Style = {};
  myTitle_Style[DocumentApp.Attribute.FONT_FAMILY] = 'Montserrat';
  myTitle_Style[DocumentApp.Attribute.FONT_SIZE] = 14;
  myTitle_Style[DocumentApp.Attribute.BOLD] = false;
  body.setHeadingAttributes(DocumentApp.ParagraphHeading.TITLE, myTitle_Style);

  var myHeading1_Style = {};
  myHeading1_Style[DocumentApp.Attribute.FONT_FAMILY] = 'Courier New';
  myHeading1_Style[DocumentApp.Attribute.FONT_SIZE] = 14;
  myHeading1_Style[DocumentApp.Attribute.BOLD] = true;
  body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, myHeading1_Style);



  Logger.log(body.getHeadingAttributes(DocumentApp.ParagraphHeading.TITLE));
  Logger.log(body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1));
  Logger.log(body.getHeadingAttributes(DocumentApp.ParagraphHeading.NORMAL));
}

问题是只有设置 NORMAL 标题才能正常工作。事实上,NORMAL 标题似乎完全覆盖了 TITLE 和 HEADING1,因此它们现在都是 PT Serif 字体。 (将其更改为另一种字体而不是 PT Serif 会相应地更改所有内容)。

检查日志显示一切似乎都很好,但包含此脚本的实际文档显示并非如此。我已经尝试注释掉设置 NORMAL 的部分,它表明设置 TITLE 或 HEADING1 实际上除了在日志中没有任何作用。我制作了新文档,甚至使用了其他 Google 帐户,但它们似乎都有这个问题。

从here 给出的示例中复制和粘贴,这应该有效,只能确认我的问题,这似乎与this question without an adequate answer 中的相同。我似乎无法在我的代码中找到问题。非常感谢您的帮助。

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    这是一个已经在Public Issue Tracker 提交的错误:

    https://issuetracker.google.com/36763968

    您可以开始提高知名度。

    与此同时,您可以使用一种解决方法手动循环遍历所有段落并将不同的属性应用于不同的标题,例如正如here所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 2017-09-25
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多