function onOpen() {
DocumentApp.getUi()
.createMenu('My Formatting')
.addItem('Apply Custom Formatting', 'applyCustomFormatting')
.addToUi();
}
function applyCustomFormatting() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// Set the font for the entire document to Inter
body.editAsText().setFontFamily('Inter');
// Get all paragraphs in the document
var paragraphs = body.getParagraphs();
// Format all paragraphs
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
// Skip paragraphs inside table cells, they are handled later
if (paragraph.getParent().getType() === DocumentApp.ElementType.TABLE_CELL) {
continue;
}
var text = paragraph.editAsText();
text.setFontFamily('Inter');
paragraph.setLineSpacing(1.15);
var heading = paragraph.getHeading();
if (heading === DocumentApp.ParagraphHeading.HEADING1) {
text.setFontSize(18); text.setBold(true);
paragraph.setSpacingBefore(24); paragraph.setSpacingAfter(12);
} else if (heading === DocumentApp.ParagraphHeading.HEADING2) {
text.setFontSize(16); text.setBold(true);
paragraph.setSpacingBefore(18); paragraph.setSpacingAfter(9);
} else if (heading === DocumentApp.ParagraphHeading.HEADING3) {
text.setFontSize(14); text.setBold(true);
paragraph.setSpacingBefore(14); paragraph.setSpacingAfter(7);
} else if (heading === DocumentApp.ParagraphHeading.HEADING4) {
text.setFontSize(12); text.setBold(true);
paragraph.setSpacingBefore(12); paragraph.setSpacingAfter(6);
} else if (heading === DocumentApp.ParagraphHeading.HEADING5) {
text.setFontSize(10); text.setBold(true);
paragraph.setSpacingBefore(10); paragraph.setSpacingAfter(5);
} else {
text.setFontSize(10);
}
}
// Handle tables
var tables = body.getTables();
for (var i = 0; i < tables.length; i++) {
var table = tables[i];
var numRows = table.getNumRows();
for (var row = 0; row < numRows; row++) {
var tableRow = table.getRow(row);
tableRow.setMinimumHeight(0);
for (var cell = 0; cell < tableRow.getNumCells(); cell++) {
var tableCell = tableRow.getCell(cell);
tableCell.setPaddingTop(1); tableCell.setPaddingBottom(1);
tableCell.setPaddingLeft(10); tableCell.setPaddingRight(10);
var numCellChildren = tableCell.getNumChildren();
for (var child = 0; child < numCellChildren; child++) {
if (tableCell.getChild(child).getType() === DocumentApp.ElementType.PARAGRAPH) {
var cellParagraph = tableCell.getChild(child).asParagraph();
var cellText = cellParagraph.editAsText();
cellText.setFontFamily('Inter');
cellText.setFontSize(8);
cellParagraph.setLineSpacing(1.0);
}
}
}
}
}
}