Chapter 3 Case study: tidying the monthly sales sheet ・ MISSION 10

Mission 10: Delete blank rows from the bottom

📋 Mission list

Task

The October details you pasted contain blank rows. Delete all of them. When a row is deleted, the rows below move up, so check the rows from the bottom.

October sales - Google Sheets
October sales

For practice, the table goes back to its starting state every time before the code runs (a real spreadsheet doesn't go back. Script changes can sometimes be undone with Ctrl+Z, but not all at once).

Apps ScriptUntitled projectDeploy ▾
function deleteBlankRows() {
const sheet = SpreadsheetApp.getActiveSheet();
for (let row = sheet.getLastRow(); row >= 2; row) {
if (sheet.getRange(row, 1).getValue() === ) {
sheet.(row);
}
}
}
Ctrl+S: save · Ctrl+R: run (editor) · F5: reopen

Chapter 3 startsCase study: tidying the monthly sales sheet

Chapter 3 ・ MISSION 10

Delete blank rows from the bottom

The October details you pasted contain blank rows. Delete all of them. When a row is deleted, the rows below move up, so check the rows from the bottom.

Now
Goal

Reading the code

for (let row = sheet.getLastRow(); row >= 2; row--) {
Repeats from the last row up to row 2, decreasing row by 1.
if (sheet.getRange(row, 1).getValue() === '') {
If column A is empty (''). === checks whether they are "the same, including type".
sheet.deleteRow(row);
Deletes row "row". The rows below move up.

When you delete a row, the rows below each move up by one. If you delete from the top, you skip rows that have moved up. Going from bottom to top keeps the numbers of rows you haven't checked yet unchanged. Deleting or adding rows is done from the bottom.

How to get started with GAS and common code are covered in an article.What is GAS? Getting started and common code

The code you write runs in an isolated place inside this page. It can't reach outside the page or other sites.

* This is a simulation of how Google Sheets and Apps Script behave and may differ in some ways from the real thing.

* Google Sheets is a trademark of Google LLC. This site is not affiliated with Google.