将参数传递给脚本创建触发器

时间:2016-07-22 18:38:40

标签: google-apps-script

我有一个带参数的函数,并希望为无人参与的函数执行创建脚本创建触发器。



function getLastNdays(nDaysAgo) {
  var today = new Date();
  var before = new Date();
  before.setDate(today.getDate() - nDaysAgo);
  return Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd');
}

function runNextTrigger() {
  
    var GUID = "!Q@W#E$RI"
    var userProperties = PropertiesService.getUserProperties();
    var nextTrigger = ScriptApp.newTrigger("getLastNdays").timeBased().after(1 * 60 * 1000).create();
    
    var triggerId = nextTrigger.getUniqueId();
    userProperties.setProperty("LastNdays_" + GUID, triggerId);    
}




即使创建了触发器,它也没有任何作用,因为没有传递参数值是否有办法将参数传递给脚本创建触发器。

1 个答案:

答案 0 :(得分:0)

我可以为你提出类似的东西

function getLastNdays(before) {
if(!(before instanceof Date)){
  var timestamp = PropertiesService.getUserProperties().getProperty("LastNdays_" +before.triggerUid);
  before = new Date();
  before.setTime(timestamp);
}

  //var today = new Date();
  //var before = new Date();
  //before.setDate(today.getDate() - nDaysAgo);
 return Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd'); // notice return wont return anywhere as it's the function called
 SpreadsheetApp.openById("SSID")
 .getSheetByName("Sheet2").appendRow([new Date(), Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd')]); // used a spreadsheet to log the action
 }

function runNextTrigger() {

    var GUID = "!Q@W#E$RI"
    var userProperties = PropertiesService.getUserProperties();
    var nextTrigger = ScriptApp.newTrigger("getLastNdays").timeBased().after(10).create();

    var triggerId = nextTrigger.getUniqueId();
    //userProperties.setProperty("LastNdays_" + GUID, triggerId);
    var now = new Date();
    userProperties.setProperty("LastNdays_" + triggerId,now.getTime());
}