Title: setHeadingAttributes to redefine a heading style in Documents w/ GAS
Tags: google-apps-script;google-docs-api
Question: I am hoping to re-define the heading styles for a document.   My understanding is that getHeadingAttributes and setHeadingAttributes would let me do this
https://developers.google.com/apps-script/reference/document/body#setheadingattributesparagraphheading-attributes
This is how I'm going about testing this --
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));
This Body has its headings attributes modified, as revealed by the Logger output:
[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
But in my document to which this script is bound, the styling of Heading 1 has not changed for existing paragraphs with that Heading.  Nor are new paragraphs marked Heading 1 styled with the changes I've tried to make.
Incidentally, when I run that same function on NORMAL instead of HEADING1, the changes are instantly visible: Every Heading (Normal, Title, Subtitle, Heading 1, etc...) has the underline and background color applied.
I feel like I've fundamentally misunderstood the purpose of these methods (getHeadingAttributes, setHeadingAttributes).  If not these methods, I'd like to find the correct way to re-define Heading stylings for a document.  Can you point me in the right direction?
Many Thanks,


Here is another answer: Haven't played around much of this yet, but check the Enum ParagraphHeading as it seems close to what you're getting at.
Use the ParagraphHeading enumeration to configure the heading style for 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);
Comment for this answer: The setHeading method styles a paragraph with whatever the current definition of ParagraphHeading.NORMAL is (or HEADING1, or whatever).  That's not what I'm asking how to do.  I want to change the definition of what NORMAL (or HEADING1 or whatever) is.  This functionality is captured in the standard UI with  the menu choices Format / Paragraph Styles / Normal / Update Normal Text to match...

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:10/04/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<google-apps-script><google-docs-api>', 'Title': 'setHeadingAttributes to redefine a heading style in Documents w/ GAS', 'ViewCount': 756, 'id': 46568893, 'user_AboutMe': None, 'user_AccountId': 11917539.0, 'user_DisplayName': 'Gene', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 8721079.0}}