Append after data
const row = sheet.getLastRow() + 1; sheet.getRange(row, 1, 1, 4).setValues([[id, name, email, date]]) writes the next open row.
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.
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.
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.
Call sheet.getLastRow() on a Sheet object from SpreadsheetApp.
Returns 0 on an empty sheet. First data row on a blank sheet is row 1 when you write.
Pair with getLastColumn when you need a full data rectangle.
For append, next row is often getLastRow() + 1 unless you always keep a buffer row.
getRange(2, 1, getLastRow() - 1, numCols) skips a header when row 1 is headers only.
If users leave formatted blank rows below data, getLastRow may include them if any cell has content.
const row = sheet.getLastRow() + 1; sheet.getRange(row, 1, 1, 4).setValues([[id, name, email, date]]) writes the next open row.
MailApp sends 'Rows today: ' + (sheet.getLastRow() - 1) assuming row 1 is a header.
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.
Watch how getLastRow works
Guides that explain getLastRow in more depth.
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 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 →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 →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 →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 →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 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.