This script identifies specific words or phrases (e.g., Example:
, Code Snippet:
, Instructions:
, Solution:
) that constitute the entire paragraph or element in a Google Docs document and converts them to bold text.
The Script
function boldSpecificPhrases() {
const doc = DocumentApp.getActiveDocument(); // Access the active Google Doc
const body = doc.getBody(); // Get the document's body content
const paragraphs = body.getParagraphs(); // Get all paragraphs in the document
// List of phrases to bold
const phrasesToBold = ["Example:", "Code Snippet:", "Instructions:", "Solution:"];
paragraphs.forEach(paragraph => {
const text = paragraph.getText().trim(); // Get the text of the paragraph and trim whitespace
if (phrasesToBold.includes(text)) { // Check if the entire text matches one of the phrases
paragraph.editAsText().setBold(true); // Bold the entire paragraph
}
});
Logger.log("Specified phrases have been bolded.");
}
How It Works
- Access Paragraphs:
- The script retrieves all paragraphs in the document using
getParagraphs()
.
- The script retrieves all paragraphs in the document using
- Match Specific Phrases:
- It checks if the trimmed text of the paragraph matches any phrase in the predefined list
phrasesToBold
.
- It checks if the trimmed text of the paragraph matches any phrase in the predefined list
- Apply Bold Styling:
- If a match is found, the entire paragraph is bolded using
setBold(true)
.
- If a match is found, the entire paragraph is bolded using
Usage Instructions
1. Open the Apps Script Editor
- In your Google Docs, go to
Extensions > Apps Script
.
2. Add the Script
- Paste the above code into the editor.
3. Save and Name Your Project
- Click the save icon and name your project (e.g.,
BoldPhrases
).
4. Run the Script
- From the toolbar, select the
boldSpecificPhrases
function and click the play ▶️ button. - Grant the necessary permissions when prompted.
5. Verify the Changes
- Check your document to see that the specified phrases have been bolded.
Customization Options
- Add More Phrases:
- Update the
phrasesToBold
array with additional phrases to bold.
const phrasesToBold = ["Example:", "Code Snippet:", "Instructions:", "Solution:", "Your Custom Phrase:"];
- Update the
- Case Insensitivity:
- To make the matching case-insensitive, convert both the paragraph text and phrases to lowercase before comparing:
const text = paragraph.getText().trim().toLowerCase(); const lowerCasePhrases = phrasesToBold.map(phrase => phrase.toLowerCase()); if (lowerCasePhrases.includes(text)) { ... }
- Partial Matches:
- Modify the condition to allow partial matches using
.includes()
:
if (phrasesToBold.some(phrase => text.includes(phrase))) { ... }
- Modify the condition to allow partial matches using
Practical Applications
- Educational Content: Bold important labels like
Example:
andSolution:
for better visibility. - Technical Documents: Highlight terms like
Code Snippet:
to improve readability. - Formatting Templates: Quickly apply consistent styling to frequently used phrases.