简体   繁体   English

如何在准确的时间运行谷歌应用脚​​本?

[英]How to run google app script at exact time?

Currently, I want to run my google app script at exact time.目前,我想在准确的时间运行我的谷歌应用脚​​本。 I have already do some reading about this problem and I decide to follow the solution in this post It's possible run Google Sheets script Exact Time Everyday?我已经阅读了有关此问题的一些信息,并决定遵循这篇文章中的解决方案。 可以每天运行 Google 表格脚本准确时间吗? . .

However, every time the time trigger only work only once.但是,每次时间触发器只工作一次。 It will display error for the second time trigger.第二次触发时会显示错误。 I google about this problem and most of them state that is google v8 bug.( https://issuetracker.google.com/issues/150756612 ).我在谷歌上搜索了这个问题,其中大多数都说是 google v8 错误。( https://issuetracker.google.com/issues/150756612 )。

So, I'm decide to downgrade my code to ES5 since that is the only solution from the post( https://issuetracker.google.com/issues/150756612 ).所以,我决定将我的代码降级到ES5 ,因为这是帖子中唯一的解决方案( https://issuetracker.google.com/issues/150756612 )。 But, I'm facing a problem about changing the spread operator(...) to ES5但是,我面临着将spread operator(...)更改为ES5的问题

Here is my code:这是我的代码:

function runTrigger(hours,minutes) {
  
  var today_D=new Date();
  var year=today_D.getFullYear();
  var month=today_D.getMonth();
  var day=today_D.getDate();
  
  day=day+1; 
  
  var pars=[year,month,day,hours,minutes];
  
  var schedule_date=new Date(...pars);
  var hours_reamain=Math.abs(schedule_date - today_D)/36e5;
  
  ScriptApp.newTrigger("testFunction")
   .timeBased()
   .after(hours_reamain*60*60*1000)
   .create()  
}

function deleteTrigger() {
  var triggers=ScriptApp.getProjectTriggers();
  
  var ssId="1d5P4ohgSLtOzuInxq1IYQUsB4GybeYhth5Gj21RI3rA";
  var ss=SpreadsheetApp.openById(ssId);
  
  for(var i=0 ; i< triggers.length ; i++) {
    ss.appendRow([new Date() ,JSON.stringify(triggers[i]), JSON.stringify(triggers[i].getHandlerFunction()) , JSON.stringify(triggers[i].getEventType()) , JSON.stringify(triggers[i].getTriggerSource())])
    if(triggers[i].getHandlerFunction() === "testFunction") {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
}

function setTrigger() { 
    deleteTrigger();
    runTrigger(1,35);
}

function testFunction() {
  //some code at here
  setTrigger();
}

Can anybody teach me how can I change the ...(spread operator) to ES5谁能教我如何将...(spread operator)更改为ES5

What I'm doing so far is throwing a function that will set the trigger to the exact time based on the current time.到目前为止,我所做的是抛出一个函数,该函数将根据当前时间将触发器设置为确切的时间。 I make sure the previous trigger is deleted and create it again.我确保之前的触发器被删除并重新创建。

function actionAtExactHour() {
  var f = 'myFunction'
  ScriptApp.getProjectTriggers().filter(trigger => trigger.getHandlerFunction() == f).forEach(t => ScriptApp.deleteTrigger(t))
  var d = new Date();
  ScriptApp.newTrigger(f)
    .timeBased()
    .after((60 - d.getMinutes()) * 60 * 1000)
    .create()
}
function myFunction(){
  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('A1').setValue(new Date())
}

However, I can't put a trigger on actionAtExactHour to eg fire the trigger daily and have myFunction run every day at the exact time...google will disable that trigger.但是,我不能在 actionAtExactHour 上设置触发器,例如每天触发触发器并让 myFunction 每天在准确的时间运行……谷歌将禁用该触发器。 To do this, I need to run actionAtExactHour myself a few minutes before the exact time I want为此,我需要在我想要的确切时间前几分钟自己运行 actionAtExactHour

It's a javascript Date() constructor这是一个 javascript Date() 构造函数

 var schedule_date=new Date(year,month,day,hours,minutes);

Date() constructor 日期()构造函数

I do not believe one can set a trigger to run at an exact time.我不相信可以设置触发器在准确的时间运行。 My suggestion is to set the trigger to run the hour before the time you would like it to fire off, then, have you function run a loop checking for the exact time, perhaps run every minute.我的建议是将触发器设置为在您希望它触发的时间前一小时运行,然后,让您运行一个循环检查确切的时间,也许每分钟运行一次。

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

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