flymike
flymike

Reputation: 945

Google Apps Event changeType undefined in Google Sheet

function onEdit(e) {
  Browser.msgBox(e.changeType);
  if (e.changeType == 'EDIT') {
    ...
  }
}

The event fires successfully when a sheet cell is changed from empty to a number, but the msgBox output shows "undefined".

Shouldn't e.changeType contain 'EDIT'?

I'm using this reference: https://developers.google.com/apps-script/understanding_events

Upvotes: 2

Views: 4405

Answers (2)

Antonio Ooi
Antonio Ooi

Reputation: 1828

As of 03 September 2024, at least based on my testing, event installer trigger no longer work:

function onEditValidations(e) {
  Logger.log(`e = ${JSON.stringify(e)}`);
  Logger.log(`e.changeType = ${e.changeType}`);
  if (e.range.getRow() == 1 || (!e.oldValue && !e.value)) { // EDITED
    return;
  }
  // Otherwise do more stuff here
}

The output for e when formatting is changed:

e = {"authMode":"FULL",
   "range":{"columnEnd":8,"columnStart":8,"rowEnd":2,"rowStart":2},
   "source":{},
   "triggerUid":"123456",
   "user":{"email":"[email protected]","nickname":"someone"}}

Note that e.changeType doesn't even exist despite an installer trigger is used. Therefore, for my case, in order to detect change of data only, I check if e.oldValue exists. Hope this helps.

EDITED: Note that to ensure the new value will still be validated when e.oldValue is empty, the condition (!e.oldValue && !e.value) is safer.

Upvotes: 0

Serge insas
Serge insas

Reputation: 46802

The documentation you refer to does not concern the simple onEdit trigger, it works with the installable onChange trigger which is completely different.

You should rename your function to whatever name you want (but not "onEdit") and add an onchange trigger from the script Editor menu /Resources/current project trigger/

Then if you want to know what value is returned in the event info you can use a code like this :

function testonChange(e) {
  Browser.msgBox(Utilities.jsonStringify(e));
}

And you'll see exactly how the event is considered.

Upvotes: 5

Related Questions