Managing text-heavy documents often requires tedious manual edits, especially when dealing with recurring unwanted lines. If you’ve repeatedly deleted lines starting with a specific phrase, it’s time to automate the process! With Google Apps Script, you can streamline this task in Google Docs and save valuable time.
In this blog post, we’ll walk you through creating an Apps Script that removes all lines in a Google Docs document starting with the phrase:
“Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API.”
What is Google Apps Script?
Google Apps Script is a JavaScript-based platform that allows you to extend and automate Google Workspace apps like Docs, Sheets, and Gmail. It’s ideal for customizing workflows and performing repetitive tasks with ease.
Step-by-Step Guide: Removing Specific Lines from a Google Docs Document
Step 1: Open the Apps Script Editor
- Open your Google Doc.
- Navigate to
Extensions
>Apps Script
. - This opens the Apps Script editor in a new tab.
Step 2: Write the Script
Copy and paste the following script into the Apps Script editor:
function removeSpecificLines() {
const doc = DocumentApp.getActiveDocument(); // Access the current Google Doc
const body = doc.getBody(); // Get the document's body content
const targetPhrase = "Pattern to Find"; // Phrase to match
const paragraphs = body.getParagraphs(); // Get all paragraphs in the document
for (let i = paragraphs.length - 1; i >= 0; i--) { // Loop backwards to avoid skipping indices
const text = paragraphs[i].getText(); // Get text of each paragraph
if (text.startsWith(targetPhrase)) { // Check if the paragraph starts with the target phrase
if (i === paragraphs.length - 1) {
// If it's the last paragraph, set its text to a single space
paragraphs[i].setText(' ');
} else {
// Otherwise, remove the paragraph
body.removeChild(paragraphs[i]);
}
}
}
Logger.log('All matching lines have been removed or cleared.');
}
Step 3: Save and Test the Script
- Click the save icon and name your project (e.g.,
RemoveLinesScript
). - From the toolbar, select the function
removeSpecificLines
and click the play ▶️ button. - Grant the necessary permissions when prompted.
- Once executed, the script will scan the document and remove all lines starting with the target phrase.
How It Works
getBody()
: Accesses the document’s content.getParagraphs()
: Retrieves all the paragraphs in the document.startsWith()
: Checks if a paragraph starts with the specified phrase.removeChild()
: Deletes the identified paragraph from the document.
By iterating through all the paragraphs in reverse order, the script ensures no lines are skipped when removing multiple consecutive matches.
Use Cases
This script is highly versatile and can be adapted to:
- Remove disclaimers, watermarks, or autogenerated notes.
- Clean up repetitive boilerplate text.
- Preprocess documents for further analysis or formatting.
Customization Tips
- Change the Target Phrase: Replace
targetPhrase
with any string you wish to target. - Expand Matching: Modify the condition to include partial matches or case-insensitive searches using
includes()
ortoLowerCase()
.
Automate Further with Triggers
Set up a trigger to run this script automatically:
- Go to the Apps Script editor.
- Click on the clock icon ⏰ in the toolbar.
- Create a trigger to execute
removeSpecificLines
on document open, edit, or at specific intervals.