Are you tired of manually formatting questions in your Google Docs? With Google Apps Script, you can automate this tedious task and ensure consistency throughout your document. In this blog post, we’ll show you how to create a script that identifies lines starting with a number followed by ‘Question:’, removes the number, and converts the line to an H3 heading.
What is Google Apps Script?
Google Apps Script is a JavaScript-based platform that allows you to extend Google Workspace applications like Google Docs, Sheets, and Forms. With Apps Script, you can automate repetitive tasks, create custom functions, and enhance your productivity with just a few lines of code.
The Task
Our goal is to write a script that:
- Identifies lines in a Google Doc that start with a number followed by ‘Question:’.
- Removes the leading number.
- Converts these lines into H3 headings.
Step-by-Step Guide
- Open Your Google Doc:
- Go to your Google Drive and open the document you want to format.
- Access the Apps Script Editor:
- Click on
Extensions
>Apps Script
.
- Click on
- Write the Script:
- Delete any existing code in the script editor and replace it with the following script:
function formatQuestionsAsHeadings() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var questionRegex = /^\d+\.\s*Question:/;
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText().trim();
// Check if the paragraph starts with a number followed by 'Question:'
if (questionRegex.test(text)) {
// Remove the leading number and any extra spaces
var newText = text.replace(/^\d+\.\s*/, '');
// Ensure the new text is set and then format as H3 heading
paragraphs[i].setText(newText);
paragraphs[i].setHeading(DocumentApp.ParagraphHeading.HEADING3);
}
}
}
- Save the Script:
- Click the floppy disk icon or press
Ctrl + S
to save your script. You can name it something descriptive likeFormatQuestionsAsHeadings
.
- Click the floppy disk icon or press
- Run the Script:
- Click the play button (triangle icon) to run the script. You might be prompted to authorize the script to access your Google Doc.
- Review the Changes:
- After running the script, check your document. All lines starting with a number followed by ‘Question:’ should now be formatted as H3 headings.
Understanding the Script
- Get the Active Document:
var doc = DocumentApp.getActiveDocument();
retrieves the currently open Google Doc. - Get the Body of the Document:
var body = doc.getBody();
accesses the main text area of the document. - Get All Paragraphs:
var paragraphs = body.getParagraphs();
returns an array of all paragraphs in the document. - Loop Through Each Paragraph: The
for
loop iterates through each paragraph. - Check for Numbered ‘Question:’:
if (questionRegex.test(text))
checks if a paragraph starts with a number followed by ‘Question:’. - Remove the Leading Number:
var newText = text.replace(/^\d+\.\s*/, '');
removes the leading number and any extra spaces. - Set as H3 Heading:
paragraphs[i].setText(newText); paragraphs[i].setHeading(DocumentApp.ParagraphHeading.HEADING3);
updates the paragraph text and changes the paragraph style to H3.
Benefits of Automation
Automating this task not only saves time but also ensures consistency throughout your document. Whether you’re preparing a study guide, a FAQ document, or any content that involves questions, this script will help you format it efficiently.
