google-apps-script - 如果函数已经在运行,如何防止谷歌应用程序脚本触发

标签 google-apps-script triggers

我有一个触发器,每 5 分钟运行一次应用程序脚本函数。有时,该函数运行时间可能超过 5 分钟(但不到 6 分钟),这一情况可能十分之一。如果是这种情况,我不希望触发器再次运行该函数,因为它将重复当前正在执行的操作。我希望它再等一分钟然后运行。如何检查该函数是否正在运行并使用它来允许或禁止触发器?另请注意,我的脚本大多运行大约 3 或 4 分钟(非常短的条目是不同的函数),因此如果我可以提高效率并在提前完成的情况下更快地再次触发,那就太棒了。

statii

最佳答案

将一个持续 10 分钟的值放入缓存中。 在代码末尾,删除 Cache 属性。 当代码运行时,尝试从缓存中获取该值,如果存在,则退出。 我会使用缓存服务而不是属性服务,因为如果代码失败并且属性服务中的值没有更改或删除,那么您的代码可能会完全停止运行。无论代码是否完成,缓存都会在设定的时间超时。

function myCode() {
  var isItRunning;

  isItRunning = CacheService.getDocumentCache().put("itzRunning", "true",600);//Keep this value in Cache for up to X minutes
  //There are 3 types of Cache - if the Apps Script project is not
  //bound to a document then use ScriptCache
  if (isItRunning) {//If this is true then another instance of this
    //function is running which means that you dont want this
    //instance of this function to run - so quit
    return;//Stop running this instance of this function
  }

  //Put the code you want to run here

  CacheService.getDocumentCache().remove("itzRunning");
  
}

关于google-apps-script - 如果函数已经在运行,如何防止谷歌应用程序脚本触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67066779/

相关文章:

Mysql 5.5.5版本触发分隔符

插入后mysql触发发送数据到另一个mysql主机

javascript - 使用 Google Apps Script 从对话框中的输入字段返回值到 code.gs 中的 var

html - 仅将一些变量从 html 表单发送到电子表格

java - 在 Google Sheet 内运行 Java 应用程序

用于批量更新的 SQL Server 更新触发器

c# - 使用 ComboBoxItem 文本作为 Property Setter 的值

javascript - 我可以使用 .trigger ("change") 来在事件处理程序完成之前更新复选框吗?

javascript - 如何在 Google Apps 脚本中访问 HTML 文件的脚本部分中的模板变量?

javascript - 仅在 Google 电子表格的最后一行查找和替换