将电子邮件发送到多个地址的脚本

时间:2016-02-03 01:38:36

标签: javascript email google-apps-script automation google-sheets

当有人填写Google表格中的必填字段时,我正在使用Google表格发送自动预先填写的电子邮件。如何根据用户输入的位置将以下脚本发送到不同的电子邮件?例如,如果用户输入伦敦发送电子邮件至email1@email.com,或者如果用户进入巴黎,请发送电子邮件至email2@email.com。另外,我是否需要为Google表格添加触发器以了解何时启动此脚本?

function fireEmail(e){
  var userName = e.values[1];
  var timeStamp = e.values[0];
  var medalType1 = e.values[2];
  var medalType2 = e.values[3];
  var medalColor = e.values[4];
  var medalRecipient = e.values[5];
  var dateNeeded = e.values[6];
  var medalLocation = e.values[7];


  var emailBodyHTML = [];
  emailBodyHTML += '<b>You have received a medal request from ' + userName + '.</b>';
  emailBodyHTML += '<p>Onboarding Ops Medal Type, If Applicable: ' + medalType1 + '</p>';
  emailBodyHTML += '<p>Service Ops Medal Type, If Applicable: ' + medalType2 + '</p>';
  emailBodyHTML += '<p>Medal Color: ' + medalColor + '</p>';
  emailBodyHTML += '<p>Medal Recipient: ' + medalRecipient + '</p>';
  emailBodyHTML += '<p>Medal Needed by: ' + dateNeeded + '</p>';
  emailBodyHTML += '<p>Please visit <a href="place.com">the medaling document</a> to manage this request.</p>';

  var emailSubject = 'New Medal Request for ' + medalLocation + '!';

  MailApp.sendEmail({
    to: "email1@email.com",
    name: "Medaling Request",
    subject: emailSubject,


    htmlBody: emailBodyHTML    
  });

}

1 个答案:

答案 0 :(得分:1)

要检查位置,您可以使用if-statement(在您的身体之后)

var mailTo;
if (medalLocation == 'London') {
mailTo = 'abc@gmail.com';
} else if (medalLocation == 'Paris' { 
mailTo = 'def@gmail.com';
}  

如果您只有两个位置,还可以使用conditional (ternary) operator

var mailTo = (medalLocation == 'London') ? 'abc@gmail.com' : 'def@gmail.com'

另一方面,如果您有更多地点需要检查,我会建议使用switch statement

var mailTo;
switch(medalLocation) {
case 'London':
    mailTo = 'abc@gmail.com';
    break;
case 'Paris':
    mailTo = 'def@gmail.com';
    break;
case 'Brussels':
    mailTo = 'ghi@gmail.com';
    break; 
default:
    mailTo = 'jkl@gmail.com';
}

然后,在您的邮件发送到&#39;确定后,将邮件部分更改为

 MailApp.sendEmail({
to: mailTo,
name: "Medaling Request",
subject: emailSubject
htmlBody: emailBodyHTML    

});

脚本应该有一个表单提交触发器。 希望这有帮助吗?