Google Apps脚本:如何根据E列中的值从A列中提取值并在一封电子邮件中发送所有值?

时间:2014-06-27 21:36:05

标签: email google-apps-script

我正在尝试为学生出勤电子表格创建一个脚本,该表格将在E列中查找字符串“X”。对于每个“X”实例,来自A列的字符串(学生姓名)将被添加到电子邮件正文中。虽然我一直在研究基础知识,但我对JavaScript很陌生。我已经做了很多研究,发现了一些我能够修改的脚本,可以为E中的每个X实例发送一封个人电子邮件。但是,我无法弄清楚如何将这些信息合并到一封电子邮件中。

这是我到目前为止所拥有的:

function Email_ReminderNS() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
      EMAIL_SENT = "EMAIL_SENT",
      statusArray = sheet.getDataRange().getValues();
  var class = statusArray[0][8], 
      status = "X",
      email   = "XXXX"

  for (i=7;i < statusArray.length;i++){
    var emailSent = statusArray[i][84];
    if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {

      var student = statusArray[i][0];
      var body    = "This is a No-Show Report for " +student+ " from " + class;
      var subject = "No-Show Report for " + student+ " from " + class;
      MailApp.sendEmail(email,subject,body,{NoReply : true});
      sheet.getRange(i+1, 85).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    }
  }
}

我意识到我可能需要将sendEmail函数移到IF语句之外。我尝试创建一个带有名称的数组,并将它们连接成一个字符串并将其添加到电子邮件的正文中,但我没有运气。它最终发送了姓氏而不是所有的名字。

如果有人对我有任何建议,我将深表感激。

2 个答案:

答案 0 :(得分:0)

首先设置变量以跟踪哪个学生没有出现:

var students = [];
var student_rows = [];

然后,在找到X时将学生添加到这些数组中:

if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {
  var student = statusArray[i][0];
  students.push(student);
  student_rows.push(i+1);
}

然后发送包含所有学生姓名的电子邮件(就像你说的那样在for循环之外)

var body    = "This is a No-Show Report for " + students.join(', ') + " from " + class;
var subject = "No-Show Report for " + students.join(', ') + " from " + class;
MailApp.sendEmail(email,subject,body,{NoReply : true});

最后更新电子表格,指明该电子邮件中的名称:

for (var i=0; i<student_rows.length; i++) {
  sheet.getRange(student_rows[i], 85).setValue(EMAIL_SENT);
  SpreadsheetApp.flush();
}

这是完整的脚本:

function Email_ReminderNS() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
      EMAIL_SENT = "EMAIL_SENT",
      statusArray = sheet.getDataRange().getValues();
  var class = statusArray[0][8], 
      status = "X",
      email   = "francis@bposolutions.com";

  var students = [];
  var student_rows = [];

  for (i=7;i < statusArray.length;i++){
    var emailSent = statusArray[i][84];

    if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {
      var student = statusArray[i][0];
      students.push(student);
      student_rows.push(i+1);
    }
  }

  var body    = "This is a No-Show Report for " + students.join(', ') + " from " + class;
  var subject = "No-Show Report for " + students.join(', ') + " from " + class;
  MailApp.sendEmail(email,subject,body,{NoReply : true});

  for (var i=0; i<student_rows.length; i++) {
    sheet.getRange(student_rows[i], 85).setValue(EMAIL_SENT);
    SpreadsheetApp.flush();
  }
}

答案 1 :(得分:0)

实现新版本代码的方法可能很多,另一个答案可能有效,但我认为可以改进(有点)。 首先,你可以摆脱flush方法,除了减慢功能之外别无其他功能(它最初在Google示例中用于逐行检查发送状态,当我们只发送一封邮件时,它是无用的其中的数据)

其次,使用html格式获得更好看的结果可能是个好主意。

最后,最好使用一个setValues而不是循环中的多个setValue()来回写工作表。

这是一个可能的替换代码,您必须根据需要“调整”它以最终改进消息格式,但主要结构在那里并正常工作。

function Email_ReminderNS() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
      statusArray = sheet.getDataRange().getValues();
  var email = Session.getActiveUser().getEmail(); //replace with the email you want, this value will send mails to you I used it for test.
  var class = statusArray[0][8], 
      status = "X",
      students = [];
  for (var i=7;i < statusArray.length;  i++){
    var emailSent = statusArray[i][84];
    if (status == statusArray[i][4] & emailSent != "EMAIL_SENT") {
      students.push(statusArray[i][0]);
      statusArray[i][84]="EMAIL_SENT";
    }
  }
  var subject = "No-Show Report for " + students.length + " from " + class;
  var textBody = "This is a No-Show Report for " +students.length+ " from " + class+"\n";
  var HTMLBody = "<b>This is a No-Show Report for " +students.length+ " from " + class+"</b><br><br>"
  +'<table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>Sent Mails</th><tr>';
  for(var n in students){
    HTMLBody += '<tr><td>'+n+'</td><td>'+statusArray[n][0]+'</td></tr>';
    textBody += '\n'+n+' - '+statusArray[n][0];
  }
  HTMLBody+='</table><BR> kind regards.' ;
  textBody+='\n\nKind regards';
  Logger.log(HTMLBody);
  Logger.log(textBody);
  MailApp.sendEmail(email,subject,textBody,{'NoReply' : true, 'htmlBody' : HTMLBody});
  sheet.getRange(1,1,statusArray.length,statusArray[0].length).setValues(statusArray);
}