My Question: How do you set text to be a title or subtitle in a Google Doc using Google Apps Script? (see a visual example below)
In Google Apps Script documentation on formatting paragraphs they show the different types of ParagraphHeadings in the ParagraphHeading class and then there’s the Attribute class which can be assigned to a paragraph which has some styling attributes. However, neither of these has a way to set any text as a title or a subtitle in the document. How is that accomplished?
I’m looking for something like this:
var doc = DocumentApp.getActiveDocument();
// Append a paragraph, with title.
var par1 = doc.appendParagraph("Title");
par1.setHeading(DocumentApp.ParagraphHeading.TITLE);
// Append a paragraph, with subtitle.
var par2 = doc.appendParagraph("SubTitle");
par2.setHeading(DocumentApp.ParagraphHeading.SUBTITLE);
// Append a paragraph, with heading 1.
var par3 = doc.appendParagraph("Heading 1");
par3.setHeading(DocumentApp.ParagraphHeading.HEADING1);
Here’s what I mean:

The example script you get if you choose “Create script for… Document” doesn’t use
setHeading()at all, instead it explicitly manipulates the WYSIWYG attributes. (I hate when people do that in documents, and I’m only slightly less annoyed by it in script, but that’s a topic for another day!)Example, lifted directly from that sample script, including bad grammar:
That’s just a work-around – if you haven’t done so already, you should create an issue report against apps-script to ensure all “Paragraph Types” are available to scripts.