首页 > 解决方案 > Google 文档 setHeadingAttributes() 无法正常工作

问题描述

我正在尝试使用 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 帐户,但它们似乎都有这个问题。

从这里给出的示例中复制和粘贴,据说可以工作,只能确认我的问题,这似乎与这个问题相同,没有足够的答案。我似乎无法在我的代码中找到问题。非常感谢您的帮助。

标签: google-apps-scriptgoogle-docs

解决方案


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

https://issuetracker.google.com/36763968

您可以开始提高知名度。

同时,您可以使用一种解决方法手动循环遍历所有段落并将不同的属性应用于不同的标题,例如在此处完成。


推荐阅读