简体   繁体   English

2 时间触发器 - Google 应用脚本

[英]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.我在线查看了 google app script installable trigger docs ( https://developers.google.com/apps-script/support ),其中一个示例显示了 2 如何创建 2 个时间触发器。

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此加载项在此文档中为此 Google 用户帐户创建了太多基于时间的触发器

Please advise请指教

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在这种情况下,下面提到的限制的新位置是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. 在 Google Apps 脚本插件中,无法通过代表同一用户运行的函数在同一文档上创建两个相同类型的触发器。

From https://developers.google.com/apps-script/guides/triggers/installable#limitations (emphasis mine):来自https://developers.google.com/apps-script/guides/triggers/installable#limitations (强调我的):

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.使用常规脚本而不是附加组件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM