15

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
user1468633
  • 161
  • 1
  • 1
  • 5
  • 1
    Please check this [post](http://stackoverflow.com/questions/12172849/how-to-indentify-user-using-google-apps-script/). Your script can have the same issue. – megabyte1024 Sep 06 '12 at 13:08
  • http://stackoverflow.com/questions/12172849/how-to-indentify-user-using-google-apps-script/ check this answer.... you will get the idea – Viral Shah Sep 06 '12 at 13:14

4 Answers4

19

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;
}
Wim den Herder
  • 1,197
  • 9
  • 13
  • Assuming this used to work, it no longer works as getEditors returns a list of users rather than a list of emails (see https://developers.google.com/apps-script/reference/spreadsheet/protection#geteditors). To get the email from the user, the same limitation applies. – Mattias Martens Jan 28 '19 at 22:41
  • However when you have user A & B in the session. Editors will be only [userA, userOwner], userB is nowhere to be found. And I'm actually using userB to view the document. – addlistener May 10 '22 at 12:24
8

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.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Srik
  • 7,907
  • 2
  • 20
  • 29
  • If security policies do not allow access to the user's identity, User.getEmail() returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). https://developers.google.com/apps-script/reference/base/session – Meetai.com Feb 19 '15 at 00:51
1

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

michaeldon
  • 507
  • 3
  • 11
0

In this code you can use a cell for input. Authorising scripts are not required.

function onEdit(e){
  checkUsername(e);
}

function checkUsername(e){
  var sheet = e.source.getActiveSheet();
  var sheetToCheck = 'Your profile';
  var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetToCheck);
  var CellInputUsername = 'B4';
  var ActiveCell = SpreadsheetApp.getActive().getActiveRange().getA1Notation();

  if (sheet.getName() !== sheetToCheck || ActiveCell !== CellInputUsername){return;}

  var cellInput = sheetName.getRange(CellInputUsername).getValue();
  PropertiesService.getUserProperties().setProperty("Name", cellInput);

  // Make cell empty again for new user
  sheetName.getRange(CellInputUsername).setValue("");

  var Username = PropertiesService.getUserProperties().getProperty("Name");      
  SpreadsheetApp.getUi().alert("Hello " + Username);
}
Giel
  • 1