If you regularly work with Google Docs, you might find yourself manually formatting text to improve readability. For instance, you might want to highlight questions in your document by converting them into headings. With Google Apps Script, you can automate this process, saving time and effort. In this blog post, we’ll walk through a script that converts lines starting with ‘Question:’ into H3 headings in Google Docs.
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. 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 ‘Question:’.
- 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 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();
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText().trim();
// Check if the paragraph starts with 'Question:'
if (text.startsWith('Question:')) {
// Set the paragraph as an H3 heading
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 ‘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 ‘Question:’:
if (text.startsWith('Question:'))
checks if a paragraph starts with ‘Question:’. - Set as H3 Heading:
paragraphs[i].setHeading(DocumentApp.ParagraphHeading.HEADING3);
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.
Conclusion
Google Apps Script is a powerful tool that can significantly enhance your productivity by automating repetitive tasks. With just a few lines of code, you can transform your Google Docs experience. Try out the script and see how it can simplify your document formatting tasks. Happy scripting!
