2017-10-04 77 views
0

我希望重新定義文檔的標題樣式。我的理解是,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)。如果不是這些方法,我想找到爲文檔重新定義標題樣式的正確方法。你能爲我指出正確的方向嗎?

非常感謝,

回答

0

沒打過多少左右這還,但檢查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