如何将参数传递给基于时间的Google App Script触发器
How can I pass a parameter to a time-based Google App Script trigger?
在我的脚本中,我正在从电子表格中读取数据,并创建一个基于时间的触发器,以便在特定时间使用该数据中的一些进行POST请求。
问题是,我找不到任何方法将数据传递给触发器调用的函数。Google App Script文档提供的所有功能都是为要调用的函数命名,但无法为其传递参数。
var triggerDay = new Date(2012, 11, 1);
ScriptApp.newTrigger("makePostRequest")
.timeBased()
.at(triggerDay)
.create();
有人知道我如何传递makePostRequest
参数,以便函数将执行所需的数据吗?
这是可能的,但需要多个步骤。这里最重要的是事件对象(由@St3ph提到)。
var RECURRING_KEY = "recurring";
var ARGUMENTS_KEY = "arguments";
/**
* Sets up the arguments for the given trigger.
*
* @param {Trigger} trigger - The trigger for which the arguments are set up
* @param {*} functionArguments - The arguments which should be stored for the function call
* @param {boolean} recurring - Whether the trigger is recurring; if not the
* arguments and the trigger are removed once it called the function
*/
function setupTriggerArguments(trigger, functionArguments, recurring) {
var triggerUid = trigger.getUniqueId();
var triggerData = {};
triggerData[RECURRING_KEY] = recurring;
triggerData[ARGUMENTS_KEY] = functionArguments;
PropertiesService.getScriptProperties().setProperty(triggerUid, JSON.stringify(triggerData));
}
/**
* Function which should be called when a trigger runs a function. Returns the stored arguments
* and deletes the properties entry and trigger if it is not recurring.
*
* @param {string} triggerUid - The trigger id
* @return {*} - The arguments stored for this trigger
*/
function handleTriggered(triggerUid) {
var scriptProperties = PropertiesService.getScriptProperties();
var triggerData = JSON.parse(scriptProperties.getProperty(triggerUid));
if (!triggerData[RECURRING_KEY]) {
deleteTriggerByUid(triggerUid);
}
return triggerData[ARGUMENTS_KEY];
}
/**
* Deletes trigger arguments of the trigger with the given id.
*
* @param {string} triggerUid - The trigger id
*/
function deleteTriggerArguments(triggerUid) {
PropertiesService.getScriptProperties().deleteProperty(triggerUid);
}
/**
* Deletes a trigger with the given id and its arguments.
* When no project trigger with the id was found only an error is
* logged and the function continues trying to delete the arguments.
*
* @param {string} triggerUid - The trigger id
*/
function deleteTriggerByUid(triggerUid) {
if (!ScriptApp.getProjectTriggers().some(function (trigger) {
if (trigger.getUniqueId() === triggerUid) {
ScriptApp.deleteTrigger(trigger);
return true;
}
return false;
})) {
console.error("Could not find trigger with id '%s'", triggerUid);
}
deleteTriggerArguments(triggerUid);
}
/**
* Deletes a trigger and its arguments.
*
* @param {Trigger} trigger - The trigger
*/
function deleteTrigger(trigger) {
ScriptApp.deleteTrigger(trigger);
deleteTriggerArguments(trigger.getUniqueId());
}
function example() {
var trigger = ScriptApp.newTrigger("exampleTriggerFunction").timeBased()
.after(5 * 1000)
.create();
setupTriggerArguments(trigger, ["a", "b", "c"], false);
}
function exampleTriggerFunction(event) {
var functionArguments = handleTriggered(event.triggerUid);
console.info("Function arguments: %s", functionArguments);
}
如果您还使用其他值的脚本属性,则可能需要嵌套触发器值。
另外,您可能必须使用脚本锁来防止并发修改脚本属性。
当函数从触发器启动时不能传递参数
您必须将此信息存储在某个地方以允许脚本找到它。例如,你说我知道你在电子表格中有一些数据,你可以把这些信息放在电子表格中。该函数将根据它被触发的时间管理查找适当信息的方式。
Stephane
我不确定它是否会解决您的具体问题,但我发现最方便的解决方案是将带参数的函数包装在没有参数的函数中,然后从静态变量中获取参数,您在脚本的顶层设置。
您仍然必须在脚本中设置值,但至少您可以保持逻辑分离,以便您可以使用具有不同值的基本函数。
function functionAToTrigger(){
functionToTriggerWithParams(myAParams);
}
var myAParams = {
url: 'https://aurl.com',
date: new Date(2012, 11, 1)
};
function functionBToTrigger(){
functionToTriggerWithParams(myBParams);
}
var myBParams = {
url: 'https://burl.com',
date: new Date(2017, 11, 1)
};
function functionToTriggerWithParams(myParams){
// Add some code to run some checks
// Add some code here to log the results
}
ScriptApp.newTrigger(functionAToTrigger).timeBased().everyMinutes(10).create();
ScriptApp.newTrigger(functionBToTrigger).timeBased().everyMinutes(10).create();
这个怎么样?
function test(a,b) {
GmailApp.sendEmail('your_mail_address',a,b);
}
// Created a new function in global scope.
var test1 = test.bind(null,'aaaaa','bbbb');
function setTrigger(){
ScriptApp.newTrigger('test1').timeBased().after(1 * 60 * 1000).create();
}
triggerDay
是一个唯一的触发器。makePostRequest
的第一个参数是时间驱动事件,它有未记录的属性"triggerUid"。,所以就像刚才说的@St3ph你需要存储一对你的"uniqueid"answers"parameters"并通过"triggerUid"从存储中获取
相关文章:
- 使用Google Maps API驱动时间多边形
- 时间驱动触发器在 Google 电子表格中不起作用
- 如何转换SalesForce Api提供的UTC日期格式,并使用Google Apps脚本将其转换为本地日期和时间格式
- 从Google Maps API返回的jQuery格式时间
- Google 图表中的工具提示:时间轴 API
- 在 Google 图表 y 轴上将时间格式设置为小时:分钟
- 为什么此 Google 日历 API 请求不返回开始时间等事件数据
- 最大限度地减少Google AdWords Script的响应时间
- 防止在 Google 图表 X 轴中舍入日期时间列
- 工作表的 Google 脚本 - 超出最长执行时间
- Google Apps 脚本在保护多个范围时运行时间非常慢
- Google 日历 API 将日期、时间和事件名称返回给 Javascript
- 将Google日历事件结束时间添加到脚本中
- 如何使用google-api-nodejs-client在时间轴中插入附件
- Google Feed API日期到HTML5日期时间
- 如何将参数传递给基于时间的Google App Script触发器
- 在Google图表中显示Y轴上的格式化时间
- Google应用引擎:同步所有服务器'时间和客户'javascript计时器
- 生成一个类似于Google Tasks API的RFC 3339时间戳
- Google App Script mailapp . sendmail()邮件配额或时间限制