Remove 2 blank paragraphs in Docs set to only one paragraph in a row to be blank


To create a Google Apps Script code in Google Docs that removes two consecutive blank elements (paragraphs) and replaces them with one blank element, you can use the following code. This script will iterate through the document and merge consecutive blank paragraphs into one:

function removeConsecutiveBlankParagraphs() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();

  // Check if there are at least two paragraphs
  if (paragraphs.length >= 2) {
    // Start the loop from the first paragraph and stop at the second-to-last paragraph
    for (var i = 0; i < paragraphs.length - 2; i++) {
      var currentParagraph = paragraphs[i];
      var nextParagraph = paragraphs[i + 1];
        // Check if both paragraphs are blank (empty)
        if (currentParagraph.getText() === '' && nextParagraph.getText() === '') {
          // Get the parent element (body) and remove the second consecutive blank paragraph
          const parentElement = nextParagraph.getParent();
          parentElement.removeChild(nextParagraph);
        }
    }
  }
}

Here’s how to add and run this script in Google Docs:

  1. Open your Google Docs document.
  2. Click on “Extensions” in the top menu.
  3. Select “Apps Script” to open the Google Apps Script editor.
  4. Paste the provided script into the editor.
  5. Save the script with a name, e.g., “Remove Consecutive Blanks.”
  6. Close the script editor.

Now, you can run the script by following these steps:

  1. Click on “Extensions” again in the top menu.
  2. Select your script name, e.g., “Remove Consecutive Blanks,” from the list.
  3. Click the play button (▶️) to execute the script.

The script will scan your document for consecutive blank paragraphs and merge them into one blank paragraph.

Please make sure to save your document before running the script, as it will make changes to the document’s content.