Google Apps Script is a powerful tool for automating tasks across Google Workspace applications. In this blog post, we will walk through how to use Google Apps Script to update specific paragraphs in a Google Doc. We will cover two functionalities:
- Bold paragraphs that end with a question mark.
- Insert a blank line above paragraphs containing bold text.
We’ll also include an image to enhance the visual appeal of this post.
Step-by-Step Guide
- Open Google Apps Script:
- Go to Google Apps Script and create a new project.
- Bold Paragraphs Ending with a Question Mark:
- Below is the script to find and bold paragraphs that end with a question mark in your Google Doc.
function boldParagraphsEndingWithQuestionMark() {
// Replace with the ID of your Google Doc
var docId = 'YOUR_DOC_ID_HERE';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText();
if (text.endsWith('?')) {
paragraphs[i].setBold(true);
Logger.log('Bolded paragraph: ' + text);
}
}
}
- Insert Blank Line Above Bold Paragraphs:
- Below is the script to insert a blank line above paragraphs that contain bold text.
function addBlankLineAboveBoldParagraphs() {
// Replace with the ID of your Google Doc
var docId = 'YOUR_DOC_ID_HERE';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = paragraphs.length - 1; i >= 0; i--) {
var paragraph = paragraphs[i];
var text = paragraph.getText();
Logger.log(text);
// Check if the paragraph contains bold text
if (paragraph.isBold()) {
// Insert a blank paragraph directly above
body.insertParagraph(i, '');
}
}
}
- Customize the Scripts:
- Replace
'YOUR_DOC_ID_HERE'
with the actual ID of your Google Doc. You can find this ID in the URL of your document (it’s the long string of characters between/d/
and/edit
).
- Replace
- Run the Scripts:
- Save your scripts and click the play button (Run) to execute them. You might be prompted to authorize the scripts to access your Google Docs.
- Verify the Changes:
- Open your Google Doc and check if the paragraphs ending with a question mark are bolded and if blank lines are inserted above bold paragraphs.
Explanation of the Scripts
- Bold Paragraphs Script:
DocumentApp.openById(docId)
: Opens the Google Doc with the specified ID.getBody()
: Retrieves the body of the document.getParagraphs()
: Retrieves all the paragraphs in the document.getText()
: Gets the text of a paragraph.setBold(true)
: Sets the paragraph text to bold.
- Insert Blank Line Script:
isBold()
: Checks if the paragraph text is bold.insertParagraph(i, '')
: Inserts a blank paragraph above the specified index.
Image for Visual Representation
Here is an illustration to help visualize the process:
Full Combined Script
function boldParagraphsEndingWithQuestionMark() {
// Replace with the ID of your Google Doc
var docId = 'YOUR_DOC_ID_HERE';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText();
if (text.endsWith('?')) {
paragraphs[i].setBold(true);
Logger.log('Bolded paragraph: ' + text);
}
}
}
function addBlankLineAboveBoldParagraphs() {
// Replace with the ID of your Google Doc
var docId = 'YOUR_DOC_ID_HERE';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = paragraphs.length - 1; i >= 0; i--) {
var paragraph = paragraphs[i];
var text = paragraph.getText();
Logger.log(text);
// Check if the paragraph contains bold text
if (paragraph.isBold()) {
// Insert a blank paragraph directly above
body.insertParagraph(i, '');
}
}
}
Conclusion
With Google Apps Script, you can automate and streamline many of your Google Docs tasks. This guide showed you how to bold paragraphs ending with a question mark and insert blank lines above paragraphs with bold text. Experiment with other features of Google Apps Script to enhance your productivity even further.