0
votes

I am trying to trigger a Google App Script that I have bound to a google sheet.

Data from firebase is pulled into a sheet which should trigger the script. I'm currently using onEdit(e) which works when I manually add the data in but doesn't work when the data is updated from firebase. Is there any way I can set up a trigger to run the function every time the cell values in a specific range are changed.

Thanks!

Script

function onEdit(e) {
  var ss = SpreadsheetApp.getActive()
  var dumpingGround = ss.getSheetByName("Dumping Ground")
  var dataDump = ss.getSheetByName("Data Dump")
  var ddLastRow = dataDump.getLastRow() + 1
  var editCol = e.range.getColumn();
  var editRow = e.range.getRow();

  Logger.log("edit")
  /*Copy from dumping ground to data dump*/
  if ((editRow == 1 && editCol == 2) || (editRow == 1 && editCol == 3)){

    /*Set required range for data dump and dumping ground*/
    var ddRange = '\'Data Dump\'!A' +ddLastRow+ ':BF' + ddLastRow
    var dgCopyRange = '\'Dumping Ground\'!B1:BF1'
    
    /*Copy action*/
    ss.getRange('\'Dumping Ground\'!B1:BF1').copyTo(ss.getRange(ddRange), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);

    /*Check data dump row count and delete 1000 rows if necessary*/
    if(ddLastRow = 9999){
      dataDump.deleteRows(1, 1000);
    }
  }
};
1
Check if onChange() is triggered.TheMaster

1 Answers

2
votes

From the Apps Script documentation:

Script executions and API requests do not cause triggers to run. For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.

I think your best but would be to create a time trigger and check for changes in the specified range. Naturally, I can't really say for sure if that's something that would even work for your purposes, but it'd be worth a try.

Here are some alternatives to consider:

  • If the data pull is initiated by your script, then simply have that function complete the desired actions.
  • If you have a separate service populating the data, you can use the Sheets API.
  • You could publish the script as a web app and trigger via GET or POST
  • You could try executing a function using Apps Script API. Please review the requirements for doing so, because it may not work for your requirements.