What is getLastRow in Google Sheets Apps Script?

getLastRow returns the position of the last row on a sheet that contains any content in any column. Scripts use it to find where to append new data, size a getRange read, or loop only through populated rows instead of a million blank grid lines.

When to use it

Use getLastRow before appendRow, when building dynamic ranges like A2:E lastRow, or when logging how many records exist on a Responses tab after form submits.

When to skip it

Skip getLastRow alone when blank rows sit in the middle of your table. It measures the bottom edge of the sheet footprint, not logical record count in a messy table.

How it works

  1. 1

    Call sheet.getLastRow() on a Sheet object from SpreadsheetApp.

  2. 2

    Returns 0 on an empty sheet. First data row on a blank sheet is row 1 when you write.

  3. 3

    Pair with getLastColumn when you need a full data rectangle.

  4. 4

    For append, next row is often getLastRow() + 1 unless you always keep a buffer row.

  5. 5

    getRange(2, 1, getLastRow() - 1, numCols) skips a header when row 1 is headers only.

  6. 6

    If users leave formatted blank rows below data, getLastRow may include them if any cell has content.

Examples in Google Sheets

Append after data

const row = sheet.getLastRow() + 1; sheet.getRange(row, 1, 1, 4).setValues([[id, name, email, date]]) writes the next open row.

Nightly count

MailApp sends 'Rows today: ' + (sheet.getLastRow() - 1) assuming row 1 is a header.

Read only data rows

sheet.getRange(2, 1, sheet.getLastRow() - 1, 7).getValues() loads the table without trailing empty sheet rows beyond lastRow.

Build this without starting from a blank cell

Use a Better Sheets tool for getLastRow, then watch a walkthrough when you want the full pattern.

Better Sheets resources

Common mistakes

  • Subtracting 1 for header when getLastRow is 0 on empty sheet, producing negative range sizes.
  • Expecting getLastRow to ignore blank rows in column A while other columns have stray values far down.
  • Using getLastRow on the wrong tab in a multi-sheet file.
  • Confusing getLastRow with getMaxRows, which is the grid limit, not data extent.
  • Two triggers appending at the same time without LockService, causing duplicate row numbers.

Frequently asked questions

What is the difference between getLastRow and getMaxRows?
getLastRow is the last row with content. getMaxRows is total rows in the sheet grid, mostly empty.
Does getLastRow count the header?
It counts any row with content. If row 1 is headers, lastRow includes it. Adjust math when you mean data rows only.
What if column A has gaps?
getLastRow still returns the lowest row with any content in any column on that sheet. Use QUERY or filter logic if you need stricter rules.
How does getLastRow work with appendRow?
appendRow adds after the current table and updates last row automatically. You often do not need getLastRow right before appendRow.
Can getLastRow be 0?
Yes on a completely empty sheet. Guard before getRange math.
Is there getLastColumn?
Yes. sheet.getLastColumn() mirrors the idea for the rightmost column with content.
Why is my dynamic range empty?
Check header subtraction, wrong sheet, or lastRow 0. Log lastRow in Executions.
Does deleteRow change getLastRow?
Yes. Deleting the bottom row lowers lastRow when that row was the previous last.

Related Tutorials

Watch how getLastRow works

Browse more tutorials
Automate Google Sheets With Zero Experience

Automate Google Sheets With Zero Experience

By the end of this video you should be knowledgeable about how to automa...
Every Google Drive File Created in Last 24 Hours

Every Google Drive File Created in Last 24 Hours

Create a simple report in a Google Sheet that documents every file in yo...
Hold a Giveaway Raffle in a Google Sheet

Hold a Giveaway Raffle in a Google Sheet

Get a random winner from a list of names. Also great to randomize tasks ...
Scrape Google Maps

Scrape Google Maps

Scrape any query in Google Maps into your Google Sheets. Enter your API ...
Convert Google Sheets into a REST API

Convert Google Sheets into a REST API

How to use Google Sheets as a database by turning it into a REST API wit...
How to Insert Multiple Rows in Google Sheets (Easy to Hard)

How to Insert Multiple Rows in Google Sheets (Easy to Hard)

Learn how to add multiple rows to your google sheets by hand, and automa...

Related blog posts

Guides that explain getLastRow in more depth.

Browse the blog

Related glossary terms

Done reading about getLastRow?

Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.

Need this once

Jump to a free tool or a single tutorial for this topic.

Learning Sheets for real

Unlock the full library, generators, and templates with membership.