Search code examples
google-sheetsgoogle-apps-scripttriggers

How many time based trigger can your add-on run on a document / user?


I don't understand what are the real limits of App Script time-based triggers.

Let's say I need to create two different time based triggers on a document for a user using my add-on. I want to use two triggers because it is for two different features and they need different internals & management.

I did a simple test:

function testTriggers() {
  var n = 10;

  SpreadsheetApp.getUi()
      .alert('Creating triggers' + n);

  createTriggers(n);
}

function createTriggers(n) {
  for(var i = 0; i < n; i++){
    ScriptApp.newTrigger('myTriggerFunction' + i)
        .timeBased()
        .everyHours(i+1)
        .create();
  }
}

function myTriggerFunction(){
  console.info("STARGED trigger");
  for(var i = 0; i < 10; i++){
    Utilities.sleep(10 * 1000);
    console.info('time: ' + i);
  }
  console.info("ENDED trigger");
}

function myTriggerFunction0() {
  myTriggerFunction();
}

function myTriggerFunction0() {
  myTriggerFunction();
}
function myTriggerFunction1() {
  myTriggerFunction();
}
function myTriggerFunction2() {
  myTriggerFunction();
}
function myTriggerFunction3() {
  myTriggerFunction();
}
function myTriggerFunction4() {
  myTriggerFunction();
}
function myTriggerFunction5() {
  myTriggerFunction();
}
function myTriggerFunction6() {
  myTriggerFunction();
}
function myTriggerFunction7() {
  myTriggerFunction();
}
function myTriggerFunction8() {
  myTriggerFunction();
}
function myTriggerFunction9() {
  myTriggerFunction();
}

When I make testTriggers run I receive the error:

This add-on has created too many time-based triggers in this document for this Google user account.

And I can see in my triggers window that only the first trigger was created, on myTriggerFunction0.

Is this the expected behavior ? if I check the documentation I see nothing about this, on the quota page they only tell you: 20 user / script, which I think is for every document the add-on is installed on at this point.

By looking on stack I've found this answer where it seems that in the past documentation it was stated:

Each add-on can only have one trigger of each type, per user, per document. For instance, in a given spreadsheet, a given user can only have one edit trigger, although the user could also have a form-submit trigger or a time-driven trigger in the same spreadsheet.

Has something changed ?

Also, I've a question about the 90 min / day total runtime stated in the quota page: I think this limit is user based, but is it also document based ? Will 2 triggers on 2 different documents consuming the same "execution time quota" ?


Solution

  • Installable triggers in add-ons have the same set of restrictions applied to installable triggers in other kinds of Apps Script projects, but in addition to these, others are applied to installable triggers in add-ons specifically.

    To answer your questions:

    1. Is this the expected behavior?

    Yes, this is the expected behavior in this case because you are using triggers in an add-on.

    According to the Restrictions for Installabe Triggers in Add-Ons documentation:

    Time-driven triggers cannot run more frequently than once per hour.

    Which may essentially be the reason why you are getting the error message above because you are trying to run more than one time-driven trigger on an hour basis.

    2. Has something changed?

    No, the restriction is still up and valid which means that this restriction overlaps the 20 triggers per user per script one. The latter one refers to the fact that you can have up to 20 triggers for one Apps Script script.

    3. Will 2 triggers on 2 different documents consuming the same "execution time quota"?

    Yes and that it is because the 90 minutes quota applies to the user. This essentially means that the execution time of all your triggers from all your projects must not exceed the 90 minutes mark.

    Reference