I'm trying to identify current user's name to make notes of who edited what like this:
r.setComment("Edit at " + (new Date()) + " by " + Session.getActiveUser().getEmail());
but it won't work - user's name is an empty string. Where did I go wrong?
I suppose you have this piece of code set to execute inside an
onEdit
function (or an on edit trigger).If you are on a consumer account,
Session.getActiveUser().getEmail()
will return blank. It will return the email address only when both the author of the script and the user are on the same Google Apps domain.GOOD NEWS: It's possible with this workaround!
I'm using some protection functionality that reveals the user and owner of the document and I'm storing it in the properties for better performance. Have fun with it!
I had trouble with Wim den Herder's solution when I used scripts running from triggers. Any non script owner was unable to edit a protected cell. It worked fine if the script was run from a button. However I needed scripts to run periodically so this was my solution:
When a user uses the sheet the first time he/she should click a button and run this:
This saves the user's input to a user property. It can be read back later at any time with this code:
var user = PropertiesService.getUserProperties().getProperty("ID");
In this code you can use a cell for input. Authorising scripts are not required.