Reputation: 15226
I am trying to detect what sheet an edit was made on during an installed onEdit
function.
The logger correctly records the data of the cell being edited however does not tell me what sheet this edit was made on. I need specifically to ID what sheet the edit took place on in order to complete a function I am building.
Logger:
[19-08-01 08:37:27:714 CDT] {"authMode":{},"range":{"columnStart":4,"rowStart":4,"rowEnd":4,"columnEnd":4},"source":{},"oldValue":"2.0","triggerUid":"1111111","user":{"nickname":"Mike-SMT","email":"[email protected]"},"value":"222"}
Code used to log event:
function raw_data_edited(e) {
Logger.log(JSON.stringify(e));
}
I do not see anything in the above log that would indicate specific sheet.
Is there a way to ID sheet from event?
Upvotes: 0
Views: 299
Reputation: 2774
It's strange, source
always seems to show as empty when logging the event object, but can still be used.
I wasn't sure if you meant spreadsheet ID or sheet ID, so here's how to get both using event objects:
//get spreadsheet id using "source" object
e.source.getId();
//get spreadsheet id using "range" object
e.range.getSheet().getParent().getId();
//get individual sheet id using "source" object
e.source.getActiveSheet().getSheetId();
//get individual sheet id using "range" object
e.range.getSheet().getSheetId();
Upvotes: 1
Reputation: 4247
e.range
will get you the range that was edited.
From there you can use getSheet()
to get the sheet that this range belongs to.
And then from the sheet, use getSheetId()
to get the ID of the sheet
function raw_data_edited(e) {
Logger.log(JSON.stringify(e));
var ssID = e.range.getSheet().getSheetId().toString();
Logger.log(ssID);
}
Upvotes: 1