Bjorn Behrendt
Bjorn Behrendt

Reputation: 1264

How can I get the editor's email onEdit(), or ask for Authorization on first edit

This is the first part of a larger script, but I need to get the email address of the person making edits to the cell. For the example I am just trying to add the editors email as a comment to the cell that was edited.

I know that the onEdit() runs in authMode=LIMITED which is the issue. And an installable trigger will also not work as it will only return the person who created the script and not the editor of the cell.

This means the below works for the owner of the script if I manually get them to authorize it, but will not work for any editors of the script.

I think I can get this to work if there is a way to force a user to authorize themselves on first edit.

function onEdit(e){
  Logger.log(e);
  var range = e.range;
  var email1 = Session.getActiveUser().getEmail();
  var email2 = Session.getEffectiveUser().getEmail();
  range.setNote('Active User: ' + email1 + '\nEffective User: '+ email2);
}

Upvotes: 1

Views: 1809

Answers (1)

Eric Koleda
Eric Koleda

Reputation: 12673

The rules governing email visibility are quite complex, due to legacy behavior and privacy concerns. As you noted, you won't be able to get the email address of a user unless they have authorized the script themselves. The one special case are users within a Google Apps domain. If the owner of the script has authorized it, then it will be able to read the email address of other users within the domain.

There is no way to force authorization, so for the case where users aren't in the same domain, or are consumer accounts, you'll have to ask them to authorize the script, via a menu item, button, or other means.

Upvotes: 2

Related Questions