In today’s fast-paced digital environment, automating routine formatting tasks can save you a lot of time. One common need is to highlight sentences that end with a question mark—especially when reviewing documents or preparing Q&A content. In this blog post, we’ll walk through how to create a Google Apps Script that scans through your Google Doc, identifies every sentence ending with a question mark, and makes the entire sentence bold.
Why Automate Text Formatting?
Manual editing in large documents can be error-prone and time-consuming. By using Google Apps Script, you can automate repetitive formatting tasks, including:
- Enhancing Readability: Bolded question sentences pop on the page, drawing your reader’s attention.
- Streamlining Reviews: Quickly locate and review questions in a lengthy document.
- Improving Consistency: Ensure that all question sentences are uniformly formatted.
The Approach
The script uses a simple regular expression to detect sentences that end with a question mark within each paragraph of the document. Here’s an outline of the process:
- Access the Document: The script retrieves the active Google Doc’s body.
- Iterate Through Paragraphs: It loops over each paragraph to access the text.
- Identify Question Sentences: A regular expression (
/[^.!?]+[?]+/g
) finds segments ending with a question mark. - Apply Bold Formatting: For each sentence that ends with a question mark, the script bolds the text.
Step-by-Step Code Explanation
Below is the complete Apps Script code. You can follow these steps to implement it in your Google Doc:
1. Open the Google Doc and Access the Script Editor
- Go to your Google Document.
- Click on Extensions > Apps Script to open the script editor.
2. Paste the Code into the Script Editor
function boldQuestionSentences() {
// Get the active document body.
var body = DocumentApp.getActiveDocument().getBody();
// Get all paragraphs in the document.
var paragraphs = body.getParagraphs();
// Loop over each paragraph.
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
// Get the full text of the paragraph.
var text = paragraph.getText();
// This regex matches sequences of characters ending with one or more question marks.
var sentenceRegex = /[^.!?]+[?]+/g;
var match;
// Loop over all matches (i.e. candidate sentences ending with '?' in this paragraph).
while ((match = sentenceRegex.exec(text)) !== null) {
var sentence = match[0];
// Trim whitespace and check if the sentence indeed ends with a '?'
if (sentence.trim().slice(-1) === '?') {
// Determine start and end indices of the found sentence within the paragraph.
var start = match.index;
var end = match.index + sentence.length - 1;
// Bold the text range.
paragraph.editAsText().setBold(start, end, true);
}
}
}
}
3. Save and Run the Script
- Authorize the Script: On the first run, you will need to authorize the script to modify your document.
- Run the Function: Click the run button (▶️) next to the
boldQuestionSentences
function. The script will process each paragraph and bold any sentence ending with a question mark.
How the Code Works
- Document Access:
The script starts by retrieving the document’s body using:DocumentApp.getActiveDocument().getBody();
- Paragraph Processing:
The entire document is divided into paragraphs, and each paragraph is processed individually. This makes it easy to handle different text blocks. - Regular Expression and Matching:
The regular expression/[^.!?]+[?]+/g
is used to find continuous sequences of text that finish with one or more question marks. This regex ensures that only sentences that naturally conclude with a question mark are selected. - Bold Text Application:
For each matching sentence, the script uses theeditAsText()
method to apply bold formatting over the detected range:paragraph.editAsText().setBold(start, end, true);
This simple approach guarantees that the entire sentence is emphasized without affecting other parts of your text.
Final Thoughts
Automating the formatting of your Google Docs using Apps Script not only improves efficiency but also enhances document clarity. The script discussed here is a straightforward starting point. Depending on the complexity of your documents, you might need to refine the regular expression or expand the script to handle various text elements like tables or lists.
Feel free to experiment with the code and tailor it to your specific needs. With a little bit of customization, you can further optimize your document editing workflow—making your Google Docs work smarter, not harder.
