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; 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 receiver; undefined and null both prevent a method call, but neither value alone explains why the lookup failed.

Related error messages

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

Before and after

See the change before you make it.

Problem
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);
}
Safer version
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.

What stays with you

  • Check that every target cell should receive the rule.
  • myFunction does not create the validation list values.