简体   繁体   English

以编程方式为独立的Webapp添加可安装的时间驱动触发器

[英]Programmatically adding installable time-driven trigger for stand-alone webapp

I want my stand-alone web app script to run everyday between 14:00 and 15:00 GMT +4:30 (Asia/Tehran). 我希望我的独立网络应用程序脚本每天在格林尼治标准时间+4:30(亚洲/德黑兰)14:00至15:00之间运行。 It should open a spreadsheet and check a certain cell. 它应该打开电子表格并检查某个单元格。

So far I have the following: 到目前为止,我有以下内容:

function addTrigger() {
  var ssid = "My SpreadSheet ID";
  var ss = SpreadsheetApp.openById(ssid);
  var everyDay = ScriptApp.newTrigger("checkExitTime")

At this stage, both the forSpreadSheet(String key) and timeBased() show up in the list of available methods. 在此阶段,forSpreadSheet(String key)和timeBased()都显示在可用方法列表中。 But when I chain one, the other becomes unavailable. 但是,当我链接一个时,另一个将不可用。 So if I use the forSpreadSheet method, the timeBased() methods vanish and the only methods that show up are create() onChange(), onEdit(), onOpen(), and on onFormSubmit(). 因此,如果我使用forSpreadSheet方法,则timeBased()方法将消失,仅显示的方法是create(),onChange(),onEdit(),onOpen()和onFormSubmit()。 How can I have both? 我怎么都可以?

I would like to have something like: 我想要类似的东西:

var everyDay = ScriptApp.newTrigger("checkExitTime")
            .timeBased().everyDays(1).atHour(14).inTimezone("Asia/Tehran")
            .forSpreadSheet(ss)
            .create()

ScriptApp.newTrigger returns a TriggerBuilder class which further classifies triggers into different type of triggers. ScriptApp.newTrigger返回TriggerBuilder类进一步进行分类触发进入不同类型的触发器。 So when you will call ScriptApp.newTrigger("yourFn").timeBaesd() will return you a ClockTriggerBuilder class whose methods will return you the same ClockTriggerBuilder class for method chaining, but you will remain in that class. 因此,当您调用ScriptApp.newTrigger("yourFn").timeBaesd()将返回ClockTriggerBuilder类,该类的方法将为方法链接返回相同的ClockTriggerBuilder类,但您将保留在该类中。

To access another type of trigger, in your case .forSpreadSheet(ss) will return you SpreadsheetTriggerBuilder class whose builder methods will return the same class for method chaining. 要访问另一种类型的触发器,在您的情况下, .forSpreadSheet(ss)将返回SpreadsheetTriggerBuilder类,其生成器方法将返回用于方法链接的同一类。

In your code: 在您的代码中:

var everyDay = ScriptApp.newTrigger("checkExitTime")
        .timeBased().everyDays(1).atHour(14).inTimezone("Asia/Tehran")
        .forSpreadSheet(ss)
        .create();

Using .forSpreadSheet is unnecessary and does not make any sense since you cannot chain the two methods as they are not available in the object returned. 使用.forSpreadSheet是不必要的,并且没有任何意义,因为您不能链接这两个方法,因为它们在返回的对象中不可用。

I reckon what you wanted to do is to manipulate the spreadsheet when time based trigger runs; 我认为您想做的是在基于时间的触发器运行时对电子表格进行操作。 so you should create a time driven trigger and do your task in its handler function: 因此,您应该创建一个时间驱动的触发器,并在其处理函数中执行任务:

var everyDay = ScriptApp.newTrigger("checkExitTime")
        .timeBased().everyDays(1).atHour(14).inTimezone("Asia/Tehran")
        .create();

//// Handler function of time driven trigger
function checkExitTime(e){
    var ssid = "My SpreadSheet ID";
    var ss = SpreadsheetApp.openById(ssid);
    //// do your task
}

However if you also want spreadsheet triggers like onOpen() , onEdit() etc, you have to initiate them separately like "this" . 但是,如果您还想要电子表格触发器,例如onOpen()onEdit()等,则必须像“ this”一样分别启动它们。

 // Creates an edit trigger for a spreadsheet identified by ID.
 var ssid = "My SpreadSheet ID";
 ScriptApp.newTrigger('myFunction')
     .forSpreadsheet(ssid)
     .onEdit()
     .create();

 // your onEdit event handler
 function myFunction(event){
      // your code..
 }

will execute myFunction when edit event will occur on your spreadsheet. 当您的电子表格上发生编辑事件时,将执行myFunction

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

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