Hey everyone! I’m working on a Google Docs automation script and need help with paragraph formatting. The built-in styles like headings and titles are too limited for what I need.
Here are the formatting requirements I want to apply:
Indent paragraphs by 4 cm from the left margin
Set font to Arial with 10pt size
Use single line spacing throughout
Add 1.5 line spacing before citations
Make all text justified alignment
I found this working script online but I’m stuck on how to modify it for my needs. I’m pretty new to Google Apps Script so any help would be great!
Current script I’m working with:
// Triggered when document opens
// Creates custom menu for applying text formatting
function onOpen() {
DocumentApp.getUi()
.createMenu('Custom Format')
.addItem('Apply citation format', 'applyCitationFormat')
.addToUi();
}
// Style configuration object
var formatStyle = {
bold: false,
backgroundColor: "#F0F0F0",
fontFamily: DocumentApp.FontFamily.COURIER_NEW
};
// Utility function to handle selected text elements
function processSelection(handlePartial, handleComplete) {
var currentSelection = DocumentApp.getActiveDocument().getSelection();
if (currentSelection) {
var selectedItems = currentSelection.getSelectedElements();
for (var j = 0; j < selectedItems.length; j++) {
var item = selectedItems[j];
if (item.getElement().editAsText) {
var textElement = item.getElement();
if (item.isPartial()) {
var startPos = item.getStartOffset();
var endPos = item.getEndOffsetInclusive();
return handlePartial(item, textElement, startPos, endPos);
} else {
return handleComplete(item, textElement);
}
}
}
}
}
// Menu callback function
function applyCitationFormat() {
return processSelection(
applyRangeFormat.bind(this, formatStyle),
applyElementFormat.bind(this, formatStyle)
);
}
// Format selected text range
function applyRangeFormat(styleConfig, item, textElement, startPos, endPos) {
textElement.setFontFamily(startPos, endPos, styleConfig.fontFamily);
textElement.setBackgroundColor(startPos, endPos, styleConfig.backgroundColor);
textElement.setBold(startPos, endPos, styleConfig.bold);
}
// Format entire element when fully selected
function applyElementFormat(styleConfig, item, textElement) {
textElement.setFontFamily(styleConfig.fontFamily);
textElement.setBackgroundColor(styleConfig.backgroundColor);
textElement.setBold(styleConfig.bold);
}
I’ve tried looking through documentation and asking AI tools but haven’t figured out how to add the indentation and spacing settings I need. This is for my thesis formatting so any guidance would be super helpful!
Been there with thesis formatting! Your code’s missing proper paragraph spacing. Use paragraph.setSpacingAfter() and setSpacingBefore() methods. Also, convert your measurements - 1.5 line spacing = roughly 18pts. Debug by logging the paragraph object first to see what methods you’ve got.
You’re mixing text attributes with paragraph formatting, and that’s what’s breaking things. I had the same issue when I built similar document scripts. Here’s what fixed it for me: treat paragraphs as your main objects, not text ranges. You need to rewrite your processSelection function to work directly with paragraph elements. Don’t just call editAsText() on everything. First check if your selected element is actually a paragraph using getType() === DocumentApp.ElementType.PARAGRAPH. Once you’ve got a paragraph, you can use setAttributes() with a complete paragraph style object. For your thesis formatting, build an attributes object with stuff like INDENT_START: 113.4, LINE_SPACING: 1, ALIGNMENT: DocumentApp.HorizontalAlignment.JUSTIFY. Keep font changes separate - those still go through text methods. But spacing and indentation? That’s all paragraph-level. This split makes your script way more reliable.
Google Apps Script for complex document formatting is a nightmare. I’ve wasted hours fighting paragraph attributes and point conversions.
Your approach works, but you’ll hit walls with different document types or batch processing. Maintaining all those Google Apps Script functions gets messy.
I switched to Latenode for document automation. Build workflows that connect to Google Docs API and handle formatting through a visual interface. No more wrestling with setIndentStart() or manual centimeter-to-point conversions.
Latenode lets you set formatting rules once and applies them consistently across documents. You can trigger it automatically when documents are created or modified. Way cleaner than embedding scripts everywhere.
For thesis formatting, create nodes for each requirement - indentation, fonts, spacing - and chain them. Much easier to debug than hunting through JavaScript functions.
use paragraph attributes instead of text settings. try textElement.getParent().asParagraph().setIndentFirstLine(113.4) for your 4cm indent and setLineSpacing(1) for single spacing. 4cm converts to about 113pts. for justified text, use setAlignment(DocumentApp.HorizontalAlignment.JUSTIFY).
Your script only handles text formatting, not paragraph properties. You need to work with paragraph objects for spacing and indentation. Add these methods to your style functions: paragraph.setIndentStart(113.4) for the 4cm left indent, paragraph.setSpacingBefore(21.6) for 1.5 line spacing, and don’t forget setFontSize(10) in your formatStyle object for 10pt text. Here’s what got me when I did similar thesis formatting - grab the paragraph object first with textElement.getParent().asParagraph() before using these methods. Google Docs measures in points, so your 4cm indent converts to about 113.4 points.