AQ Amra
AQ Amra

Reputation: 21

2 Time triggers - Google app scripts

i looked at the google app script installable trigger docs online (https://developers.google.com/apps-script/support) , and one of the examples shows how 2 create 2 time triggers.

function createTimeDrivenTriggers() {
  // Trigger every 6 hours.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .everyHours(6)
      .create();

  // Trigger every Monday at 09:00.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(9)
      .create();
}

My code:

function createTimeDrivenTriggers() {
  // Trigger every 1 hours.
  ScriptApp.newTrigger('MainFunctionDaily')
      .timeBased()
      .everyHours(1)
      .create();

  // Trigger every Friday at 13:00.
  ScriptApp.newTrigger('MainFunctionWeekly')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.FRIDAY)
      .atHour(13)
      .create();
}

However when i try to create the triggers, i get an error:

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

Please advise

Upvotes: 2

Views: 3105

Answers (2)

Wicket
Wicket

Reputation: 38180

UPDATE: The documentation for add-ons was moved. In this case the new location of the below referred restriction is https://developers.google.com/gsuite/add-ons/concepts/editor-triggers


On Google Apps Scripts add-ons it's not possible to create two triggers of the same type on the same document by a function ran on behalf of the same user.

From https://developers.google.com/apps-script/guides/triggers/installable#limitations (emphasis mine):

Limitations
Several limitations apply to installable triggers in add-ons:

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.

The alternatives are

  • Use different accounts to create each trigger.
  • Create several add-ons one for each required trigger
  • Use a regular script rather than an add-on.

Upvotes: 5

csaav
csaav

Reputation: 1

Two possible issues:

  1. Your triggers are contradicting each other
  2. You may need to add a create function before the second trigger.

You will probably need to choose one of your triggers only but you can try this:

function createTimeDrivenTriggers() {
  // Trigger every 6 hours.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .everyHours(6)
      .create();
}

function createTimeDrivenTriggers() {
  // Trigger every Monday at 09:00.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(9)
      .create();
}

Upvotes: -2

Related Questions