问题
I have a script that is currently set to run onEdit. Ideally, I'd like the script to run ONLY when someone makes a change to cell B26 of the sheet in question.
Is this possible?
回答1:
This is code taken directly from the documentation:
function onEdit(e){
// Set a comment on the edited cell to indicate when it was changed.
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
It already has e.range in it. All you need to do is add and if statement, and get the cell address of the range:
function onEdit(e){
var cellAddress,cellAddressToTestFor;
cellAddressToTestFor = 'B26';
// Get cell edited - If it's B6 then do something
cellAddress = e.range.getA1Notation();
Logger.log('cellAddress: ' + cellAddress);
if (cellAddress === cellAddressToTestFor) {
//To Do - Code here if cell edited is correct
Logger.log('the check worked!');
};
}
Add that code. Edit a cell, then VIEW the LOGS.
来源:https://stackoverflow.com/questions/30426124/can-i-add-a-trigger-based-on-change-of-a-particular-cell