setHeadingAttributes重新定义文档中的标题样式w/GAS

问题描述:

我希望重新定义文档的标题样式。我的理解是,getHeadingAttributes和setHeadingAttributes会让我做这个setHeadingAttributes重新定义文档中的标题样式w/GAS

https://developers.google.com/apps-script/reference/document/body#setheadingattributesparagraphheading-attributes

这是我该怎么去有关测试这一点 -

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

    styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1); 

    Logger.log("Before: \n" + JSON.stringify(styledef)); 

    styledef[DocumentApp.Attribute.UNDERLINE] = true; 
    styledef[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffff00'; 

    body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, styledef); 

    styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1); 

    Logger.log("After: \n" + JSON.stringify(styledef)); 

} 

这身体有它的标题属性修改,如发现由Logger输出:

[17-10-04 09:20:53:379 MDT] Before: 
    {"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":false,"BACKGROUND_COLOR":null,"INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}} 

    [17-10-04 09:20:53:382 MDT] After: 
    {"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":true,"BACKGROUND_COLOR":"#ffff00","INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}} 

UNDERLINE changed from false to true 

BACKGROUND_COLOR changed from null to #ffff00 

但是在我的文档中绑定了这个脚本,标题1的样式对于具有该标题的现有段落没有改变。标题1的新段落也没有标明我试图做出的改变。顺便说一句,当我在NORMAL而不是HEADING1上运行相同的功能时,所做的更改即时可见:每个标题(标题,标题,副标题,标题1等)都应用了下划线和背景颜色。

我觉得我从根本上误解了这些方法的目的(getHeadingAttributes,setHeadingAttributes)。如果不是这些方法,我想找到为文档重新定义标题样式的正确方法。你能为我指出正确的方向吗?

非常感谢,

没打过多少左右这还,但检查Enum ParagraphHeading,因为它似乎接近你在说什么。

使用ParagraphHeading枚举为ParagraphElement配置标题样式。

var body = DocumentApp.getActiveDocument().getBody(); 

// Append a paragraph, with heading 1. 
var par1 = body.appendParagraph("Title"); 
par1.setHeading(DocumentApp.ParagraphHeading.HEADING1); 

// Append a paragraph, with heading 2. 
var par2 = body.appendParagraph("SubTitle"); 
par2.setHeading(DocumentApp.ParagraphHeading.HEADING2); 

// Append a paragraph, with normal heading. 
var par3 = body.appendParagraph("Text"); 
par3.setHeading(DocumentApp.ParagraphHeading.NORMAL); 
+0

该方法的setHeading样式的段落与任何ParagraphHeading.NORMAL的当前定义为(或标题1,或其他)。这不是我要求怎么做的。我想改变NORMAL(或HEADING1或其他)的定义。此功能在标准用户界面中捕获,菜单选择格式/段落样式/普通/更新普通文本以匹配... – Gene