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

lskq00tm  于 2023-06-20  发布在  Java
关注(0)|答案(5)|浏览(122)

在我的脚本中,我从电子表格中阅读数据,并创建一个基于时间的触发器,以便在特定时间使用某些数据发出POST请求。
问题是,我找不到任何方法将数据传递给触发器调用的函数。GoogleAppScript文档提供的只是命名要调用的函数的功能,但无法传递参数。

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

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

fykwrbwg

fykwrbwg1#

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

如果您也将脚本属性用于其他值,则可能必须嵌套触发器值。
此外,您可能必须使用脚本锁来防止脚本属性的并发修改。

pengsaosao

pengsaosao2#

从触发器启动函数时不能传递参数。
您必须将此信息存储在某个地方,以便脚本能够找到它。例如,你说我知道你在电子表格中有一些数据,你可以把这些信息放在电子表格中。该函数将管理查找适当信息的方式,具体取决于它被触发的时间。
斯特凡

kuhbmx9i

kuhbmx9i3#

我不确定它是否能解决您的特定问题,但我发现的最方便的解决方法是将带参数的函数 Package 在不带参数的函数中,然后从您在脚本顶层设置的静态变量中获取参数。
您仍然需要在脚本中设置值,但至少可以保持逻辑分离,以便可以使用具有不同值的基函数。

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();
guykilcj

guykilcj4#

这个怎么样?

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();
}
plicqrtu

plicqrtu5#

triggerDay是一个Trigger,它有唯一的id。makePostRequest的第一个参数是时间驱动的事件,它有未记录的属性“triggerUid”,所以正如@St3ph所说,你需要以某种方式存储一对“uniqueid”和“parameters”,并通过“triggerUid”从存储中获取它

相关问题