How to remove multiple lines that contain text using Google Apps Script Doc clean up

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

  var valuesToRemove = ["Copy code", "javascript", "html"]; // Replace with the values you're looking for

  for (var i = paragraphs.length - 1; i >= 0; i--) {
    var text = paragraphs[i].getText().trim();
    if (valuesToRemove.includes(text)) {
      body.removeChild(paragraphs[i]);
    }
  }
}
function removeBlankLines() {
 var doc = DocumentApp.getActiveDocument();
 var body = doc.getBody();
 var paragraphs = body.getParagraphs();

 for (var i = paragraphs.length - 2; i >= 0; i--) {
 var text = paragraphs[i].getText().trim();
 if (text === '') {
 body.removeChild(paragraphs[i]);
 }
 }
}

provided code in detail. It consists of two functions: removeMultipleSpecificLines and removeBlankLines. These functions are designed to manipulate the content of a Google Docs document.

removeMultipleSpecificLines()

Explanation:

  1. var doc = DocumentApp.getActiveDocument();: This line gets the currently active Google Docs document using DocumentApp.getActiveDocument() and stores it in the doc variable.
  2. var body = doc.getBody();: It retrieves the body of the document using getBody() method and stores it in the body variable.
  3. var paragraphs = body.getParagraphs();: This line gets all paragraphs in the document’s body and stores them in the paragraphs array.
  4. var valuesToRemove = ["Copy code", "javascript", "html"];: An array called valuesToRemove is defined, containing specific text values that you want to remove from the document. You can customize this array to include any text values you want to target for removal.
  5. The for loop iterates through all paragraphs in reverse order (from the last paragraph to the first) using for (var i = paragraphs.length - 1; i >= 0; i--).
  6. var text = paragraphs[i].getText().trim();: Within the loop, it gets the text content of the current paragraph, removes any leading or trailing whitespace using .trim(), and stores it in the text variable.
  7. if (valuesToRemove.includes(text)) {: It checks if the text matches any value in the valuesToRemove array.
  8. If a match is found, it removes the paragraph using body.removeChild(paragraphs[i]);. This effectively removes the entire paragraph containing the matching text from the document.

removeBlankLines()

Explanation:

This function is similar to the previous one but is specifically designed to remove blank lines (paragraphs with no content) from the document.

  1. It gets the document, body, and paragraphs in the same way as the previous function.
  2. The for loop iterates through all paragraphs in reverse order (excluding the last one) using for (var i = paragraphs.length - 2; i >= 0; i--).
  3. It gets the text content of the current paragraph, removes any leading or trailing whitespace using .trim(), and stores it in the text variable.
  4. It checks if the text is empty (blank) using if (text === '').
  5. If a blank line is found, it removes the paragraph using body.removeChild(paragraphs[i]);.

These two functions can be used to clean up a Google Docs document by removing specific lines with certain text values or removing entirely blank lines.