If you work extensively with Google Docs, you’ve probably found yourself needing to clean up headings or rearrange content. Repetitive tasks like updating headings or removing redundant lines can consume valuable time. Thankfully, Google Apps Script provides a powerful way to automate such tasks.
In this post, we’ll walk through a Google Apps Script function that processes Heading 4
paragraphs in a Google Doc, removes specific patterns, and promotes subsequent text into the heading, simplifying document organization.
The Problem: Managing Structured Content in Google Docs
Imagine you’re working on a document containing hundreds of headings like:
Heading 4: Question 10
Paragraph: Which loop is most suitable when the number of iterations is known beforehand?
Your goal is to remove the “Question 10” text from the heading and move the subsequent paragraph into its place, while preserving the Heading 4
style:
Heading 4: Which loop is most suitable when the number of iterations is known beforehand?
Manually making these edits is time-consuming, but with Google Apps Script, you can automate this in seconds.
The Solution: Google Apps Script Function
Below is the reformatHeading4Questions
function that solves this problem:
function reformatHeading4Questions() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
// Iterate through paragraphs in reverse order
for (let i = paragraphs.length - 1; i >= 0; i--) {
const paragraph = paragraphs[i];
// Check if the paragraph is Heading 4 and matches the "Question <number>" pattern
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING4) {
const text = paragraph.getText().trim();
// Match "Question <number>" where the number is between 1 and 25
const match = text.match(/^Question (2[0-5]|1[0-9]|[1-9])$/);
if (match) {
// Check if there is a next paragraph to move as Heading 4
if (i + 1 < paragraphs.length) {
const nextParagraph = paragraphs[i + 1];
const nextText = nextParagraph.getText().trim();
// Update the current Heading 4 paragraph with the next paragraph's content
paragraph.setText(nextText);
// Remove the "next paragraph"
body.removeChild(nextParagraph);
}
}
}
}
Logger.log("Processed all Heading 4 'Question' headings successfully.");
}
How It Works
- Iterate Through Paragraphs in Reverse:
- The script loops through all paragraphs in reverse order to prevent conflicts caused by modifying the document structure during iteration.
- Check for Heading 4:
- It checks if a paragraph is styled as
Heading 4
usingparagraph.getHeading()
.
- It checks if a paragraph is styled as
- Match Specific Patterns:
- A regex pattern
/^Question (2[0-5]|1[0-9]|[1-9])$/
ensures only headings likeQuestion 1
throughQuestion 25
are processed.
- A regex pattern
- Promote Next Paragraph Content:
- The content of the next paragraph is moved into the
Heading 4
paragraph, replacing the original text.
- The content of the next paragraph is moved into the
- Remove Redundant Paragraph:
- The script deletes the subsequent paragraph after its content is moved, maintaining a clean structure.
Running the Script
- Open a Google Doc containing the headings to be processed.
- Navigate to
Extensions > Apps Script
. - Paste the script into the editor.
- Save and run the
reformatHeading4Questions
function.
Before and After
Before Running the Script:
Heading 4: Question 10
Paragraph: Which loop is most suitable when the number of iterations is known beforehand?
After Running the Script:
Heading 4: Which loop is most suitable when the number of iterations is known beforehand?
Key Benefits
- Saves Time: Automates tedious manual edits, especially in large documents.
- Ensures Consistency: Applies uniform formatting and organization throughout the document.
- Customizable: Easily adapt the script to other heading styles or content structures.
Use Cases
- Educational Content: Reformat question-based headings and merge explanations.
- Document Cleanup: Remove placeholders or redundant text in headings.
- Standardized Reports: Apply consistent heading styles in business or research documents.
Conclusion
Google Apps Script offers a powerful way to streamline repetitive tasks in Google Docs. The reformatHeading4Questions
function demonstrates how simple scripts can save time and improve productivity by automating content reorganization. Whether you’re managing educational materials, reports, or any structured document, this script can make your workflow smoother and more efficient.