Automate Formatting in Google Docs: Convert Bolded Paragraphs to Headings with Google Apps Script

Formatting documents can be time-consuming, especially when you have a large Google Doc with numerous bolded paragraphs that need to be converted into specific headings. Fortunately, Google Apps Script allows you to automate this task with a few lines of code! This post will guide you through creating a Google Apps Script to find all bolded paragraphs in your document and convert them into Heading 3 elements.

With this script, you’ll save time and ensure a consistent document structure. Let’s dive in!


Understanding the Goal

Our goal is to:

  • Identify all paragraphs in a Google Document that contain bolded text.
  • Convert these bolded paragraphs into Heading 3 elements for better structure and organization.

This is especially useful for documents with important sections marked in bold but lacking hierarchical formatting. By turning these bold paragraphs into Heading 3 elements, you’ll improve the readability of your document and create a well-structured outline.

Setting Up the Script

Before you start, make sure you’re working in a Google Document where you want to apply this formatting.

  1. Open the Script Editor:
    • In your Google Document, go to Extensions > Apps Script. This will open the Apps Script editor in a new tab.
  2. Clear Any Existing Code:
    • If there’s any code already present in the editor, delete it to start fresh.
  3. Copy and Paste the Script Code (provided below) into the editor.

The Script Code

Here’s the complete Google Apps Script to identify bolded paragraphs and convert them into Heading 3 elements.

/**
* Adds a custom menu to the Google Docs UI for easy access.
*/
function onOpen() {
DocumentApp.getUi()
.createMenu('Custom Scripts')
.addItem('Convert Bold Paragraphs to H3', 'convertBoldParagraphsToH3')
.addToUi();
}

/**
* Finds all paragraphs with bolded text and converts them to Heading 3.
*/
function convertBoldParagraphsToH3() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var convertedCount = 0;

paragraphs.forEach(function(paragraph) {
// Check if the paragraph contains any bold text
var textElement = paragraph.editAsText();
var isBold = false;

for (var i = 0; i < textElement.getText().length; i++) {
if (textElement.isBold(i)) {
isBold = true;
break;
}
}

// If the paragraph contains bold text, convert it to Heading 3
if (isBold) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
convertedCount++;
}
});

DocumentApp.getUi().alert(convertedCount + ' bolded paragraphs have been converted to Heading 3.');
}

Explanation of the Script

  1. onOpen Function:
    • Adds a custom menu titled Custom Scripts to your Google Docs interface, with an item labeled Convert Bold Paragraphs to H3.
    • This menu item allows you to run the main function directly from the Google Docs interface.
  2. convertBoldParagraphsToH3 Function:
    • Purpose: Iterates through all paragraphs in the document, identifies those with bolded text, and converts them to Heading 3 elements.
    • Main Logic:
      • Retrieves all paragraphs in the document.
      • For each paragraph, it checks each character to see if it’s bolded using isBold(i).
      • If any character in the paragraph is bold, it sets the paragraph style to HEADING3.
    • User Feedback: After processing, the script displays an alert showing how many paragraphs were converted.

How to Use the Script

  1. Save and Authorize the Script:
    • Click the save icon (💾) to save your script.
    • Run the onOpen function to add the custom menu, then authorize the script when prompted.
  2. Refresh the Document:
    • Go back to your Google Document and refresh the page to ensure the custom menu loads.
  3. Run the Script:
    • In the Google Docs toolbar, click on Custom Scripts > Convert Bold Paragraphs to H3.
    • The script will convert all bolded paragraphs into Heading 3 elements and notify you of the number of paragraphs updated.

Example of the Script in Action

Before Running the Script

Let’s say you have the following paragraphs:

  • “Important Notice: Please review the new guidelines.” (The entire paragraph is bolded)
  • “This is a regular paragraph with no bold text.”
  • “Action Required: Submit your project by Monday.” (The entire paragraph is bolded)

After Running the Script

After executing the script, the bolded paragraphs are converted to Heading 3 elements:

  • Converted to Heading 3: “Important Notice: Please review the new guidelines.”
  • Unchanged: “This is a regular paragraph with no bold text.”
  • Converted to Heading 3: “Action Required: Submit your project by Monday.”

This automated formatting makes your document more structured and readable, with important sections visually distinguished.

Tips and Troubleshooting

Here are a few tips and solutions to common issues you might encounter:

  1. Custom Menu Not Appearing:
    • Make sure you’ve saved and authorized the script, then refresh your Google Document.
  2. Script Not Converting Bold Paragraphs:
    • Double-check that the paragraphs you want to convert are fully bolded.
    • This script currently checks for any bolded text in a paragraph. You can adjust it if you want it to apply only to fully bold paragraphs.
  3. Authorization Issues:
    • If you encounter authorization errors, return to the Apps Script editor, run the onOpen function again, and reauthorize the script.

Advanced Enhancements

If you’d like to extend the functionality of the script, here are some ideas:

1. Convert Based on Specific Formatting

You might want to convert paragraphs based on other criteria, such as italicized text, specific font sizes, or even color.

2. Different Heading Levels Based on Content

You could modify the script to convert paragraphs to different heading levels based on keywords in the paragraph or its length.

3. Create a Summary or Table of Contents

After converting bold paragraphs to headings, you could add a feature to generate a summary or table of contents based on the newly created headings.