Skip to content

Diagnostic reference

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

Why setValues rejects a ragged 2D array, how to find the short row, and how to size the range from the data.

Updated September 12, 2026

Apps Script error

Exception: The number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 2.

What it means

setValues needs a rectangular 2D array whose width matches the target range. One ragged row, often from a missing trailing value or a joined string that was never split, fails the whole write. Every row must have the same number of entries as the destination has columns.

Related error messages

  • The data has 5 but the range has 4.

Before and after

See the change before you make it.

Problem
function writeRows() {
  const values = [['a', 'b'], ['c']];
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, 2).setValues(values);
}
Safer version
function writeRows() {
  const values = [['a', 'b'], ['c', '']];
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

How to fix it

  • Give each row the same number of entries; this example fills the short row with an empty string.
  • Set the range width from values[0].length.
  • Check every row when data comes from a parser or another service.

What stays with you

  • Decide the fill value for short rows; the fix uses an empty string.
  • Padding changes the values supplied to the write; choose the fill value before running.