如何将参数传递给google-app-script中的触发函数

时间:2017-04-24 11:30:06

标签: google-apps-script google-docs gmail-api

我有一系列收件人以及必须发送邮件的预定时间。我很想知道是否有某种方法可以创建一个触发器,我可以通过这些参数(收件人)

function send_mail(recipients)
{
  var id = DocumentApp.getActiveDocument().getId();
  for(i=0;i<recipients.length;i++)
 {
   var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
   var param = 
    {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions:true,
    };
   var html = UrlFetchApp.fetch(url,param).getContentText();
   var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" +
                                        "To:" +recipients[i]+"\r\n" +
                                        "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
                                        html+"\r\n\r\n");
   var message = Gmail.newMessage();
   message.raw = raw;
   var sentMsg = Gmail.Users.Messages.send(message, recipients[i]);
 }
}
function schedule_mail(recipients,schedule)
{
  ScriptApp.newTrigger("send_mail").timeBased().at(schedule).create();
}

如果params无法传递给timeBased触发器,那还有其他的解决方法吗?

1 个答案:

答案 0 :(得分:1)

没有直接的方法可以做到这一点。但这是一个解决方法。 注意:这只是指令而不是实际代码。在代码中查看我的评论。

function send_mail(recipients)
{
  ............
  ............
}

function triggerHandler(e){
  var triggerId = e.triggerUid;
  //Iterate through all triggers and get the matching triggerId and corresponding recipient
  send_mail(recipient);
}

function schedule_mail(recipients,schedule)
{
  var trigger = ScriptApp.newTrigger("triggerHandler").timeBased().at(schedule).create();
  var ID = trigger.getUniqueId();
  //Save this ID against the recipient details in user properties
}