To create a Google Apps Script that selects all Heading 3 (H3) paragraphs in a Google Docs document, checks if they start with the text “question” (case-insensitive), and updates them to regular paragraphs if they do, you can use the following script:
function updateH3Paragraphs() {
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var paragraphs = body.getParagraphs();
paragraphs.forEach(function(paragraph) {
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
var text = paragraph.getText();
if (text.toLowerCase().startsWith("question")) {
paragraph.setHeading(DocumentApp.ParagraphHeading.NORMAL);
}
}
});
}
How to Use This Script:
- Open your Google Docs document.
- Click on
Extensions
in the menu, thenApps Script
. - Copy and paste the script above into the script editor.
- Save the script and name your project.
- Run the function
updateH3Paragraphs
from within the script editor. - Grant the necessary permissions when prompted.
What the Script Does:
- It loops through all paragraphs in the document.
- Checks if a paragraph is styled as Heading 3.
- If the paragraph text starts with “question” (case-insensitive), it changes the paragraph heading to normal text.
Tips and Considerations:
- This script checks for the word “question” at the start of the paragraph regardless of what follows it. If you need a more specific check (e.g., “question” as a whole word), the script may require additional string handling.
- Always test scripts on a copy of your document to prevent unintended changes.
- The script runs on the entire document. If you wish to run it on a specific section, additional modifications are needed.