Remove Blank Lines in Google Docs with Google Apps Script

Blank lines in a document can clutter its appearance, making it look disorganized and harder to read. Manually going through a long document to remove these blank lines can be tedious, especially if you’re working with large or collaboratively edited files. Thankfully, Google Apps Script offers a quick solution to automatically detect and remove empty lines, streamlining the cleanup process.

In this post, we’ll walk you through a simple Google Apps Script that identifies and removes any blank lines in your Google Doc, leaving a neat and polished result.

Why Use Google Apps Script?

Google Apps Script is a versatile tool that allows you to automate tasks and customize Google Workspace apps. This script helps improve document readability by removing all unnecessary blank lines, creating a cleaner and more professional look without manual editing.

Apps Script to Remove Blank Lines

Below is the Apps Script code to remove empty lines in your Google Doc.

function removeBlankLines() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();

paragraphs.forEach(paragraph => {
const text = paragraph.getText().trim();
// Check if the paragraph is empty after trimming whitespace
if (text === "") {
paragraph.removeFromParent();
}
});
}

How the Script Works

  1. Access the Document: The script retrieves the active Google Doc and accesses its main body content.
  2. Loop Through Paragraphs: It iterates over each paragraph in the document.
  3. Detect Blank Lines: For each paragraph, the script trims any whitespace using trim() and checks if the remaining text is empty.
  4. Remove Empty Paragraphs: If the paragraph is blank, it is removed from the document using removeFromParent().

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 blank lines.
  2. Access Google Apps Script:
    • Go to Extensions > Apps Script to 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. Save and Run:
    • Save your project with a relevant name, such as “Remove Blank Lines.”
    • Select removeBlankLines from the dropdown and click the Run button.
  5. Authorize Permissions: When prompted, authorize the script to make changes to your Google Docs.

Example Use Case

Imagine your document has unnecessary blank lines scattered throughout, like this:

Section Title

Some content here.


Another paragraph with extra blank lines.


Conclusion

After running the script, all blank lines are removed, resulting in a neatly formatted document:

Section Title
Some content here.
Another paragraph with extra blank lines.
Conclusion

Customizing the Script

If you only want to remove consecutive blank lines but leave some intentional spacing, you can adjust the script to skip specific sections. For example, you could add conditions based on keywords or specific paragraph styles if needed.

Conclusion

With just a few lines of Google Apps Script, you can clean up blank lines in Google Docs, making it look professional and easy to read. This script is ideal for anyone looking to tidy up their documents without manually editing each line.