Skip to content

Diagnostic reference

The number of rows in the data does not match the number of rows in the range

Why a setValues range sized from a separate count disagrees with the array, and the fix: size the range from values.length.

Updated September 12, 2026

Apps Script error

Exception: The number of rows in the data does not match the number of rows in the range. The data has 9 but the range has 10.

What it means

The target range height and the outer array length disagree. Usually a header row was stripped from the data but not from the range math, or getLastRow moved between the read and the write. Size the range from the array itself so the two always match.

Related error messages

  • The data has 9 but the range has 10.

Before and after

See the change before you make it.

Problem
function copyRows() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const values = sheet.getRange('A2:B10').getValues();
  const out = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Out');
  out.getRange(2, 1, 10, 2).setValues(values);
}
Safer version
function copyRows() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const values = sheet.getRange('A2:B10').getValues();
  const out = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Out');
  if (!out) throw new Error('Sheet "Out" not found.');
  out.getRange(2, 1, values.length, values[0].length).setValues(values);
}

How to fix it

  • A2:B10 contains nine rows; a ten-row destination does not match.
  • Set the destination height from values.length and width from values[0].length.
  • Check that the destination sheet exists before writing.

What stays with you

  • Confirm the destination sheet name before accepting the guard.
  • myFunction does not clear or resize the destination sheet.