Google-sheets – Run a Script When Tab Programmatically added to a Google Spreadsheet

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I'm trying to pull data from an Adwords report to a Google Sheet through an Adwords script. This will be done on a daily basis. Every time the Adwords script runs, a new tab is created (and an old one is to be deleted) on the Google sheet. I'm trying to figure out how to use the 'Creation of a New Tab' event to trigger a script in GSheets that will run a (V)lookup between the newly created tab and an existing tab on the Sheet. So essentially this whole process summed up is as follows:

i) Adwords script to pull data to a G Sheet daily – done
ii) G Sheet script to look up a value in the new tab and compare against a value in an existing tab. – currently stuck
iii) Build yet another tab "Tab C" to collect the records with different values
iv) G Sheet script to send email notification with output of "Tab C"
v) G Sheet script to Delete an older tab

As you can imagine, this is way beyond my coding skills at the moment. So any ideas appreciated.

Best Answer

The closest to 'Creation of a New Tab' event is the Change event but this and the other events triggered by "actions" only works for changes made by a user through the Google Sheets user interface.

UPDATE: the Change event also works for changes made through the Google Sheets API

In order to make the tasks after the creation of the new sheet (tab), the "first option" is to modify the Adwords script by adding the corresponding commands after the code line that creates the sheet.

If you don't want or can't do that here are some alternatives

  • use a time-driven trigger programmed to run a certain time after the Adwords script run.
  • use an open trigger that runs every time that the spreadsheet is open.
  • add a button or custom menu to manually trigger the script.
  • use the Google Apps Script IDE to run the script.

References