When working on structured documents, especially educational materials or guides, it’s common to have sections labeled with examples (e.g., Example 1:, Example 2:). Formatting these sections manually as headings can be time-consuming.
With Google Apps Script, you can automate this task! In this blog post, we’ll create a script to identify all elements starting with the word “Example” followed by a number and a colon (e.g., Example 2:) and convert them into H3 headings.
The Power of Google Apps Script
Google Apps Script is a JavaScript-based platform that lets you extend the functionality of Google Workspace apps like Docs, Sheets, and Slides. It’s perfect for automating repetitive tasks like reformatting document content.
The Script: Converting Example Elements to H3
Here’s how you can implement this automation in your Google Docs:
function convertExamplesToHeadings() {
const doc = DocumentApp.getActiveDocument(); // Access the active Google Doc
const body = doc.getBody(); // Get the document's body content
const paragraphs = body.getParagraphs(); // Get all paragraphs in the document
const examplePattern = /^Example \d+:/; // Regular expression to match lines like "Example 1:"
paragraphs.forEach(paragraph => {
const text = paragraph.getText(); // Get the text of the paragraph
if (examplePattern.test(text)) { // Check if the text matches the pattern
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3); // Convert the paragraph to an H3 heading
}
});
Logger.log("All 'Example' elements have been converted to H3 headings.");
}
How It Works
getBody(): Fetches the body content of the document.getParagraphs(): Retrieves all paragraphs for analysis.- Regular Expression (
/^Example \d+:/):- Matches text starting with the word “Example”, followed by a number, and ending with a colon.
setHeading(): Converts matching elements into H3 headings.
Step-by-Step Guide to Use the Script
1. Open the Apps Script Editor
- In your Google Docs, go to
Extensions > Apps Script.
2. Add the Script
- Paste the above script into the editor.
3. Save and Name Your Project
- Click the save icon and name the project (e.g.,
ExampleToHeadings).
4. Run the Script
- From the toolbar, select the
convertExamplesToHeadingsfunction and click the play ▶️ button. - Authorize the script when prompted.
5. Check Your Document
- All lines starting with
Examplefollowed by a number and a colon will now be formatted as H3 headings.
Practical Applications
- Educational Content: Automatically format examples in tutorials, guides, or textbooks.
- Technical Documentation: Highlight sections labeled as examples for better readability.
- Reports and Manuals: Ensure consistent styling of numbered examples throughout documents.
Customization Options
- Change Heading Level:
- Replace
HEADING3withHEADING1,HEADING2, etc., to adjust the heading level.
- Replace
- Adjust the Pattern:
- Modify the regular expression to match other patterns, such as
Example A:,Case Study 1:, etc.
- Modify the regular expression to match other patterns, such as
- Add Styling:
- Combine this script with additional styling options to further customize the appearance of headings.
