Apps Script to update paragraphs that end in question mark first sentance as H3 with order intact

Title: Automatically Convert Questions to H3 Headings in Google Docs

Have you ever found yourself writing a lengthy document in Google Docs, only to realize later that you need to format questions differently to make them stand out? If so, you’ll appreciate this handy Google Apps Script function called convertQuestionToH3(). In this post, we’ll dive into how this function works and how you can use it to streamline your document editing process.

What does convertQuestionToH3() do?

Let’s break down the code:

function convertQuestionToH3() {
  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  
  // Iterate through paragraphs in reverse order
  for (var i = paragraphs.length - 1; i >= 0; i--) {
    var paragraph = paragraphs[i];
    var text = paragraph.getText();
    
    // Split the paragraph into sentences while preserving punctuation marks
    var sentences = text.match(/[^.!?]+[.!?]+/g);
    
    if (sentences && sentences.length > 0) {
      // Get the first sentence
      var firstSentence = sentences[0].trim();
      
      // Check if the first sentence ends with a question mark
      if (firstSentence.slice(-1) === '?') {
        // Insert the H3 paragraph below the current paragraph
        var h3Paragraph = body.insertParagraph(i + 1, "");
        h3Paragraph.setText(firstSentence);
        h3Paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
        
        // Insert the rest of the paragraph as normal text
        var restOfText = text.substring(firstSentence.length).trim();
        if (restOfText !== "") {
          body.insertParagraph(i + 2, restOfText);
        }
        
        // Remove the original paragraph
        body.removeChild(paragraph);
      }
    }
  }
}


Here’s a breakdown of what each part of the code does:

  1. Accessing Document Body: The function begins by obtaining the body of the active Google Docs document.
  2. Iterating Through Paragraphs: It then loops through each paragraph in the document, starting from the end and moving backward. This reverse order ensures that newly inserted content won’t affect the iteration.
  3. Splitting Paragraphs into Sentences: Each paragraph is split into sentences while preserving punctuation marks.
  4. Identifying Questions: The function checks if the first sentence of the paragraph ends with a question mark.
  5. Converting to H3 Heading: If a question is identified, the function inserts an H3 heading containing the question. It then inserts the remaining part of the paragraph as regular text below the heading.
  6. Removing Original Paragraph: Finally, the original paragraph containing the question is removed from the document.

How to Use convertQuestionToH3()

To use this function:

  1. Open your Google Docs document.
  2. Click on Extensions -> Apps Script.
  3. Paste the provided code into the script editor.
  4. Save the script.
  5. Run the convertQuestionToH3() function by clicking the play button or going to Run -> Run function -> convertQuestionToH3.

Conclusion

With the convertQuestionToH3() function, you can effortlessly format questions in your Google Docs document as H3 headings, making them more prominent and organized. This script is just one example of how Google Apps Script can automate repetitive tasks and enhance your productivity in Google Docs. Give it a try and see how it simplifies your document editing workflow!