根据列值发送电子邮件=是

时间:2019-04-04 18:03:21

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

我有一个Google表格,其中“电子邮件和姓名”列是根据用户提交的表单自动填写的。另外,我还有2列,其中一列包含赠品代码列表,另一列是作为发送电子邮件的批准。

因此,现在我设法创建一个按钮,单击该按钮将发送带有赠品代码的所有电子邮件。

现在,

  1. 如何只将电子邮件发送给“状态”列中值为“ y”的用户?
  2. 此外,在发送电子邮件之后,值“ y”将变为“ d”吗?。以便稍后,如果我再次使用该功能,它将仅发送给新用户。 / li>

这是工作表示例https://docs.google.com/spreadsheets/d/1KkShktBnJoW9TmIzNsAuAJb6XslBrMwJGEJu7xeF1fk/edit?usp=sharing

这是代码

function sendArticleCountEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.setActiveSheet(ss.getSheetByName("Test1"));
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange("A2:E1000");
  var data = dataRange.getValues();
  for (i in data) {
    var rowData = data[i];
    var emailAddress = rowData[1];
    var timeStamp = rowData[0];
    var recipient = rowData[2];
    var code = rowData[3];
    var status = rowData[4];
    var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
    var subject = 'Here is your giveaway code!';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

/**
 * The event handler triggered when opening the spreadsheet.
 * @param {Event} e The onOpen event.
 */
function onOpen(e) {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Send Emails', functionName: 'sendArticleCountEmails'}
  ];
  spreadsheet.addMenu('Send Emails', menuItems);
}

1 个答案:

答案 0 :(得分:1)

只需添加如下if语句:

function sendArticleCountEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.setActiveSheet(ss.getSheetByName("Test1"));
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange("A2:E1000");
  var data = dataRange.getValues();
  for (i in data) {
    var rowData = data[i];
    var emailAddress = rowData[1];
    var timeStamp = rowData[0];
    var recipient = rowData[2];
    var code = rowData[3];
    var status = rowData[4];
    var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
    var subject = 'Here is your giveaway code!';
    if (status == "y") {
        MailApp.sendEmail(emailAddress, subject, message);
        var actualRow = parseInt(i)+2;
        sheet.getRange(actualRow,5).setValue("d");
        }
  }
}