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

The code before and after the fix.

Problem 2 changed lines
function writeRows() {
  const values = [['a', 'b'], ['c']];
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, 2).setValues(values);
}
Fixed 2 changed lines
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.

Things to check

  • Decide the fill value for short rows before running. The fix uses an empty string, which changes the values the write supplies.