【发布时间】: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