Search code examples
google-apps-scriptgoogle-sheetstriggerseventtrigger

Trigger function when adding a row in an other spreadsheet


I have a problem with triggers in Google App Script. I have a spreadsheet holding my users and other stuff (appSheet), and I have a second spreadsheet that holds all the data (dataSheet).

I also built a script that imports some data from "appSheet" to "dataSheet", and it works pretty well. The problem is I need it to execute automatically when a new row (user) is added in the app sheet. The built-in triggers in Google App Script doesn't allow me to set that kind of trigger.

I set this importing function in the dataSheet, maybe should I set it in the appSheet ? It may seem super easy but I'm a newbie to this :)

Has someone an insight on how to automatically trigger that function ?

Thanks a lot !


Solution

  • Thanks guys for your answers ! I found a very simple way to achieve this.

    Instead of setting the script on the dataSheet and having the need of an "onEdit" trigger, I put the script in the appSheet and did little modifications.

    The script is as followed :

    function exportData() {
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var values = ss.getSheetByName('mySourceSheet').getRange('A2:B').getValues();
    
      var dataSheet = SpreadsheetApp.openById("myDestinationSpreadsheet");
      var targetSheet = dataSheet.getSheetByName("myDestinationSheet");
    
      targetSheet.getRange(2,1,values.length,values[0].length).setValues(values);
    }
    

    The "2" in the "targetSheet.getRange" meaning I want it to begin in row 2.

    Everything runs perfectly now with the built-in triggers in Apps Script.