如何使用Google脚本发送一封包含多行数据的电子邮件

时间:2019-12-20 00:08:16

标签: google-apps-script google-sheets

我相信有一个类似的问题已经发布(here),但我迷路了/感到困惑。我本想要求提供完整的代码以避免发布此问题,但我无法这样做。

我下面有这段代码,该代码向具有数据的几个人发送电子邮件。收件人会收到多封邮件,但只希望一封包含多行(基于设置值)的电子邮件。

我已经为HTML中的消息外观创建了单独的脚本代码。

我想完成的是:

  1. 检查请求者列。

  2. 获取行数据(将在HTML表中打印)

  3. 转到下一行请求者的电子邮件,如果该电子邮件与先前的电子邮件相同,则不发送新电子邮件,而是添加数据(依此类推)。

  4. 如果下一行与上一行不同,请先中断并发送包含表数据的电子邮件,然后发送包含新数据的新电子邮件。


    function testEmail(){
      var test = 'Test Data';
      var ss, s, start, last, range, rValues,
          row, sponsor, item, location, serialNumber, status, sentEmail, requester, 
          followUpEmail, followUpDate, today, dateString, followupSent;

      ss = SpreadsheetApp.openById(spreadsheetId);
      s = ss.getSheetByName(test);
      start = 2;

      range = s.getRange(start, 1, s.getLastRow(),s.getLastColumn());
      rValues = range.getValues();
      s.sort(12,true); // sorts the requester email address column

      followupSent = "Email Sent";

      for (var i = 0; i < rValues.length-1; i++ ) {
        row = rValues[i];
        sponsor = row[1];
        item = row[2];
        location = row[3];
        serialNumber = row[13];
        status = row[16];
        sentEmail = row[19];
        requester = row[11];
        followUp = row[21]; 
        followUpDate = row[22];  

        today = new Date();
        today.setDate(today.getDate());
        dateString = Utilities.formatDate(today, 'PST', "MM/dd/yyyy");

        /* do not send email */
        if ((status === "Cancelled" ) || (status === "Complete" && followUpEmail === 'Email Sent') || (status === "Deprovision")) 
        { continue; }

        /* Send email */
        /* This is where I would like send only 1 email message with table data rows if email address is same as previous. If email address is different, send a message with table add rows */
        if (status === "Complete" && followUp === '') {

    /* data post in HTML table */
              var group2 = group2;
              var group2 = {
                timeStamp: rValues[i][0],
                sponsor: rValues[i][1],    
                item: rValues[i][2],
                location: rValues[i][3],
                project: rValues[i][8],
                access: rValues[i][9],
                requester: rValues[i][11],
                device_serialNumber: rValues[i][13],
              };
            }
          }
          sendFollowUpEmail(dateString,group2);
          s.getRange(start + i,22,1,2).setValues([ [followupSent,dateString] ]);
          SpreadsheetApp.flush();
        }

0 个答案:

没有答案