可用于doGet()函数的变量? GAS电子邮件批准WebApp

时间:2015-01-05 10:34:45

标签: google-apps-script

我已经创建了一个WebApp来自动化信用卡费用的审批流程。这样做有用,但我想进一步加强技巧。我希望返回/批准电子邮件包含请求电子邮件的信息收集,但我似乎无法访问doGet函数中发送邮件功能中创建的变量。我该怎么做?

脚本/ WebApp:

function CCDEmail(e) {
  var Explain = e.values[1];
  var Cost = e.values[2];  
  var Items = e.values[3];
  var theDate = e.values[4];
  var emailfrom = e.values[5];
  var emailto = e.values[6];

  var url = 'https://script.google.com/macros/s/AKfycbxnhUpURschXwP_rVFSUv673DtHcfuAvNkMTYHpBHU/dev';

  var approve = url + '?approval=true'+'&reply='+emailfrom; 
  var reject = url + '?approval=false'+'&reply='+emailfrom;

  var html = "<body>"+
             "<h2>Please review Credit Card Expenses</h2><br />"+
             "Month:"+ theDate+ "<br />"+
             "Total Cost:"+ Cost + "<br />" + "List of items:"+ Items+ "<br />"+
             "Explanation of Other purchases:"+ Explain+ "<br />"+
             "<br />"+
             "<a href="+ approve +">Approve</a><br />"+
             "<a href="+ reject +">Reject</a><br />"+
             "</body>";

  MailApp.sendEmail(emailto, "Credit Card Declaration " +theDate, "What no html?", {htmlBody: html});  
}



function doGet(e){
  //add date and other variables from above
  var answer = (e.parameter.approval == 'true') ? 'Approved' : 'Please see me!'; 

  MailApp.sendEmail(e.parameter.reply, "Credit Card Declaration", "Your manager said: "+ answer);   
  //add send email to accounts

  var app = UiApp.createApplication();
  app.add(app.createHTML('<h2>An email was sent to '+ e.parameter.reply + ' saying: '+ answer + '</h2>'))
  return app
}

提前致谢,

1 个答案:

答案 0 :(得分:0)

第一个问题是,“您是否会从CCDEmail(e)致电doGet(),或者CCDEmail(e)功能是否会在以后独立运行。

如果独立于CCDEmail(e)调用doGet()函数,则需要一种方法将值存储在doGet()函数之外。有三种方法可以做到这一点:1)缓存2)全局变量。 3)属性

缓存是临时的,因此根据打开应用程序与运行CCDEmail(e)函数之间的时间间隔,您可能不想使用缓存。

有些人强烈反对使用全局变量,因此根据您的意见,您可能会也可能不会想要使用它们。

属性很好用,但如果你有很多数据,你可能会达到极限。

可能首先使用“属性”选项。

doGet()中声明并设置一些变量,然后检索CCDemail()函数中的值:

function doGet(e){
  //add date and other variables from above
  var answer = (e.parameter.approval == 'true') ? 'Approved' : 'Please see me!'; 

  var Explain = e.values[1];
  var Cost = e.values[2];  
  var Items = e.values[3];
  var theDate = e.values[4];
  var emailfrom = e.values[5];
  var emailto = e.values[6];

  // Set multiple script properties in one call.
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperties({
    'argExplain': Explain,
    'argCost': Cost,
    'argItems': Items'
  });



  MailApp.sendEmail(e.parameter.reply, "Credit Card Declaration", "Your manager said: "+ answer);   
  //add send email to accounts

  var app = UiApp.createApplication();
  app.add(app.createHTML('<h2>An email was sent to '+ e.parameter.reply + ' saying: '+ answer + '</h2>'))
  return app
}

要检索值:

function CCDEmail() {

  // Get multiple script properties in one call, then log them all.
  var scriptProperties = PropertiesService.getScriptProperties();
  var data = scriptProperties.getProperties();

  var Explain = data['argExplain'];
  var Cost = data['argCost'];  
  var Items = data['argItems'];
  var theDate = data['argtheDate'];
  var emailfrom = data['argemailfrom'];
  var emailto = data['argemailto'];

  Logger.log(data['argExplain']);

  var url = 'https://script.google.com/macros/s/AKfycbxnhUpURschXwP_rVFSUv673DtHcfuAvNkMTYHpBHU/dev';

  var approve = url + '?approval=true'+'&reply='+emailfrom; 
  var reject = url + '?approval=false'+'&reply='+emailfrom;

  var html = "<body>"+
             "<h2>Please review Credit Card Expenses</h2><br />"+
             "Month:"+ theDate+ "<br />"+
             "Total Cost:"+ Cost + "<br />" + "List of items:"+ Items+ "<br />"+
             "Explanation of Other purchases:"+ Explain+ "<br />"+
             "<br />"+
             "<a href="+ approve +">Approve</a><br />"+
             "<a href="+ reject +">Reject</a><br />"+
             "</body>";

  MailApp.sendEmail(emailto, "Credit Card Declaration " +theDate, "What no html?", {htmlBody: html});  
}

我没有测试这段代码。它可能有错误,但希望如何完成你想要的基本想法是正确的。