Introduction
If you work with Google Docs frequently, you’ve probably encountered documents with list items that contain nothing but blank spaces. These empty list items can clutter your document, disrupt its flow, and detract from readability. Manually cleaning them up can be time-consuming, especially in large documents. Fortunately, Google Apps Script provides a quick solution to remove these blank list items automatically, streamlining your document organization.
In this post, we’ll walk through a custom Google Apps Script that finds and removes empty list items from your Google Doc with just a single click.
Why Use Google Apps Script?
Google Apps Script is a powerful tool that lets you automate tasks across Google Workspace apps. With just a bit of JavaScript, you can customize and extend Google Docs, Sheets, and other Google services. In this tutorial, we’ll leverage Apps Script to remove blank list items, improving the document’s appearance and readability in seconds.
The Script to Remove Blank List Items
Here’s the code you need to eliminate any blank list items from a Google Doc:
function removeBlankListItems() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
paragraphs.forEach(paragraph => {
// Check if the paragraph is a list item and if it’s empty or whitespace
if (paragraph.getType() === DocumentApp.ElementType.LIST_ITEM && paragraph.getText().trim() === "") {
paragraph.removeFromParent();
}
});
}
How It Works
- Retrieve the Document: The
DocumentApp.getActiveDocument()
method fetches the active Google Doc where the script will run. - Access Document Body:
getBody()
provides access to the main body of the document, allowing us to inspect each line (or paragraph). - Check for Blank List Items: The script iterates through each paragraph and checks if the paragraph is a list item (
DocumentApp.ElementType.LIST_ITEM
) and if it’s empty or contains only whitespace (getText().trim() === ""
). - Remove Empty List Items: If a paragraph meets both criteria, it’s removed from the document using
removeFromParent()
.
Setting Up the Script
To use this script, follow these steps:
- Open Google Docs: Go to the Google Doc where you want to remove blank list items.
- Open Apps Script Editor:
- Click on Extensions > Apps Script to open the Apps Script editor in a new tab.
- Add the Code: Copy and paste the code above into the editor.
- Save the Script: Name the project (e.g., “Remove Blank List Items”) and save it.
- Run the Script:
- From the dropdown list in the toolbar, select
removeBlankListItems
and click the Run button. - Grant permission for the script to access your Google Docs as prompted.
- From the dropdown list in the toolbar, select
Example Use Case
Imagine you have a bulleted list in your Google Doc like this:
- First item
-
- Second item
-
Running this script will automatically remove the blank list items, leaving only the meaningful content:
- First item
- Second item
This can be especially useful for cleaning up imported or auto-generated content that includes unnecessary blank lines.
Customizing the Script
The code can be customized for specific needs:
- Only Target Certain Types of List Items: You could add conditions to focus on numbered or bulleted lists.
- Remove All Blank Paragraphs: If you want to remove any blank lines, not just list items, you can remove the list item check and only check for blank paragraphs.
Conclusion
With just a few lines of Google Apps Script, you can automate the cleanup of blank list items in your Google Docs. This quick, easy script helps maintain a cleaner, more organized document without the hassle of manual editing.
