Coding level: Intermediate
Duration: 20 minutes
Project type: Editor add-on
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the solution.
- Set up the script.
- Run the script.
About this solution
Clean up spreadsheet data by automatically removing empty rows and columns, cropping the spreadsheet to the edges of the data range, and filling in gaps in the data.
How it works
The script performs the following functions:
- Delete blank rows–Within a selected range, the script identifies empty rows and deletes them. If cells within a row contain space characters, the row isn't considered empty.
- Delete blank columns–Within a selected range, the script identifies empty columns and deletes them. If cells within a column contain space characters, the column isn't considered empty.
- Crop sheet to data range–The script identifies where the data range ends and deletes the excess rows and columns.
- Fill in blank rows–The script copies and pastes the content of the selected active cell to the empty cells in the rows below it. The script stops pasting content when it encounters a row that isn't empty or reaches the end of the data range.
Apps Script services
This solution uses the following service:
- Spreadsheet Service–Gets the active sheet and performs all of the cleanup functions.
Prerequisites
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts might require administrator approval).
- A web browser with access to the internet.
Set up the script
- Click the following button to open the Clean sheet Apps Script project.
Open the project - Click Overview .
- On the overview page, click Make a copy .
- At the top of your copied project, click Deploy > Test deployments.
- Next to Select type, click Enable deployment types > Editor add-on.
- Click Create new test.
- Under Test document, click No document selected.
- Choose a spreadsheet with data to clean up and click Insert. To use a sample document, make a copy of the Sample cleanup data spreadsheet.
- Click Save test.
- To open the spreadsheet, select the radio button next to the saved test and click Execute.
Run the script
- In the spreadsheet, select the range
A1:F20
. - Click Extensions > Copy of Clean sheet > Delete blank rows.
- When prompted, click Continue and authorize the script.
- Click Extensions > Copy of Clean sheet > Delete blank rows again.
- Click Extensions > Copy of Clean sheet > Delete blank columns.
- Click Extensions > Copy of Clean sheet > Crop sheet to data range.
- Select cell
C7
. - Click Extensions > Copy of Clean sheet > Fill in blank rows below.
Review the code
To review the Apps Script code for this solution, click View source code below:
View source code
Code.gs
Menu.gs
Contributors
This sample is maintained by Google with the help of Google Developer Experts.