Apps script for docs to check if the paragraph ends with a question mark if it does convert it to an h3

To create a Google Apps Script that checks if a paragraph in a Google Document ends with a question mark and if so, converts it to a heading 3, follow these steps:

  1. Open your Google Document.
  2. Go to Extensions > Apps Script.
  3. Delete any code in the script editor and paste the following code:j
function checkAndConvertParagraphs() {
  // Open the active document.
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();
  
  // Get all the paragraphs in the document.
  var paragraphs = body.getParagraphs();
  
  // Loop through all paragraphs.
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    var text = paragraph.getText();
    
    // Check if the paragraph ends with a question mark.
    if (text.endsWith('?')) {
      // Convert the paragraph to Heading 3.
      paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
    }
  }
}
  1. Save the script with a name, for example, “Convert Question Paragraphs”.
  2. Close the script editor.
  3. To run the script, you may need to reload your document. Then go to Extensions > Apps Script, select the function checkAndConvertParagraphs from the dropdown, and click the run icon.
  4. You might have to authorize the script the first time you run it by following the on-screen instructions.

This script will iterate through all paragraphs in your document, check if they end with a question mark, and if they do, the paragraph’s style will be changed to Heading 3. If you need to apply this script to new or modified paragraphs, you’ll need to run the script again.