0
votes

I have a code, it inserts the last modification to the current row in the specified cell. And i need to insert the time in one cell, and the name of the user who made ​​the change, in the next, is it possible?

Can anybody help me? Thanks.

function onEdit(event) {

  var tsheet = 'Sheet1' ;
  var colspec=[[17,17,18],[15,15,16],[9,9,8]];

  var s = event.source.getActiveSheet();
  var sname = s.getName();
  if (sname == tsheet) {
    var r = event.source.getActiveRange();
    var scol = r.getColumn();
    var i=colspec.length;
    while (i>0) {
      i--;
      if (scol >= colspec[i][0] && scol <= colspec[i][1]) {
        s.getRange(r.getRow(), colspec[i][2]).setValue(new Date());
      }
    }
  }
}
1
What kind of user do you want to log? Windows authentication? Sql user?Rob
This question is related to Google-apps-script, the user is (most probably) the session user... (inappropriate tag I'm afraid ;)Serge insas
My bad, delete this tag. I need a login (user name) who edit this google spreadsheetsnoop31

1 Answers

4
votes

Here is a code I use to track changes on a spreadsheet, the onEdit() function is a simple trigger, the alert() function is an installable onEdit trigger since it need authorization.

function onEdit(event){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  r.setComment("Last modified: " + (new Date())+' by '+Session.getActiveUser());
  ss.toast('Dernière cellule modifiée = '+r.getA1Notation()+' par '+Session.getActiveUser());
}

function alert(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var cell = ss.getActiveCell().getA1Notation()
      if(Session.getActiveUser()!='[email protected]'){
  MailApp.sendEmail('[email protected]', 'Modification dans le planning', 'la case '+cell+' du document a été modifiée par '+Session.getActiveUser());
} 
}