为每个新提交的表单发送确认电子邮件(每个新行)

时间:2019-01-16 15:34:17

标签: javascript google-sheets automation

我有一个Google表单,该表单在每次提交后都会创建一个新的Google电子表格

对于每次提交,我必须通知18个人中的1个人。我正在尝试自动发送一封电子邮件,而不是手动发送给每个电子邮件,其中包含 new 行中的数据。

但是,发生的事情是:每次创建新行时,都会发送电子邮件,但电子邮件包含每个较旧的行,而不仅仅是新的相关行。

因此,理想情况下

  1. 提交表单,在电子表格中创建新行
  2. 该行(第2、7、8、9列)中的数据并放入电子邮件中。
  3. 这个-并且只有这个-被发送到该人的相应电子邮件。

但是,就目前情况而言,在第一步之后,如果我运行代码,它将通过电子邮件将工作表中每一行的第2、7、8、9列发送给该人。哪个不好:(

注意:表单上是该人的姓名,这就是为什么我在代码中将姓名链接到电子邮件地址的原因。 (我知道这做得很糟糕)

有什么想法可以调整以在电子邮件回复中仅包括新行列数据吗?

    function sendEmail() {

     //setup function
     var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
     var StartRow = 2;
     var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
     var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,14);
     var AllValues = WholeRange.getValues();

     var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange(1, 3).getValue();

        //define who to send to 
        var Person 1 = "person1@email.com";
        var Person 2 = "person2@email.com";
        var Person 3 = "person3@email.com";
        var Person 4 = "person4@email.com";
        var Person 5 = "person5@email.com";
        var Person 6 = "person6@email.com";
        var Person 7 = "person7@email.com";
        var Person 8 = "person8@email.com";
        var Person 9 = "person9@email.com";
        var Person 10 = "person10@email.com";
        var Person 11 = "person11@email.com";
        var Person 12 = "person12@email.com";
        var Person 13 = "person13@email.com";
        var Person 14 = "person14@email.com";
        var Person 15 = "person15@email.com";
        var Person 16 = "person16@email.com";
        var Person 17 = "person17@email.com";
        var Person 18 = "person18@email.com";

var message = "";
 //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[14];

     //if row has been sent, then continue to next iteration
       if (EmailSent == "sent") 
        continue;

     //set HTML template for information
      message +=
        "<p>A student has filled in an independent travel request, please review it and let the office know if you have an issue with this travel. We will be processing this request as soon as possible. Details below: </p>" + "<br><br><br>" +
          "<p><b>Student Name: </b>" + CurrentRow[2] + "</p>" +
          "<p><b>Departure Date </b>" + CurrentRow[7] + "</p>" +
          "<p><b>Return Date </b>" + CurrentRow[8] + "</p>" +
          "<p><b>Accommodation: </b>" + CurrentRow[9] + "</p><br><br>";

    //set the row to look at
      var setRow = parseInt(i) + StartRow;

       //mark row as "sent"
      ActiveSheet.getRange(setRow, 14).setValue("sent");
    }


         //set subject line
         var Subject = "Travel Notification";

      //send the actual email  
      if(CurrentRow[3] == "Person One" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person1,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
     else if(CurrentRow[3] == "Person Two" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person2,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Three" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person3,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
     else if(CurrentRow[3] == "Person Four" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person4,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}

      ///
      else if(CurrentRow[3] == "Person Five" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person5,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Six" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person6,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}

      ///
     else if(CurrentRow[3] == "Person Seven" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person7,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}

      ///
      else if(CurrentRow[3] == "Person Eight" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person8,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Nine" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Peron9,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
     else if(CurrentRow[3] == "Person Ten" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person10,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Eleven" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person11,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Twelve" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person12,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}

      ///
     else if(CurrentRow[3] == "Person Thirteen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person13,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
     else if(CurrentRow[3] == "Person Fourteen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person14,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Fifteen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person15,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Sixteen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person16,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
     else if(CurrentRow[3] == "Person Seventeen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person17,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}
      ///
      else if(CurrentRow[3] == "Person Eighteen" && CurrentRow[13] != "sent" ) {
       MailApp.sendEmail({
          to: Person18,
          cc: "",
          subject: Subject,
          htmlBody: message,
      } )}


    } 

0 个答案:

没有答案