Removing Specific Paragraphs in Google Docs using Google Apps Script
Google Apps Script provides a powerful way to automate and enhance the functionality of Google Docs. One common task you might encounter is the need to remove specific paragraphs that contain only certain phrases or single words. In this blog post, we’ll walk through how to create a script to achieve this, using the removeSpecificParagraphs
function.
Overview of the Script
The script we will discuss is designed to remove paragraphs that contain only the phrases “Copy code” or the single word “JavaScript.” Additionally, we include a helper function, logDocumentText
, to log the text of each paragraph for debugging purposes.
The Code
Here is the complete script:
function removeSpecificParagraphs() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var phrasesToRemove = ['Copy code', 'JavaScript'];
for (var i = paragraphs.length - 1; i >= 0; i--) {
var text = paragraphs[i].getText().replace(/\s+/g, ' ').trim();
Logger.log('Paragraph ' + i + ': "' + text + '"');
if (phrasesToRemove.includes(text)) {
body.removeChild(paragraphs[i]);
}
}
}
function logDocumentText() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var text = paragraphs[i].getText().replace(/\s+/g, ' ').trim();
Logger.log('Paragraph ' + i + ': "' + text + '"');
}
}
How It Works
- Access the Document and Body: The script begins by accessing the active document and its body. This is where all the paragraphs are stored.
- Retrieve Paragraphs: We retrieve all the paragraphs in the document using
getParagraphs()
. - Define Phrases to Remove: We define an array,
phrasesToRemove
, containing the phrases or words that we want to remove from the document. - Iterate Through Paragraphs: The script then iterates through all the paragraphs in reverse order (from the last paragraph to the first). Iterating in reverse ensures that removing a paragraph does not affect the iteration process.
- Normalize and Check Text: For each paragraph, the script retrieves the text, normalizes it by replacing multiple whitespace characters (including newlines) with a single space, and trims any leading or trailing spaces. The normalized text is then logged for debugging purposes.
- Remove Matching Paragraphs: If the normalized text matches any of the phrases in the
phrasesToRemove
array, the paragraph is removed from the document. - Logging Function: The
logDocumentText
function can be used to log the text of each paragraph before making changes. This can help ensure that the script is identifying the correct paragraphs.
Running the Script
To use this script, follow these steps:
- Open Your Google Document: Open the Google Document where you want to remove specific paragraphs.
- Access Apps Script: Go to
Extensions
>Apps Script
. - Enter the Script: Delete any existing code in the script editor and replace it with the script provided above.
- Save the Script: Save the script (
File
>Save
). - Run the Script: To remove the paragraphs, run the
removeSpecificParagraphs
function by clicking the play button or selectingRun
>Run function
>removeSpecificParagraphs
. - Check Logs: To view the logs and debug, run the
logDocumentText
function. Open theExecutions
tab in the Apps Script editor to see the logs.
Conclusion
By using this script, you can easily automate the removal of specific paragraphs from your Google Documents. This can be particularly useful for cleaning up documents or ensuring consistency in formatting and content. The logging functionality provides an additional layer of confidence, helping you verify that the script is working as intended before making any changes.
