问题
This code works as intended when you manually change a cell.
function onEdit(event) {
// assumes source data in sheet named Prepsheet
// target sheet of move to named TopUp Required
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "PrepSheet" && r.getColumn() == 15 && r.getValue() == 0) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("TopUp Required");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
My problem is I am using =sum(A2-B2) to change the cell. if cell then =0 it should run the script, but doesn't because the cell wasnt manually changed.
How could I modify code to do this? Or how would you do it
Appreciate any help
回答1:
The workaround would be using the installable onChange trigger in combination with a second spreadsheet with an =IMPORTRANGE() function.
Explanation:
While an onEdit trigger does not detect any cell content update caused by a cell formula at all, onChange can detect certain changes, such as the update occuring through the =IMPORTRANGE() formula.
Workflow:
- Create a second, empty spreadsheet
- Assign to a cell in a sheet of the second Spreadsheet the formula
=IMPORTRANGE(IMPORTRANGE(spreadsheet_url, range_string), whereby spreadsheet_url is the URL of the first spreadsheet that contains all your data and formulas (e.g.=sum(A2-B2)) and range_string the range of interest (e.g. "PrepSheet!O1:O" for column 15 in sheetPrepSheet) - Attach a script to the second spreadsheet, rather then to the original spreadsheet
- Use Scriptproperties to store cell old cell values and compare to the new values - in order to detect which cell has been edited
Sample:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("name"); // the name of the sheet containing the =IMPORTRANGE() formula
var origin=SpreadsheetApp.openById('ID of original spreadsheet');
var s=origin.getSheetByName("PrepSheet");
var lastRow=s.getLastRow();
var range=sheet.getRange(1,1,lastRow,1); //the column into which you imported column 15 from the original spreadsheet
function initialSetUp(){//run this function only once, unless your range of interest changes
//change if required
var values=range.getValues();
for(var i=0;i<values.length;i++){
PropertiesService.getScriptProperties().setProperty('values '+i, values[i][0]);
}
}
function triggeredOnChange() {
var values=range.getValues();
var numColumns = s.getLastColumn();
var targetSheet = origin.getSheetByName("TopUp Required");
for(var i=0;i<values.length;i++){
var scriptValue=PropertiesService.getScriptProperties().getProperty('values '+i);
var newValue=values[i][0];
if(newValue!=scriptValue && newValue==0){
Logger.log(scriptValue);
Logger.log(newValue);
var row = i+1;
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
}
回答2:
onEdit triggers only fire on user edits.
onEdit() reference
Using all event object parameters should result in faster execution.
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="PrepSheet" && e.range.columnStart==15 && e.value==0) {
var tsh=e.source.getSheetByName("TopUp Required");
var target=tsh.getRange(tsh.getLastRow() + 1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(target);
}
}
But of course, none of this will change the fact that onEdit triggers only occur for user edits.
来源:https://stackoverflow.com/questions/59362734/on-edit-doesnt-work-if-cell-auto-updates-need-a-workaround-google-sheets