TypeError: Cannot read property 'range' of undefined

function onEdit(e) {var cellaAttiva = e.range;}

I wrote this simple line, it is part of a more complex code. But already in this way he signals this error to me. Could you tell me why?

var fd =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Database");

var fm =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Menu");

var db =fd.getRange(2,1,fd.getLastRow(),3);

var primoLivello   = 1

var secondoLivello = 2

var terzoLivello   = 3

function onEdit(e) {

  

  var cellaAttiva = e.range;

  var valoreCella = cellaAttiva.getvalue();

  var r  = cellaAttiva.getRow();

  var c  = cellaAttiva.getColumn();

  var fa = cellaAttiva.getSheet().getName();

  if (fa== "Menu" && r>1 && c==primoLivello){

    fm.getRange(r, secondoLivello).clearContent();

    fm.getRange(r, secondoLivello).clearDataValidations();

    var datiFiltrati = db.filter(function(o){return o[0]== valoreCella});

    var lista        = datiFiltrati.map(function(o){return o[1]});

    var convalida = SpreadsheetApp

    .newDataValidation()

    .requireValueInList(lista)

    .setAllowInvalid(false)

    .build();

    fm.getRange(r,secondoLivello).setDataValidation(convalida);

  } else if (fa == "Menu" && r > 1 && c == primoLivello){

    fm.getRange(r,terzoLivello).clearContent();

    fm.getRange(r, terzoLivello).clearDataValidations();

    var datoPrimoLivello=fm.getRange(r, primoLivello).getValue();

    var datiFiltrati = db.filter(function(o){return o[1]== valoreCella && o[0]==datoPrimoLivello});

    var lista        = datiFiltrati.map(function(o){return o[2]});

    var convalida = SpreadsheetApp

    .newDataValidation()

    .requireValueInList(lista)

    .setAllowInvalid(false)

    .build();

    fm.getRange(r,terzoLivello).setDataValidation(convalida);

  }

}

Is this a google apps script problem ?

You can't get a spreadsheet like this:

var fd =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Database");

If your script is bound to the spreadsheet then just use:

var fd =SpreadsheetApp.getActiveSpreadsheet();

if not the use the getByUrl or openById methods.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.