I have a spreadsheet in Google Sheets that is shared with five people. Only a few columns are edited; all of the other columns are locked.
We need the ability to track changes to these editable columns, so, based on this answer, I added a script in Apps Script like this:
function onEdit() {
if( s.getName() == "MySpecialSheet" ) { // Set to sheet name
var r = s.getActiveCell();
var col= r.getColumn();
if( col == 3 ) { // Check the column
var nextCell = r.offset(0, 1);
var datetime = new Date();
datetime = Utilities.formatDate(datetime, "GMT+9", "yyyy/MM/dd, hh:mm:ss"); // +9 for Tokyo
nextCell.setValue(datetime);
};
};
}
This automatically inserts the time when the relevant cell was edited, which is great. However, I also want to add a way to tell who edited the cell.
I checked the User class in the apps script documentation, but it seems the only method is getEmail()
, which:
is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user).
Since my script uses onEdit()
, I have no way to get the email address. Is there a way to get any kind of ID (I don't need the email address-- it could be the name of the account or a number) for the person editing a cell, and then insert that into another cell?
If not, I suppose I can create a column that people can just type their initials into, but people tend to forget so I would prefer an automated solution if possible.