Fetch editors name in google spreadsheet and write it to cell

给你一囗甜甜゛ 提交于 2019-12-25 05:15:55

问题


Is there any way to have an onEdit function that fetches the editors name and writes it to B2 whenever A2 is changed?

https://docs.google.com/spreadsheets/d/1hYp2KAZSlRN2HNUWvyHAkc35mJtDPBSVipl4PtrSIJw/edit?usp=sharing

(e.g, the name of my google account is "Thomas G.", so this should be written to B2.) Possible?


回答1:


IMPORTANT CAVEAT: the code below will generally work only if the owner/publisher of the script and the user accessing the sheet belong to the same Google Apps domain. It will NOT work for non-apps users.

You can do this with the following onEdit() method in your sheet:

function onEdit(e) {
  var editedRange = e.range;
  if ( editedRange.getColumn()==1 ) { // if column A was edited
    var u = Session.getActiveUser().getEmail();
    editedRange.offset(0, 1).setValue(u); // write user's email to corresponding row in column B
  }
};

Note: this will require users to authorize the script's access to their data on Google (to know the user's email).



来源:https://stackoverflow.com/questions/26091149/fetch-editors-name-in-google-spreadsheet-and-write-it-to-cell

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