如何将参数传递给基于时间的Google App脚本触发器? [英] How can I pass a parameter to a time-based Google App Script trigger?

查看:82
本文介绍了如何将参数传递给基于时间的Google App脚本触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my script, I am reading data from a Spreadsheet and creating a time based trigger to make a POST request with some of that data at a specific time.

问题是,我找不到任何将数据传递给触发器调用的函数的方法。 Google App脚本文档提供的所有功能就是可以命名要调用的函数,但无法传递参数。

The problem is, I can't find any way to pass the data to the function that is called by the trigger. All that the Google App Script doc offers is the ability to name the function to call, but no way to pass it parameters.

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

有人知道我如何通过 makePostRequest

推荐答案

这是可能的,但需要多个步骤。这里最重要的是事件对象(由@ St3ph提及)。

This is possible but requires multiple steps. The most important thing here are event objects (mentioned by @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);
}

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

In case you are using the script properties for other values as well you might have to nest the trigger values.

另外,您可能还必须使用脚本锁定,以防止同时修改脚本属性。

Additionally you might have to use the script lock to prevent concurrent modification of the script properties.

这篇关于如何将参数传递给基于时间的Google App脚本触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆