To create a Google Apps Script that finds any element in a Google Docs document that starts with a number and ends with a colon “:” and updates that element to an H3 type, you can use the following script:
function updateElementsToH3() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.getText();
var elements = body.getParagraphs();
// Regular expression to match elements starting with a number and ending with ":"
var regex = /^\d+.*:$/;
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var elementText = element.getText();
// Check if the element matches the regex pattern
if (regex.test(elementText)) {
// Create a new H3 element and set its text
var h3 = body.insertParagraph(i + 1, '');
h3.setText(elementText);
h3.setHeading(DocumentApp.ParagraphHeading.HEADING3);
// Remove the original element
element.removeFromParent();
}
}
}
Here’s how to add and run this script in Google Docs:
- Open your Google Docs document.
- Click on “Extensions” in the top menu.
- Select “Apps Script” to open the Google Apps Script editor.
- Paste the provided script into the editor.
- Save the script with a name, e.g., “Update Elements to H3.”
- Close the script editor.
Now, you can run the script by following these steps:
- Click on “Extensions” again in the top menu.
- Select your script name, e.g., “Update Elements to H3,” from the list.
- Click the play button (▶️) to execute the script.
The script will scan your document for elements that start with a number and end with “:” and update them to H3 headings. Please make sure to save your document before running the script, as it will make changes to the document’s content.
