gpt4 book ai didi

javascript - 如何将参数传递给基于时间的 Google App Script 触发器?

转载 作者:数据小太阳 更新时间:2023-10-29 04:06:00 25 4
gpt4 key购买 nike

在我的脚本中,我从电子表格中读取数据并创建一个基于时间的触发器,以便在特定时间使用其中一些数据发出 POST 请求。

问题是,我找不到任何方法将数据传递给触发器调用的函数。 Google App Script 文档提供的全部功能是能够命名要调用的函数,但无法向其传递参数。

 var triggerDay = new Date(2012, 11, 1);
ScriptApp.newTrigger("makePostRequest")
.timeBased()
.at(triggerDay)
.create();

有谁知道我如何传递 makePostRequest 参数以便函数将使用所需数据执行?

最佳答案

这是可能的,但需要多个步骤。这里最重要的是event objects (@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);
}

如果您也将脚本属性用于其他值,您可能必须嵌套触发器值。

此外,您可能必须使用 script lock以防止同时修改脚本属性。

关于javascript - 如何将参数传递给基于时间的 Google App Script 触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com