When working with structured documents in Google Docs, it’s common to start headings with a number and a period (e.g., “1. Introduction”). However, if you decide to remove these numbered prefixes from your H3 headings for a cleaner look, manually editing each heading can be time-consuming, especially in large documents.
Thankfully, Google Apps Script offers a straightforward way to automate this task. In this guide, we’ll create an Apps Script that searches for H3 headings in your Google Doc and removes any preceding numbers followed by a period (e.g., “1.”, “2.”, etc.) from each heading.
Why Google Apps Script?
Google Apps Script is a powerful tool that lets you extend and automate tasks across Google Workspace. By using Apps Script, you can create custom functionality in Google Docs, Sheets, Gmail, and more. Here, we’ll leverage Apps Script to identify H3 headings and remove any numeric prefix for a cleaner, more polished document format.
Apps Script to Remove Numbered Prefixes from H3 Headings
Below is the code you need to set up this functionality in Google Docs. The script identifies H3 headings and removes any prefix that consists of a number followed by a period (e.g., “1.”, “2.”).
function removeNumberedPrefixFromH3() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
paragraphs.forEach(paragraph => {
// Check if the paragraph is an H3 heading
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
const text = paragraph.getText();
// Regular expression to match a leading number with a period (e.g., "1. ")
const updatedText = text.replace(/^\d+\.\s*/, "");
// Update the paragraph with the modified text
paragraph.setText(updatedText);
}
});
}
How the Script Works
- Retrieve Document Content: The script accesses the active Google Doc and retrieves its body content.
- Identify H3 Headings: The
getHeading()
function checks if each paragraph is styled as an H3 heading. - Regular Expression Match: For paragraphs identified as H3 headings, the script uses a regular expression (
/^\d+\.\s*/
) to detect and remove a prefix that consists of one or more digits, followed by a period and any whitespace. - Update the Heading Text: If the prefix is detected, it’s removed, and the cleaned text is set as the new content for the paragraph.
Setting Up the Script
To use this script in Google Docs, follow these steps:
- Open Google Docs: Open the Google Doc where you want to remove numbered prefixes from H3 headings.
- Open Google Apps Script:
- Click on Extensions > Apps Script.
- This will open the Apps Script editor in a new tab.
- Add the Code: Copy the script code above and paste it into the editor.
- Save and Run:
- Save the project with a name like “Remove Numbered Prefixes from H3”.
- Select
removeNumberedPrefixFromH3
from the dropdown list in the toolbar and click the Run button. - When prompted, authorize the script to access your Google Docs.
Example Use Case
Suppose you have a document with H3 headings like this:
1. Introduction
2. Methodology
3. Results
After running the script, the headings will be transformed to:
Introduction
Methodology
Results
This script is perfect for documents where you want a more streamlined format without manually editing each heading.
Customizing the Script
You can easily modify the script to work with other heading levels. For example:
- To target H2 headings, change
DocumentApp.ParagraphHeading.HEADING3
toDocumentApp.ParagraphHeading.HEADING2
. - To handle different numbering styles, you can adjust the regular expression to match other patterns, such as letters (e.g., “A.”).
Conclusion
With just a few lines of Google Apps Script, you can automate the removal of numbered prefixes from H3 headings in Google Docs, saving time and ensuring a consistent, professional document format. This script can be a valuable tool for anyone working with structured documents, from writers to project managers and editors.
