Scriptdrill

Reading and writing sheets Updated November 2026

getLastRow

sheet.getLastRow() returns the row number of the last row that has content in any column (0 on an empty sheet); sheet.getLastColumn() does the same for columns. It counts formulas that return empty text and formatted cells with values, so for the last filled row of one column read that column and scan upward.

Syntax

sheet.getLastRow() | sheet.getLastColumn() | sheet.getMaxRows()
PartWhat it is
getLastRow()Last row with content anywhere on the sheet
getMaxRows()Total rows in the grid, including empty ones
getDataRange()Range from A1 to the last row and column with content

Examples

const sheet = SpreadsheetApp.getActiveSheet();
const last = sheet.getLastRow();
Logger.log(last);

Result: Row number of the last entry, for example 57

const data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 3).getValues();

Result: All rows below the header in columns A to C

function lastRowInColumn(sheet, col) {
  const values = sheet.getRange(1, col, sheet.getMaxRows(), 1).getValues();
  for (let i = values.length - 1; i >= 0; i--) {
    if (values[i][0] !== "") return i + 1;
  }
  return 0;
}
Logger.log(lastRowInColumn(sheet, 2));

Result: Last filled row in column B, for example 31, even if column A goes further

const next = sheet.getLastRow() + 1;
sheet.getRange(next, 1).setValue("New entry");

Result: Writes into the first empty row below the data

Common mistakes

  • getLastRow returns a number far below the data because of a stray value or formula in a far column: clear the stray cell, or use the column scan above.
  • getLastRow returns 1 on a sheet with data: the sheet variable points to the wrong tab, so check getSheetByName("Name") returned the sheet you expect.
  • Formulas like =IF(A2="","",B2) in a long column push getLastRow to the end of the formulas: scan the column with a filter on non-empty display values instead.
  • getRange(2, 1, getLastRow() - 1, n) throws on an empty sheet because numRows is 0 or negative: return early when getLastRow() < 2.

Practice getLastRow

These drills check your answer on live data, free, no account needed.

  • Items in stockEach item looks like `{ name, qty }`. Return the items that are in stock, meaning `qty` is greater than 0, in their original order. A negative `qty` is a back order, not stock.

Questions

What is the difference between getLastRow and getMaxRows?

getLastRow is the last row with content; getMaxRows is the number of rows the sheet has in total, including blank ones at the bottom (often 1000).

How do I get the first empty row?

sheet.getLastRow() + 1 for the first row after all content, or for one column use the scan function and add 1.

Related

  • appendRowAdd a row to a Google Sheet with Apps Script: appendRow for a single row after the last row with data, and setValues for many rows at once, with examples.
  • getRange and getValuesRead Google Sheets data with Apps Script: getRange by A1 notation or row and column numbers, then getValue for one cell or getValues for a 2D array of rows.
  • Loop through rowsLoop through rows in Google Sheets with Apps Script: read the data once with getValues, loop the 2D array with for, for...of or forEach, then write back.
  • Delete rowsDelete rows in Google Sheets with Apps Script: deleteRow and deleteRows by position, delete rows that match a condition by looping from the bottom, blanks.

Previous: setValue and setValuesNext: appendRow