1
votes

I have a script for GOogle Sheets that works almost perfectly.I just need one addition: To add the logged in user to the cell. I have a sheet called "GMB Descriptions" When a user enters information in any cell row in column 4, it shows the date & time in column 5 of the same row. No problem. The issue is I want it to show the logged in user in that cell as well Currently (script below) shows: MM/dd/yyyy - hh:mm a I want it to show: MM/dd/yyyy - hh:mm a - LoggedIn User I understand it would use the user email who made the change to that row.

Summary: How do I add the code to the following script to also show the user who made that edit?

function onEdit() {
 var s = SpreadsheetApp.getActiveSheet();
 if( s.getName() == "GMB Descriptions" ) { //checks that we're on the correct sheet
   var r = s.getActiveCell();
   if( r.getColumn() == 4 ) { //checks the column
     var nextCell = r.offset(0, 1);
     if( nextCell.getValue() === '' ) //is empty?
       var time = new Date();
       time = Utilities.formatDate(time, "GMT-8", "MM/dd/yy - h:mm a");
       nextCell.setValue(time);
   };
 };
}
2
You should "ask" active user to run the auth script manually (by button click, for example). This script will take Session.getActiveUser().getEmail() value indeed. You can use this value in onEdit script then. onEdit has limited auth mode and can not take email directly.Александр Ермолин
Hello,, I don't want to "ask" the user to do anything. I just want the users account (email) to show in the cell. That's all. This script does it: webapps.stackexchange.com/questions/73476/… but only in one cell. I want to add it to my original so it does it for each row every time a change is made to the row. So question is how do I add it to the original script?Ðħira Ÿesufu
Thank you for the link. You see, here e.user.getEmail() is used. The problem is that it has limited functionality (security reason). For example it will NOT work if the Sheet is shared by hyperlink for editing.Александр Ермолин
This sheet is only shared internally and only for people who are logged in - so the security issue, is not an issue.Ðħira Ÿesufu

2 Answers

0
votes

Try this:

function onEdit(e) {
 var rg=e.range;
 var sh=rg.getSheet();
 var name=sh.getName();
 if( name=="GMB Descriptions") { 
   if( rg.columnStart==4) { 
     var nextCell = rg.offset(0, 1);
     if( nextCell.getValue()=='') { 
       var dt=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yy - h:mm a");
       nextCell.setValue(Utilities.formatString('%s\n%s',dt,Session.getActiveUser().getEmail()));
      }
    }
  }
}
0
votes

The modified (and slightly reduced) script is below:

function onEdit(e) {
  var rg = e.range;
  if (rg.getSheet().getName() == "GMB Descriptions" && rg.columnStart == 4) { 
    var nextCell = rg.offset(0, 1);
    if (nextCell.getValue() == '') {
      var dt = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy - h:mm a - ");
      var userMail = e.user.getEmail();
      nextCell.setValue(dt + userMail);
    }
  }
}

Thanks to Cooper for Utilities hint, it works fine. We use e.user.getEmail() assuming no security problems (according to asker opinion). While testing for me, it works as expected.