Reputation: 3
I'm trying to change the Headings on Google docs using the setHeadingAttributes() function.
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));
}
The problem is that only setting the NORMAL heading works properly. In fact, the NORMAL heading seems to completely override both TITLE and HEADING1, such that they all are now in PT Serif font. (Changing it to another font rather than PT Serif changes everything accordingly).
Checking the logs shows that everything seems to be fine, but the actual document that contains this script shows otherwise. I've tried commenting out the part of setting the NORMAL and it shows that setting the TITLE or HEADING1 actually does nothing except in the logs. I've made new documents, even used other Google accounts, but they all seem to have this problem.
Copying and pasting from the example given here, which supposedly works, only confirms my problem, which seems to be the same as in this question without an adequate answer. I can't seem to find the problem in my code. Many thanks for your help.
Upvotes: 0
Views: 233
Reputation: 26806
https://issuetracker.google.com/36763968
You can give it a start to increase visibility.
In the meantime, you can use a workaround manually looping through all paragraphs and applying the different attributes to different heading, e.g. as done here.
Upvotes: 2