I’m working on a Google Docs project and need help with changing paragraph styles using Apps Script. My document has several heading levels and I want to adjust them.
Here’s what I’m trying to do:
- Change Header 1 to 14pt, blue, and underlined
- Make Header 2 into 11pt normal text
- Keep normal text at 10pt, but add “Topic X:” in bold at the start
I’m not sure how to approach this with Apps Script. Can anyone give me some pointers on how to:
- Identify different heading levels
- Modify text styles (size, color, underline)
- Add bold text to the beginning of paragraphs
Has anyone done something similar before? Any help or code examples would be great. Thanks!
I’ve worked on similar projects before, and here’s what I found effective:
For identifying heading levels, use getParagraphs() and getHeading() methods. Then, you can modify styles with setFontSize(), setForegroundColor(), and setUnderline().
To change Header 2 to normal text, use setHeading(DocumentApp.ParagraphHeading.NORMAL).
For adding ‘Topic X:’ to normal paragraphs, use insertText() at the beginning, then setBold() for that portion.
Here’s a condensed example:
function modifyStyles() {
var body = DocumentApp.getActiveDocument().getBody();
var paras = body.getParagraphs();
for (var i = 0; i < paras.length; i++) {
var p = paras[i];
var heading = p.getHeading();
if (heading === DocumentApp.ParagraphHeading.HEADING1) {
p.setFontSize(14).setForegroundColor('#0000FF').setUnderline(true);
} else if (heading === DocumentApp.ParagraphHeading.HEADING2) {
p.setFontSize(11).setHeading(DocumentApp.ParagraphHeading.NORMAL);
} else if (heading === DocumentApp.ParagraphHeading.NORMAL) {
p.insertText(0, 'Topic X: ').editAsText().setBold(0, 8);
p.setFontSize(10);
}
}
}
This should get you started. Let me know if you need any clarification.
hey zack, i’ve done smthing similar before. here’s a quick tip:\n\nuse getparagraphs() to grab all paragraphs, then loop thru them. check heading type with getheading(). for h1, use setfontsize(14), setforegroundcolor(‘#0000FF’), and setunderline(true).\n\nfor h2, just use setheading(DocumentApp.ParagraphHeading.NORMAL) and setfontsize(11).\n\nhope this helps!
I’ve tackled similar projects before, and here’s what worked well for me:
For identifying heading levels, you can use the getParagraphs() method to loop through all paragraphs, then check each one’s heading type with getHeading().
To modify styles, setFontSize(), setForegroundColor(), and setUnderline() are your go-to methods. For Header 2, you’ll want to use setHeading(DocumentApp.ParagraphHeading.NORMAL) to change it to normal text, then adjust the font size.
Adding ‘Topic X:’ to normal paragraphs is a bit trickier. You’ll need to use insertText() at the start of each paragraph, then apply bold formatting to just that part.
Here’s a rough outline of how your script might look:
function modifyStyles() {
var body = DocumentApp.getActiveDocument().getBody();
var paras = body.getParagraphs();
for (var i = 0; i < paras.length; i++) {
var p = paras[i];
var heading = p.getHeading();
if (heading === DocumentApp.ParagraphHeading.HEADING1) {
// Modify Header 1
} else if (heading === DocumentApp.ParagraphHeading.HEADING2) {
// Modify Header 2
} else if (heading === DocumentApp.ParagraphHeading.NORMAL) {
// Add 'Topic X:' and adjust normal text
}
}
}
Hope this helps get you started! Let me know if you need any clarification.