Skip to content

Diagnostic reference

Cannot read properties of undefined (reading 'setDataValidation')

Why setDataValidation fails on an undefined range, usually an out-of-range index, and how to iterate the known cells instead.

Updated September 12, 2026

Apps Script error

TypeError: Cannot read properties of undefined (reading 'setDataValidation')

What it means

The range receiving the validation rule is undefined. The method is correct, but the object it is called on is missing. The usual cause is an array index that misses: cells[2] on a two-entry array, a loop variable past the end, or a helper that returns nothing on some path. Check the expression that supplied the range.

Related error messages

  • Cannot read properties of undefined (reading 'setValue')
  • Cannot read properties of undefined (reading 'getRange')

Before and after

The code before and after the fix.

Problem 2 changed lines
function addValidation() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const rule = SpreadsheetApp.newDataValidation()
    .requireValueInList(['Yes', 'No'])
    .build();
  const cells = ['B2', 'B3'].map(a1 => sheet.getRange(a1));
  cells[2].setDataValidation(rule);
}
Fixed 1 changed line
function addValidation() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const rule = SpreadsheetApp.newDataValidation()
    .requireValueInList(['Yes', 'No'])
    .build();
  ['B2', 'B3'].forEach(a1 => sheet.getRange(a1).setDataValidation(rule));
}

How to fix it

  • A two-entry array has indexes 0 and 1, so cells[2] is undefined.
  • Iterate the known cell addresses instead of indexing past the array.
  • Apply the validation rule to each intended target.

Things to check

  • Check that every target cell should receive the rule.