0
votes

I have the simple trigger set to onEdit and used the code below to execute it. Everything works fine but when I try to shift the tab where my main function executes, the whole trigger stopped working and typeError comes up "cannot read property 'range' of undefined"

I have 3 sheets. Main sheet where function runs, 2nd sheet where my onEdit takes place and a secondary sheet. Trigger won't work when I shift my main sheet to the second or last tab.

function onEdit(e) {
  if (e.range.getA1Notation() == 'C9') {
    if (/^\w+$/.test(e.value)) {        
      this[e.value]();
      e.range.clear();
    }
  }
}
2

2 Answers

0
votes

You can't run onEdit as it will return that error. Your onEdit should work even you shift the tab, just try it on your sheet.

0
votes

I've found out why. The issue lies in my main function code on how I open another sheet. When my main sheet is in the first tab, I can get it by openbyURL. This won't work when I shift the tab away.

var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Name');

I replaced the openbyurl with the liner above and everything works