Automating Line Removal in Google Docs using Google Apps Script

Automating Line Removal in Google Docs using Google Apps Script

Google Apps Script offers an excellent way to automate tasks in Google Docs, making document management more efficient. One practical application is removing specific lines within paragraphs that contain certain phrases. In this blog post, we’ll walk through creating a script to remove lines containing “Copy code” or “JavaScript” if they appear on their own line within a paragraph.

Overview of the Script

The script we will discuss is designed to find and remove lines containing the phrases “Copy code” or “JavaScript” if these phrases are the only content on the line. This can be particularly useful for cleaning up instructional documents or code samples.

The Code

Here is the complete script:

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

var phrasesToRemove = ['Copy code', 'JavaScript']; // Add phrases to remove here

for (var i = paragraphs.length - 1; i >= 0; i--) {
var text = paragraphs[i].getText().trim();
var lines = text.split(/\r?\n/);

var newLines = lines.filter(function(line) {
var trimmedLine = line.trim();
return !phrasesToRemove.includes(trimmedLine);
});

if (newLines.length !== lines.length) {
paragraphs[i].setText(newLines.join('\n'));
}
}
}

How It Works

  1. Access the Document and Body: The script begins by accessing the active document and its body.
  2. Retrieve Paragraphs: It retrieves all the paragraphs in the document using getParagraphs().
  3. Define Phrases to Remove: An array, phrasesToRemove, contains the phrases that you want to remove if they are on their own line.
  4. Iterate Through Paragraphs: The script iterates through all the paragraphs in reverse order to ensure that removing lines does not affect the iteration process.
  5. Split Paragraphs into Lines: For each paragraph, the script splits the text into lines based on newline characters.
  6. Filter Lines: It filters out lines that contain only the specified phrases.
  7. Update Paragraph Text: If any lines were removed, the paragraph text is updated with the remaining lines.

Running the Script

To use this script, follow these steps:

  1. Open Your Google Document: Open the Google Document where you want to remove lines containing “Copy code” or “JavaScript” if they are on their own line.
  2. Access Apps Script: Go to Extensions > Apps Script.
  3. Enter the Script: Delete any existing code in the script editor and replace it with the script provided above.
  4. Save the Script: Save the script (File > Save).
  5. Run the Script: To remove the phrases, run the removeWordsOnOwnLine function by clicking the play button or selecting Run > Run function > removeWordsOnOwnLine.

Conclusion

By using this script, you can efficiently automate the removal of specific lines from your Google Documents. This can be especially helpful for cleaning up documents and ensuring they adhere to desired formatting and content standards. Feel free to customize the phrasesToRemove array to include any words or phrases you want to target.