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:
var doc = DocumentApp.getActiveDocument();
: This line gets the currently active Google Docs document usingDocumentApp.getActiveDocument()
and stores it in thedoc
variable.var body = doc.getBody();
: It retrieves the body of the document usinggetBody()
method and stores it in thebody
variable.var paragraphs = body.getParagraphs();
: This line gets all paragraphs in the document’s body and stores them in theparagraphs
array.var valuesToRemove = ["Copy code", "javascript", "html"];
: An array calledvaluesToRemove
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.- The
for
loop iterates through all paragraphs in reverse order (from the last paragraph to the first) usingfor (var i = paragraphs.length - 1; i >= 0; i--)
. 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 thetext
variable.if (valuesToRemove.includes(text)) {
: It checks if thetext
matches any value in thevaluesToRemove
array.- 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.
- It gets the document, body, and paragraphs in the same way as the previous function.
- The
for
loop iterates through all paragraphs in reverse order (excluding the last one) usingfor (var i = paragraphs.length - 2; i >= 0; i--)
. - It gets the text content of the current paragraph, removes any leading or trailing whitespace using
.trim()
, and stores it in thetext
variable. - It checks if the
text
is empty (blank) usingif (text === '')
. - 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.