If you’ve ever had to manually edit paragraphs in a Google Doc, you know it can be a tedious task. For example, imagine working through a document where you must find every paragraph starting with “// Exercise X:” and convert them into proper headings. Not only is this repetitive, but it’s also easy to make mistakes if you’re working with a long document. Luckily, Google Apps Script can come to the rescue! Today, we’ll walk through a script that does just that: automatically finding and converting paragraphs into headings in your Google Doc.
What Does the Script Do?
The Google Apps Script we’re discussing today has a simple but powerful goal: it scans through your Google Doc to find paragraphs that begin with “// Exercise”, removes the prefix (including the number), and converts the paragraph into an H3 heading. This is incredibly useful if you’re creating educational materials, exercises, or any structured content where paragraphs need to be transformed into headings for better organization.
The Code Explained
Here’s the Google Apps Script that accomplishes the task:
function convertExercisesToHeadings() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
const exercisePattern = /^\/\/\s*Exercise\s*\d+\s*:/;
paragraphs.forEach(paragraph => {
const text = paragraph.getText();
if (exercisePattern.test(text)) {
const newText = text.replace(exercisePattern, '').trim();
paragraph.setText(newText);
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
}
});
}
Let’s break down what this script does, step by step:
- Access the Document: The
convertExercisesToHeadings()
function starts by accessing the active Google Doc usingDocumentApp.getActiveDocument()
. Thebody
of the document is then retrieved, which is where all the content resides. - Retrieve Paragraphs: It then gets all the paragraphs in the document using
body.getParagraphs()
. This is important since we need to iterate over all the paragraphs to find the ones that match our criteria. - Define the Pattern: The
exercisePattern
variable is a regular expression (/^\/\/\s*Exercise\s*\d+\s*:/
) that matches paragraphs starting with “// Exercise”, followed by a number and a colon. The regular expression helps locate the paragraphs that we want to edit. - Loop Through Paragraphs: The script then loops through each paragraph. If a paragraph matches the pattern (checked using
exercisePattern.test(text)
), the script:- Removes the Prefix: The
text.replace(exercisePattern, '').trim()
removes the “// Exercise X:” part and trims any leading or trailing whitespace. - Update and Transform: It then sets the updated text back into the paragraph and changes its style to
HEADING3
.
- Removes the Prefix: The
Why Use This Script?
This script is perfect if you’re working with documents with many structured exercises or sections you want to convert into headings for better readability. Instead of manually scrolling and editing each paragraph, you can automate the process, saving time and reducing the risk of errors.
How to Use the Script
To use this script in your Google Docs environment:
- Open the document where you want to apply the changes.
- Click on Extensions > Apps Script to open the Google Apps Script editor.
- Paste the code into the editor and click the Run button.
After running the script, all the paragraphs starting with “// Exercise X:” will be transformed into H3 headings, allowing for a more organized and structured document.
Final Thoughts
Google Apps Script is a powerful tool for automating tasks in Google Workspace apps, like Docs, Sheets, and Slides. This script demonstrates how you can leverage it to manipulate text efficiently, making your documents more precise and organized. Whether you’re a teacher creating exercises, a developer writing tutorials, or simply someone who likes well-structured documents, this script can save you significant time.
If you have any questions or want to expand the functionality of this script, feel free to ask! There’s a lot you can do with Google Apps Script, and this example is just the beginning.