Question

I need to get the user property of an event (see documentation) and I have been going nuts over this as the result of the following simplified script included user in some cases and not in others:

function myOnEdit(event) {
  var result = "";

  for (var key in event) {
    result = result + key + ": " + event[key] + "; ";
  }
}

What influences whether the event includes the user property?

Was it helpful?

Solution

After much struggle I realized that this is an authorization issue of sorts.

Under normal circumstances the above myOnEdit does not need to be authorized, and if executed manually it does not ask for authorization. It executes flawlessly -- just without the user info.

If, however, the following line (or something similar) appears somewhere -- anywhere -- in the script, that solves the problem:

Session.getActiveUser().getEmail();

The existence of this line forces the authorization request to appear when any function in the script is ran manually and once this authorization is granted, the result of the original myOnEdit will include the user property.

Yay!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top