Automatically stamp username who made changes to Google Sheet

那年仲夏 提交于 2020-02-06 02:49:53

问题


I have a Google Sheet that I would like to automatically stamp the username of the person who last made a change to a specific row. I currently have this code to work as I like to do the same function with the time:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  var r = s.getActiveCell();
  if( r.getColumn() != 2 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange('G' + row.toString()).setValue(time);
  };
 };

Assuming this is somewhat close to what I require for the username, I tried using this:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  var r = s.getActiveCell();
  if( r.getColumn() != 2 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange('H' + row.toString()).setValue(Session.getActiveUser);
  };
 };

But that spits out some weird command that is clearly broken.

How can I do this?

This is the sheet I am working on: https://docs.google.com/spreadsheets/d/1VifU8AJWuPn53-_JCQKbPTjHxKDArkyG0G7zt1wzmPg/edit?usp=sharing


回答1:


Is this what you are trying to do?

function onEdit(e) {
  var r = e.range;
  if( r.getColumn() != 2 ) { //checks the column
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange(r.getRow(),8).setValue(Session.getActiveUser().getEmail());
  }
 }


来源:https://stackoverflow.com/questions/31662671/automatically-stamp-username-who-made-changes-to-google-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!