Google Apps Script recursive schedule trigger

假如想象 提交于 2020-08-09 09:10:16

问题


I am trying to programmatically add a trigger to a Google Apps Script - I have a function main() I want to schedule, which wraps another timebased trigger call using the ScriptApp.newTrigger().create() call like this

 function main(){
/*
* do stuff
*/

doScriptCallback();
}
 function doScriptCallback(){
  if(CONFIG.CALLBACK_SCRIPT_NAME != ''){
    try {
      ScriptApp.newTrigger(CONFIG.CALLBACK_SCRIPT_NAME)
      .timeBased()
      .after(5000)
      .create()
      Logger.log('Scheduled ' + CONFIG.CALLBACK_SCRIPT_NAME);
    } catch(e) {
      Logger.log(e);
    }
  }
}

running main() from the editor correctly runs doScriptCallback() and schedules the function in CONFIG.CALLBACK_SCRIPT_NAME

But if I schedule main() then the doScriptCallback only logs the message, but the function does not run.

Is this a restriction in Google Apps script?


回答1:


This appears to be a bug!

There is already a report on Google's Issue Tracker which detail the same kind of behaviour:

  • Installed triggers are disabled when created from another trigger function in V8

Google does seem to know about this issue but if it's causing problems you can file your own bug about it here.

You can also hit the ☆ next to the issue number in the top left on the aforementioned pages which lets Google know more people are encountering this and so it is more likely to be seen to faster - it already appears that this is a commonly encountered issue.

Workaround:

In the mean time it appears you can disable V8 runtime in Apps Script, and this will run your triggers in the Rhino run time. The reports only affect V8, and I can confirm I was able to run the function created by the nested trigger in Rhino.

You can disable V8 by following the Run > Disable new Apps Script runtime powered by Chrome V8.

I hope this is helpful to you!



来源:https://stackoverflow.com/questions/61590985/google-apps-script-recursive-schedule-trigger

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