In Google Docs, using bold text is a common way to highlight key points or section titles. However, if you’re looking to standardize your document formatting by turning bolded lines into proper headings, this can be a tedious task to do manually. Fortunately, Google Apps Script can help! In this post, we’ll walk you through a simple script that will automatically convert all bolded lines in your Google Doc into H3 headings.
By leveraging Apps Script, you can ensure consistent formatting throughout your document, saving time and enhancing readability.
Why Use Google Apps Script?
Google Apps Script is a JavaScript-based platform that allows you to automate repetitive tasks and extend the functionality of Google Workspace apps like Google Docs. In this tutorial, we’ll use it to quickly find all bolded lines and convert them to H3 headings, which are ideal for sub-sections or minor headings in your document.
Apps Script to Convert Bolded Lines to H3
Here’s the code to set up your script in Google Docs:
function convertBoldToH3() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
paragraphs.forEach(paragraph => {
const text = paragraph.getText().trim();
// Check if the paragraph is bolded and not empty
if (text !== "" && isParagraphBold(paragraph)) {
// Apply H3 style to the paragraph
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
}
});
}
// Helper function to check if the entire paragraph is bold
function isParagraphBold(paragraph) {
const attributes = paragraph.getAttributes();
return attributes[DocumentApp.Attribute.BOLD] === true;
}
How the Script Works
- Retrieve Document Content: The script begins by accessing the active Google Doc and retrieving its body content.
- Loop Through Paragraphs: It iterates through each paragraph in the document.
- Check for Bold Text: Using
isBold(0)
, the script checks if the first character of the paragraph is bold. If the entire paragraph is bold, this is true for the rest of the paragraph as well. - Convert to H3: If the paragraph is bold and not empty, the script applies the H3 heading style (
HEADING3
) to the paragraph, instantly converting it into an H3 heading.
Setting Up the Script
To use this script, follow these steps:
- Open Google Docs: Open the Google Doc where you want to convert bolded lines to H3 headings.
- Open Google Apps Script:
- Go to Extensions > Apps Script.
- This opens the Apps Script editor in a new tab.
- Paste the Code: Copy and paste the code above into the editor.
- Save and Run:
- Save your project, naming it something like “Convert Bold to H3.”
- Select
convertBoldToH3
from the dropdown list in the toolbar and click the Run button.
- Grant Permissions: When running for the first time, the script will ask for permission to access your Google Docs. Approve the request to proceed.
Example Use Case
Imagine your document contains sections like these:
**Introduction**
Some text explaining the introduction.
**Methods**
Details on methods used.
**Conclusion**
Final thoughts on the subject.
After running this script, all lines that were previously bolded (like “Introduction,” “Methods,” and “Conclusion”) will automatically be converted to H3 headings, making them appear larger and more structured within your document.
Customizing the Script
If you want to convert bolded lines to other heading levels, you can modify the line:
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
Change HEADING3
to another level, such as HEADING2
or HEADING4
, as needed.
Conclusion
Using this Google Apps Script, you can easily convert bolded lines in your Google Doc to H3 headings, ensuring a cleaner and more consistent document structure. This quick automation is perfect for standardizing section titles, making your document more readable and professionally formatted.
