简体   繁体   English

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

[英]Send Email based on column value = yes

I have a Google Sheet where the Email & Name columns are auto-fill based on user submitted form.我有一个 Google 表格,其中电子邮件和姓名列是根据用户提交的表单自动填充的。 Also, I have 2 more columns where one column contain the list of giveaway code and another column is to act as an approval to send the email.此外,我还有两列,其中一列包含赠品代码列表,另一列用于批准发送电子邮件。

So for now I manage to create a button where when click it will send all emails with the giveaway code.所以现在我设法创建一个按钮,当点击它时,它将发送带有赠品代码的所有电子邮件。

Now,现在,

  1. How can I only send the email to the user that only has a value "y" in the Status column?如何仅将电子邮件发送给“状态”列中只有“y”值的用户?
  2. Also, after sending the emails, the value "y" will changed to "d"?另外,发送电子邮件后,值“y”将更改为“d”? So that later, if I use the function again, it will only sending to the new users.这样以后,如果我再次使用该功能,它只会发送给新用户。

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

This is the code这是代码

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);
}

Just add an if statement like this:只需添加这样的 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");
        }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM