Created
May 27, 2023 23:03
-
-
Save g-simmons/1a29de1a2a9f1cd8f3dd2f743136ffdd to your computer and use it in GitHub Desktop.
Set indentation levels and font sizes for nested lists in google doc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ui = DocumentApp.getUi(); | |
ui.createMenu('Custom Menu') | |
.addItem('Set Indentation and Font Size', 'setIndentationAndFontSize') | |
.addToUi(); | |
} | |
function setIndentationAndFontSize() { | |
// var doc = DocumentApp.getActiveDocument(); | |
var doc = DocumentApp.openById('<<<<YOUR-DOCUMENT-ID>>>>'); | |
var body = doc.getBody(); | |
Logger.log(body); | |
// Define indentation amounts and font sizes for each nesting level | |
var indentationLevelsStart = [0,25,50,75,100,115]; // Adjust indentation values as needed | |
var fontSizes = [18,14,10,8,6,6]; // Adjust font sizes as needed | |
// Get all paragraphs in the document | |
var paragraphs = body.getParagraphs(); | |
var numberedLists = body.getListItems(); | |
for (var i =0; i < numberedLists.length; i++) { | |
var listItem = numberedLists[i]; | |
var nestingLevel = Math.floor(listItem.getNestingLevel()); | |
if (nestingLevel >= 0 && nestingLevel <= 6) { | |
// Set the appropriate indentation and font size based on nesting level | |
var indentationStart = indentationLevelsStart[nestingLevel]; | |
var indentationFirst = indentationStart + 18; | |
var fontSize = fontSizes[nestingLevel]; | |
listItem.setIndentFirstLine(indentationStart); | |
try {listItem.setIndentStart(indentationFirst);} | |
catch (error){Logger.log(error);} | |
listItem.setFontSize(fontSize); | |
listItem.setGlyphType(DocumentApp.GlyphType.BULLET).setGlyphType(DocumentApp.GlyphType.NUMBER); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment