Google Apps Script that selects all Heading 3 paragraphs and updates them if the first text matches

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:

  1. Open your Google Docs document.
  2. Click on Extensions in the menu, then Apps Script.
  3. Copy and paste the script above into the script editor.
  4. Save the script and name your project.
  5. Run the function updateH3Paragraphs from within the script editor.
  6. 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.