【问题标题】:How to solve execution time issue of AppScripts for non-G suite users?如何解决非 G Suite 用户的 AppScripts 执行时间问题?
【发布时间】:2021-03-17 21:45:10
【问题描述】:

我有一个对 G Suite 用户运行良好的考勤系统(执行时间为 30 分钟),但我想让它为所有 gmail 用户运行(执行时间为 6 分钟)。当老师在我的 Google 表格页面上按下特定菜单时,学生可以在 10 分钟内通过 Android 应用向 Google 表格报告他们的出勤情况。

整个代码对 G Suite 用户来说运行良好,但我想实现 适用于所有 Gmail 用户。我试图从普通的 Gmail 中执行它,但它 最后显示超时。

我有以下两行代码让学生有 10 分钟的时间回复:

function refreshSheet(){

  //Few other lines of codes are here

  var val = dashboard.getRange("C6").getValue(); //C6 is a dropdown list in Sheets of "Start 1-Period" and "Start 2-Period"
  if (val == "Start 1-Period"){ ScriptApp.newTrigger("onePeriod").timeBased().after(10*60*1000).create();}
  if (val == "Start 2-Period"){ ScriptApp.newTrigger("twoPeriod").timeBased().after(10*60*1000).create();}
  
  //These few lines of codes below within the refreshSheet() should be executed after the above called trigger and its function execution.

  for (var i=0; i<students.length; i++) {
      if (students[i][0] !== '') ss.removeEditor(students[i][0].toString());
  } 
  
  protectionm.remove();  
  SpreadsheetApp.flush();
}

function onePeriod(){}

function twoPeriod(){}

我在这里查看了一些帖子,但没有一个对我有用。我该如何解决?

【问题讨论】:

标签: google-apps-script google-sheets web-applications


【解决方案1】:

您可以做两件事:

  1. 如果您将脚本部署为 WebApp,您可以将其作为“您”执行 - 即来自 GSuite 域的用户,执行限制为 30 分钟。

  2. 以不同的方式设计您的脚本。

  • 不要等待 10 分钟 Utilities.sleep(),而是合并 time-driven trigger
  • 您可以使用trigger builder
  • 这意味着 - 结束当前函数并指定脚本的延续(嵌入在第二个函数中)将在 10 分钟后开始运行
  • 中间的时间不计为执行时间,也不会导致超时。

示例:

function myFirstFunction(){
// do something
//now set-up the trigger:
    ScriptApp.newTrigger("mySecondFunction").timeBased().after(10*60*1000).create();
}

// will be called after the amount of seconds specified in `after()`
function mySecondFunction(){
//now implement the rest of your code
}

【讨论】:

  • 它对我不起作用。对不起。请在帖子中查看我修改后的代码。
  • 触发器根本没有创建。代码有问题吗?
  • 您是否手动运行了refreshSheet() 并且此后还没有创建触发器?检查下Edit-&gt;Current project's triggers。如果尚未创建触发器,则您的 if 语句可能尚未完成。通过在您的ifstatement 中实现一些日志来检查。
猜你喜欢
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 2019-09-21
  • 1970-01-01
相关资源
最近更新 更多