Remove Specific Phrases in Google Docs Using Google Apps Script

When working with large Google Docs, you may need to remove specific phrases that occur as standalone lines. These could be placeholders, outdated notes, or boilerplate text that no longer applies. Manually deleting these lines can be a hassle, especially if they appear throughout the document. Luckily, Google Apps Script provides an easy way to automate this process!

In this post, we’ll explore a simple Google Apps Script that searches for specific phrases in a Google Doc and removes them if they’re the only content on their line. We’ll also make this script case-insensitive to ensure all variations of the phrase get removed in one go.

Why Use Google Apps Script?

Google Apps Script is a powerful tool that allows you to automate and customize tasks across Google Workspace apps. This script will help streamline your document cleanup by automatically removing unwanted lines. Whether you’re a writer, editor, or project manager, this quick automation can save time and reduce clutter in your documents.

Script to Remove Specific Phrases (Case-Insensitive)

Below is the code you’ll need to set up this functionality in Google Docs. It uses an array of phrases to identify and delete matching lines, regardless of case.

function removeSpecificPhrases() {
const phrasesToRemove = ["Phrase 1", "Phrase 2", "Another Phrase", "Sample Text"]; // Add phrases here
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();

paragraphs.forEach(paragraph => {
const text = paragraph.getText().trim();
// Check if the paragraph text (in lowercase) matches any of the phrases (also in lowercase)
if (phrasesToRemove.some(phrase => phrase.toLowerCase() === text.toLowerCase())) {
paragraph.removeFromParent();
}
});
}

How the Script Works

  1. Define Phrases to Remove: In the phrasesToRemove array, list each phrase you want to remove. Each phrase should be enclosed in quotes, separated by commas.
  2. Retrieve Document Content: The script gets the active Google Doc and iterates through each paragraph.
  3. Case-Insensitive Comparison: The script converts both the paragraph text and each phrase in phrasesToRemove to lowercase, ensuring that matches are found regardless of case.
  4. Remove Matching Paragraphs: If a paragraph’s text matches any phrase in the array, it’s removed from the document.

Setting Up the Script

To use this script in Google Docs, follow these steps:

  1. Open Google Docs: Open the Google Doc where you want to remove specific phrases.
  2. Access Google Apps Script:
    • Click on Extensions > Apps Script.
    • This will open the Apps Script editor in a new tab.
  3. Paste the Code: Copy the script code above and paste it into the editor.
  4. Run the Script:
    • From the dropdown list in the toolbar, select removeSpecificPhrases and click the Run button.
    • The script will prompt you to grant permission to access your Google Docs—accept to proceed.

Example Use Case

Imagine you have a document with lines like these:

Phrase 1
Some other text
phrase 2
Unwanted line
Another Phrase
sample text

After running this script, all standalone lines that match any of the phrases (in any case format) will be removed, leaving only the other content. This feature is particularly useful if the document has been created collaboratively and includes placeholders or common terms that are no longer relevant.

Customizing the Script

You can easily modify this script to suit your needs:

  • Add More Phrases: Expand the phrasesToRemove array by adding more phrases in quotes.
  • Specific Case Requirements: If case sensitivity is needed, simply remove the toLowerCase() calls from the condition.

Conclusion

With just a few lines of Google Apps Script, you can automate the removal of specific phrases from Google Docs, saving time and helping keep your documents clean and focused. This script is easy to use, customizable, and ensures consistency, especially for large or frequently edited documents.