使用Apps脚本以编程方式更改Google Docs的默认段落标题 [英] Change default paragraph headings programmatically for Google Docs with Apps Script
问题描述
我已经搜索该功能已有一段时间了. 基本上,我想更改标题",副标题",标题1"等的默认段落样式.
I've been searching for this feature for quite awhile now. Basically, I'd like to change the default paragraph styles for Title, Subtitle, Heading 1 and so on.
我知道可以通过Google文档界面( https://support .google.com/docs/answer/116338?hl = zh_CN ),但据我所知,不是使用Apps Script编程编写的.
I know it is possible with the Google Docs interface (https://support.google.com/docs/answer/116338?hl=en), but as far as I know not programatically with Apps Script.
有人找到了解决方案吗?如果不是,我们是否可以将其作为功能请求提交给Google Apps脚本小组?出于商标目的,这将是对现有的Apps Script Docs API的很好补充.
Did anyone find a solution for this yet? And if not, can we submit it as a feature request for the Google Apps script team? It would be a great addition to the already existing Apps Script Docs API for branding purposes.
推荐答案
Now this is possible with the setHeadingAttributes method. For example, here I redefine the styles of Heading levels 1 and 2.
myHeading1 = {};
myHeading1[DocumentApp.Attribute.FONT_SIZE] = 24;
myHeading1[DocumentApp.Attribute.FONT_FAMILY] = "Georgia";
myHeading2 = {};
myHeading2[DocumentApp.Attribute.FONT_SIZE] = 16;
myHeading2[DocumentApp.Attribute.FONT_FAMILY] = "Verdana";
myHeading2[DocumentApp.Attribute.FOREGROUND_COLOR] = "#555555";
var body = DocumentApp.getActiveDocument().getBody();
body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, myHeading1);
body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING2, myHeading2);
似乎无法将属性重置为默认值,因此,如果您认为需要重置,请使用
It doesn't seem possible to reset the attributes back to their default, so if you think that a reset will be needed, get the original attributes with getHeadingAttributes and store them in document properties.
请注意,更改标题属性不会立即影响现有的标题:除非有人触摸其标题级别(即从标题级别下拉列表中选择某项,甚至与当前级别相同),否则它们将保持其当前样式. .要将更改追溯应用到现有段落,请参见此答案.
Note that changing heading attributes does not immediately affect already existing headings: they stay with their current style unless someone touches their heading level (i.e., selects something from the heading level drop-down, even the same level as the current one). To apply the changes retroactively to existing paragraphs, see this answer.
这篇关于使用Apps脚本以编程方式更改Google Docs的默认段落标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!