1

I have a small google spreadsheets which multiple users are accessing and adding info in it. I've been trying to get the email of every user which edits a row but I can't seem to find out how it works! In here it's written that I can do it with getActiveUser() function but when changes are made, it just leaves a blank cell on the row!

Here is what I've done so far:

function onEdit(e) {
  var s = SpreadsheetApp.getActiveSheet();
  var email = Session.getActiveUser().getEmail();

  if( s.getName() == "Sources with Bugs" ) {                                       
    var r = s.getActiveCell();
    if( r.getColumn() == 2) {                                                      
      var userCell = r.offset(0, 4);
      userCell.setValue(email);
    }
  }
}

So as I said, basically if a user edits the 2nd column of any rows, the 6th column should be filled with the user's email! Anyone has any idea what am I doing wrong?

player0
  • 124,011
  • 12
  • 67
  • 124
CodeMonkey
  • 2,511
  • 4
  • 27
  • 38
  • @MaxMakhrov Yes but in there it says: allows a script to run without that user's authorization or "execute as me" (that is, authorized by the developer instead of the user) onEdit ! so basically when I allow, all users should be giving permission by my consent! isnt it? – CodeMonkey Aug 28 '19 at 07:58

1 Answers1

1

You may want to give a shot to e.user.getEmail() instead of Session.getActiveUser().getEmail() — for the reasons described on @Max Makhrov's link in the comment above.

Benoît Wéry
  • 862
  • 1
  • 6
  • 8
  • Yea, I tried that too but i get the following: TypeError: Cannot read property "user" from undefined. (line x, file "Code")Dismiss – CodeMonkey Aug 28 '19 at 08:04
  • That would happen if you try to run `onEdit(e)` manually from the script editor. If you want to test the function, you should actually edit a cell in your sheet or look at this trick: https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas – Benoît Wéry Aug 28 '19 at 08:10