Update Docs page elements update finding a H3 and combine it with the next paragraph

function udpateQuestion() {
  var doc = DocumentApp.getActiveDocument(); // Get the active document
  var body = doc.getBody(); // Get the body of the document
  var paragraphs = body.getParagraphs(); // Get all paragraphs in the document

  // Iterate through each paragraph in the document
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i]; // Get the current paragraph

    // Checks if the paragraph style is Heading 3
    if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {

      // Get the index of the next element in the document
      var nextElementIndex = body.getChildIndex(paragraph) + 1;

      // Get the next element using the index
      var nextElement = body.getChild(nextElementIndex);

      // Log the type of the next element (for debugging purposes)
      Logger.log(nextElement.getType());

      // Check if the next element is of type PARAGRAPH
      if (nextElement.getType() === DocumentApp.ElementType.PARAGRAPH) {

        // Get the text content of the next paragraph
        var nextText = nextElement.asParagraph().getText();

        // Set the text content of the current heading 3 paragraph to be the same as the next paragraph
        paragraph.setText(nextText);

        // Remove the original next element (the next paragraph)
        body.removeChild(nextElement);
      }
    }
  }
}

Here’s a breakdown of how the code works:

  1. It starts by getting the active document and the body of the document using DocumentApp.getActiveDocument() and getBody().
  2. It retrieves all the paragraphs in the document using getParagraphs() and stores them in the paragraphs array.
  3. The script then iterates through each paragraph in the paragraphs array.
  4. For each paragraph, it checks if the paragraph style is set to “Heading 3” using paragraph.getHeading(). This is likely used to identify specific sections of the document.
  5. If the paragraph is indeed a Heading 3, it proceeds to work with the next element in the document.
  6. It calculates the index of the next element in the document using body.getChildIndex(paragraph) + 1.
  7. It retrieves the next element using the calculated index and logs its type using Logger.log(nextElement.getType()). This is for debugging purposes to see the type of the next element.
  8. It checks if the next element is of type PARAGRAPH using nextElement.getType() === DocumentApp.ElementType.PARAGRAPH.
  9. If the next element is indeed a paragraph, it gets the text content of the next paragraph using nextElement.asParagraph().getText().
  10. It then sets the text content of the current Heading 3 paragraph to be the same as the next paragraph using paragraph.setText(nextText).
  11. Finally, it removes the original next element (the next paragraph) from the document using body.removeChild(nextElement).

In summary, this script is designed to find Heading 3 paragraphs in the document, take the content of the next paragraph, and replace the content of the Heading 3 paragraph with it, effectively merging the two paragraphs while removing the original next paragraph.

function udpateQuestion() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    // Checks if the paragraph style is Heading 1
    if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
    
      var nextElementIndex = body.getChildIndex(paragraph) + 1;
      var nextElement = body.getChild(nextElementIndex);
      Logger.log(nextElement.getType());
        if (nextElement.getType() === DocumentApp.ElementType.PARAGRAPH) {
          var nextText = nextElement.asParagraph().getText();
          paragraph.setText(nextText);
              body.removeChild(nextElement); // Remove the original next element
            }
    }
  }
}