Automate Formatting in Google Docs with Google Apps Script

If you’ve ever found yourself manually converting single-item lists into headings in Google Docs, you’ll appreciate how much time automation can save. With Google Apps Script, you can create a script to identify single-item lists with specific font sizes, remove their list formatting, and convert them into regular headings.

This post introduces a powerful function, convertSingleListItemsToH2(), to streamline this repetitive task.


What Does the Script Do?

The convertSingleListItemsToH2() function automates the process of:

  1. Identifying Single-Item Lists: Finds list items in a Google Docs document.
  2. Checking Font Size: Filters list items based on their font size (e.g., font size 12).
  3. Converting to H2 Headings: Removes the list formatting and transforms the content into a regular H2 heading.

The Script: convertSingleListItemsToH2

Here’s the full script:

function convertSingleListItemsToH2() {
const DOCID = "YOUR_DOCUMENT_ID"; // Replace with your actual Google Docs ID
const doc = DocumentApp.openById(DOCID); // Open the document by ID
const body = doc.getBody(); // Get the body of the document
const elements = body.getNumChildren(); // Get the number of child elements in the body

for (let i = 0; i < elements; i++) {
const element = body.getChild(i);

// Check if the element is a list item
if (element.getType() === DocumentApp.ElementType.LIST_ITEM) {
const listItem = element.asListItem();
const text = listItem.getText();

// Get the font size of the text
const textStyle = listItem.editAsText();
const fontSize = textStyle.getFontSize(0); // Font size of the first character

// Check if the font size is 12
if (fontSize === 12) {
Logger.log("Converting: " + text);

// Create a new paragraph with the same text
const newParagraph = body.insertParagraph(i, text);
newParagraph.setHeading(DocumentApp.ParagraphHeading.HEADING2); // Set as H2 heading

// Remove the original list item
body.removeChild(listItem);
}
}
}
Logger.log("Single-item lists with font size 12 have been converted to regular H2 headings.");
}

How It Works

  1. Document Access:
    • The script accesses the document using its ID (DOCID).
  2. Iterate Through Elements:
    • Loops through all child elements of the document body using getNumChildren().
  3. Filter List Items:
    • Identifies list items using getType() and checks their font size with editAsText() and getFontSize().
  4. Convert to Regular Paragraph:
    • Inserts a new paragraph with the same text as the list item.
    • Sets the new paragraph’s style to H2 heading using setHeading().
  5. Remove List Formatting:
    • Deletes the original list item using removeChild().

Use Cases

This script is particularly useful for:

  • Cleaning Up Documents: Converts single-item lists into a consistent heading format.
  • Streamlining Document Styling: Applies uniform heading styles across the document.
  • Automating Repetitive Tasks: Saves time by automating the tedious process of reformatting list items.

Step-by-Step Guide to Use the Script

  1. Copy the Script:
    • Paste the above code into the Apps Script editor (Extensions > Apps Script).
  2. Replace YOUR_DOCUMENT_ID:
    • Update the DOCID variable with the ID of your Google Docs file. The ID is found in the document’s URL.
  3. Save and Run:
    • Save the script, select the convertSingleListItemsToH2 function, and click the ▶️ play button.
  4. Authorize the Script:
    • Grant the necessary permissions when prompted.
  5. Review Your Document:
    • Check your Google Docs file to see the transformed headings.

Tips and Customization

  1. Change the Font Size Filter:
    • Modify the font size condition (if (fontSize === 12)) to target a different size.
  2. Use Different Heading Levels:
    • Replace HEADING2 with HEADING1, HEADING3, etc., for different heading styles.
  3. Log Changes:
    • Use Logger.log() to review which items were converted.