javascript - 如何阻止函数每天执行超过 x 次?

标签 javascript google-apps-script google-sheets

在 Google Sheets 脚本中,如何阻止函数执行超过 x 个,例如一天5次?

例如,我有一个触发器,每 20 分钟执行一次函数,根据函数中的条件发送一封电子邮件,但我只希望它每天最多发送此电子邮件 5 次。我该怎么做?

最佳答案

正如其他用户所建议的,您可以在 Project Properties 中存储一个值,并用它来计算脚本已执行的次数。我建议您使用条件将新属性与 5 进行比较。

Remind to convert the property returned result type to Integer, as it is an String by default, otherwise you won't be able to add 1 in each execution.

作为示例,我们可以使用 setProperty修改已经保存的值,例如:

var userProperties = PropertiesService.getUserProperties();
var newValue = +userProperties.getProperty('Execution_times') + 1;

userProperties.setProperty('Execution_times', newValue); // Updates stored value.

关于javascript - 如何阻止函数每天执行超过 x 次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60248907/

相关文章:

python - 如何将数据框从 iPython 复制/粘贴到 Google 表格或 Excel 中?

php - 使用 ajax 的 php 和 javascript 是否可以使用进度条

javascript - Grunt usemin 转换 Assets 路径

google-apps-script - Tasks.Tasklists.list 中仅返回 20 个项目

google-apps-script - 将文本、图像、表格、所有格式、边距从 GDoc 复制到另一个

google-apps-script - 将 gzip 压缩的数据导入 Google 表格

image - 在谷歌电子表格中使图像可点击

javascript - 设置间隔随机+超时

javascript - 如何在 meteor cordova 中添加插件变量?

google-apps-script - getRangeByName 停止工作!文档损坏或已知问题?