需要根据条件或输入谷歌表单将电子邮件发送到不同的电子邮件地址

问题描述 投票:0回答:1

我需要根据从谷歌表单上的一个特定问题中选择的不同条件,通过 Appscript 向不同的人发送电子邮件。例如。所有EBO选择发送到电子邮件addy 1,所有非EBO选择发送到电子邮件addy 2,然后所有选择发送到电子邮件addy 3。下面是我所拥有的,但无论在表单上选择什么选项,它都只发送第一个 if 语句的电子邮件。

function variedEmails() {
  var aSheet = SpreadsheetApp.getActiveSpreadsheet();

//Take the last row. Row is converted to string and put into an array
  var lastSheet = aSheet.getSheetValues(aSheet.getLastRow(), 1, 1, aSheet.getLastColumn());

  var lastString = lastSheet.toString();
  var data = lastString.split(",");

//Take the first column (header values). Row is converted to string and put into an array 
  var headerSheet = aSheet.getSheetValues(1, 1, 1, aSheet.getLastColumn());

  var headerString = headerSheet.toString();
  var headers = headerString.split(",");

//omit the time and Timezone from timestamps in form
  var dateTime = data[0].split(" ");
  var dateTime1 = data[9].split(" ");

  data[0] = dateTime[0] + " " + dateTime[1] + " " + dateTime[2] +  " " + dateTime[3];
  data[9] = dateTime1[0] + " " + dateTime1[1] + " " + dateTime1[2] +  " " + dateTime1[3];

//string to build an HTML table 
  var table = "<table style='border: 1px solid border-collapse: collapse'>";

  for( var i = 0; i < headers.length; i++ ) {
//determine if cell is blank, and only add data if data exists for that cell
    if( data[i] != "" ) {
      table += "<tr style='border: 1px solid'><td style='border: 1px solid'>"+ headers[i] + "</td><td style='border: 1px solid'>" + data[i] + "</td></tr>";
    }
  }

  table += "</table>";

  if(data[4] == "Increase Usage Priority (Ex: EBO)"){
    MailApp.sendEmail({
    to: "[email protected]",
    subject: "Usage Priority - Mass Change Request Form Response",
    htmlBody: table, noReply: true
    })

    if(data[4] != "Increase Usage Priority (Ex: EBO)"){
    MailApp.sendEmail({
    to: "[email protected]",
    subject: "Mass Change Request Form Response",
    htmlBody: table, noReply: true
  })

    MailApp.sendEmail({
    to: "[email protected]",
    subject: "All - Mass Change Request Form Response",
    htmlBody: table, noReply: true
    })
  }

}
}
google-apps-script google-forms
1个回答
0
投票

我实际上是在盯着它看了一个小时后才弄清楚这一点的。我在错误的位置放置了一些右括号,这导致 if 语句相互嵌套,而不是让脚本单独读取每个 if 语句。正确的代码如下:

  if(data[4] == "Increase Usage Priority (Ex: EBO)"){
    MailApp.sendEmail({
    to: "[email protected]",
    subject: "Usage Priority - Mass Change Request Form Response",
    htmlBody: table, noReply: true
    })
  }
  if(data[4] != "Increase Usage Priority (Ex: EBO)"){
    MailApp.sendEmail({
    to: "[email protected]",
    subject: "Mass Change Request Form Response",
    htmlBody: table, noReply: true
    })
  }
  MailApp.sendEmail({
  to: "[email protected]",
  subject: "All - Mass Change Request Form Response",
  htmlBody: table, noReply: true
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.