Vlad
Vlad

Reputation: 329

Why do I see installable triggers in my source google apps script that is published as a Sheets add-on

I need help with understanding installable triggers. I developed a Sheets add-on and I need to add some function to run when user renames a tab (sheet). As I understand it is possible only when using the onChange installable triggers (there is no such simple trigger).

My add-on has a menu to open a sidebar. When clicking that menu, my function first checks and creates a trigger if it does not exist:

  if(ScriptApp.getProjectTriggers().filter(t=>t.getHandlerFunction() == 'gsOnChange').length==0){
    console.log(Session.getActiveUser().getEmail(),'Added trigger: gsOnChange()')
    ScriptApp.newTrigger('gsOnChange').forSpreadsheet(SpreadsheetApp.getActive()).onChange().create()
  }

The add-on will be used by thousands of users in thousands of spreadsheets. I expect that trigger to be created for the current spreadsheet.

  1. So why do I see multiple triggers in my source script project? I would expect that trigger to be bound to the spreadsheet it was created for.
  2. Is there any better approach to handle "on sheet rename" events in Sheets add-ons?

Many thanks in advance!!!

enter image description here

Upvotes: 0

Views: 156

Answers (1)

Gabriel Carballo
Gabriel Carballo

Reputation: 1333

The situation with the Triggers being installed in your add-on script is expected as the triggers are bound to the script and not to the Google sheet. Pretty much your code is currently creating a Trigger based on the active user making changes on the active Spreadsheet. Now, as these triggers are pretty much created by other users you will notice that they can't be deleted as you can only make changes to Triggers that are created by your account. Now, related to Trigger creation there is a limit of 20 per user per script that you can verify in this documentation.

Now, if your add-on is meant to modify or check the specific change for spreadsheet of the users you need to consider that the quotas that I posted above, however if it's only one trigger that it is required per user this shouldn't reach the limit of trigger creation as it's based on user created trigger meaning that a user can only create up to 20 triggers on the same script, in your scenario it seems that the trigger will be created based on the user running the function.

Related to the approach on how to handle "on sheet rename" events, it would be better to create a separated question.

Upvotes: 1

Related Questions