Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine current user in Apps Script

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?

like image 607
user1468633 Avatar asked Sep 06 '12 13:09

user1468633


People also ask

How can I see log in script app?

A basic approach to logging in Apps Script is to use the built-in execution log. To view these logs, at the top of the editor, click Execution log. When you run a function or use the debugger, the logs stream in real time. You can use either the Logger or console logging services in the built-in execution log.

How do you filter in App script?

Get the range that the filter applies to The below sample gets the filter on the active sheet, then uses the getRange() method from this class to log the range that the filter applies to. let ss = SpreadsheetApp. getActiveSheet(); // Gets the existing filter on the active sheet.

How do I find my timezone in Appscript?

getTimeZone() Gets the time zone of the script.


3 Answers

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!

function onEdit(e) {
  SpreadsheetApp.getUi().alert("User Email is " + getUserEmail());
}

function getUserEmail() {
  var userEmail = PropertiesService.getUserProperties().getProperty("userEmail");
  if(!userEmail) {
    var protection = SpreadsheetApp.getActive().getRange("A1").protect();
    // tric: the owner and user can not be removed
    protection.removeEditors(protection.getEditors());
    var editors = protection.getEditors();
    if(editors.length === 2) {
      var owner = SpreadsheetApp.getActive().getOwner();
      editors.splice(editors.indexOf(owner),1); // remove owner, take the user
    }
    userEmail = editors[0];
    protection.remove();
    // saving for better performance next run
    PropertiesService.getUserProperties().setProperty("userEmail",userEmail);
  }
  return userEmail;
}
like image 166
Wim den Herder Avatar answered Oct 25 '22 06:10

Wim den Herder


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.

like image 40
Srik Avatar answered Oct 25 '22 07:10

Srik


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:

function identifyUser(){
   var input = Browser.inputBox('Enter User Id which will be used to save user to events (run once)');
  PropertiesService.getUserProperties().setProperty("ID", input);
}

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");

like image 34
michaeldon Avatar answered Oct 25 '22 08:10

michaeldon