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

The code before and after the fix.

Problem 1 changed line
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);
}
Fixed 2 changed lines
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, so 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.

Things to check

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