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:
- Identifying Single-Item Lists: Finds list items in a Google Docs document.
- Checking Font Size: Filters list items based on their font size (e.g., font size 12).
- 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
- Document Access:
- The script accesses the document using its ID (
DOCID).
- The script accesses the document using its ID (
- Iterate Through Elements:
- Loops through all child elements of the document body using
getNumChildren().
- Loops through all child elements of the document body using
- Filter List Items:
- Identifies list items using
getType()and checks their font size witheditAsText()andgetFontSize().
- Identifies list items using
- 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().
- Remove List Formatting:
- Deletes the original list item using
removeChild().
- Deletes the original list item using
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
- Copy the Script:
- Paste the above code into the Apps Script editor (
Extensions > Apps Script).
- Paste the above code into the Apps Script editor (
- Replace
YOUR_DOCUMENT_ID:- Update the
DOCIDvariable with the ID of your Google Docs file. The ID is found in the document’s URL.
- Update the
- Save and Run:
- Save the script, select the
convertSingleListItemsToH2function, and click the ▶️ play button.
- Save the script, select the
- Authorize the Script:
- Grant the necessary permissions when prompted.
- Review Your Document:
- Check your Google Docs file to see the transformed headings.
Tips and Customization
- Change the Font Size Filter:
- Modify the font size condition (
if (fontSize === 12)) to target a different size.
- Modify the font size condition (
- Use Different Heading Levels:
- Replace
HEADING2withHEADING1,HEADING3, etc., for different heading styles.
- Replace
- Log Changes:
- Use
Logger.log()to review which items were converted.
- Use
