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
Call sheet.getLastRow() on a Sheet object from SpreadsheetApp.
-
2
Returns 0 on an empty sheet. First data row on a blank sheet is row 1 when you write.
-
3
Pair with getLastColumn when you need a full data rectangle.
-
4
For append, next row is often getLastRow() + 1 unless you always keep a buffer row.
-
5
getRange(2, 1, getLastRow() - 1, numCols) skips a header when row 1 is headers only.
-
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.
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
Automate Google Sheets With Zero Experience
Every Google Drive File Created in Last 24 Hours
Hold a Giveaway Raffle in a Google Sheet
Scrape Google Maps
Convert Google Sheets into a REST API
How to Insert Multiple Rows in Google Sheets (Easy to Hard)
Related blog posts
Guides that explain getLastRow in more depth.
Automate Row Deletion in Google Sheets with Apps Script
In this post, we'll cover how to remove rows automatically in Google Sheets using Apps Script, ensuring you save time and streamline your spreadsheet management. You're probably already familiar with the manual way of right-clicking and deleting rows, but there's a more efficient way to handle this—automation! Manual versus Semi-Automatic Deletion You may know that you can manually delete rows in Google Sheets by right-clicking and selecting "Delete row." You can even select multiple rows...
Read post →Extract URLs from Google Sheets
Extract url from hyperlink google sheets.Skip the Command K. This Google Sheet tutorial will make it easier for you to extract URLS from Google Sheets.
Read post →Related terms
Apps Script loops
Loops in Apps Script let you repeat code over rows, columns, sheets, or API pages. You read ranges into arrays with getValues(), process each item in a for or forEach loop, then write results back in one setValues() call when possible. Batch reads and writes beat cell-by-cell updates for speed and quota limits.
Read guide →Google Sheets automation
Automation means work happens without repeating the same clicks every day: imports update, emails send, rows move, and dashboards refresh. In Sheets, automation usually stacks built-in features, Apps Script, and sometimes the Sheets API or add-ons. Start with the lightest tool that still solves the job.
Read guide →Script Editor
The Script Editor is the Apps Script workspace inside your spreadsheet where you write JavaScript that talks to Sheets, Gmail, and other Google services. You open it from Extensions > Apps Script. Every function you save can be run manually, bound to a trigger, or linked from a custom menu.
Read guide →Google Sheets workflows
A workflow is the path work takes through your sheet: intake, review, approval, done. Good workflows use consistent columns, statuses everyone understands, and just enough automation to move tasks forward without hiding steps from the team.
Read guide →Done reading about getLastRow?
Membership unlocks 600+ 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.