I’m trying to run the onChange function below using triggers but keeps failing.
The error is:
TypeError: Cannot read properties of undefined (reading 'getSheet') at onChange(Code:161:21)
Line 161 is var sheet = range.getSheet();
My function:
var range = e.range;
var sheet = range.getSheet();
if (sheet.getName() == 'SMS' && range.getRow() > 1 && range.getColumn() < 3) {
var colA = sheet.getRange(range.getRow(), 1).getValue();
var colB = sheet.getRange(range.getRow(), 2).getValue();
if (colA != '' && colB != '') {
sheet.getRange(range.getRow(), 3).setFormula("='SMS Text'!$B$1");
}
}
}```
Triggers settings are:
[![enter image description here][1]][1]
I have also tried:
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/IjjDm.png
[2]: https://i.stack.imgur.com/gynFP.png
Differences in onEdit and onChange event objects
function onMyEdit(e){Logger.log(JSON.stringify(e))};
//{"authMode":"FULL","oldValue":"10.0","range":{"columnEnd":6,"columnStart":6,"rowEnd":6,"rowStart":6},"source":{},"triggerUid":"","user":{"email":"","nickname":""},"value":"pp"}
function onMyChange(e){Logger.log(JSON.stringify(e))};
//{"authMode":"FULL","changeType":"EDIT","source":{},"triggerUid":"","user":{"email":"","nickname":""}}
onChange event object does not supply a range value
The event object for onChange and onEdit trigger is a lot different.
You will need to replace e.range somehow because onChange does not supply a range value.