On Edit doesnt work if cell auto updates - Need a workaround Google Sheets

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


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:

  1. Create a second, empty spreadsheet
  2. 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 sheet PrepSheet)
  3. Attach a script to the second spreadsheet, rather then to the original spreadsheet
  4. 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);
    }
  }
}