2 Time triggers - Google app scripts

谁说我不能喝 提交于 2020-04-30 09:28:49

问题


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


回答1:


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.



回答2:


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();
}


来源:https://stackoverflow.com/questions/37204774/2-time-triggers-google-app-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!