Google Apps脚本中用于从Google表格发送电子邮件的if条件不起作用

时间:2017-02-24 16:38:13

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

以下脚本的目的是从Google表格发送电子邮件,但我只希望在条件满足时发送。

在M列(第12行)的电子表格中,值为TRUE或FALSE。根据在单元格内运行的AND脚本,单元格显示TRUE或FALSE。例如,TRUE的一个条件是列P为空(而不是显示EMAIL_SENT)。 AND语句工作正常。

但是当我运行脚本时,无论这种情况如何,它都会发送电子邮件。所以情况出了问题......

我是基于这个教程here ...但是我只是这样运行它并且原来似乎也不适合我,这对我来说没有帮助! / p>

// Create a date object for the current date and time.
var now = new Date();

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
var SENDYESNO = "FALSE";

function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 2;   // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var title = row[0];
var name = row[2];
var link = row[9];
var emailAddress = row[10];  // First column
var message = row[14];       // Second column
var emailSent = row[16];     // Third column
var date = row[17];
var yesNo = row[12];
if (yesNo != SENDYESNO) {  // Prevents sending duplicates
  var subject = "Sending emails from a Spreadsheet";
  GmailApp.sendEmail(emailAddress, subject, "Dear " + title + " " +  name + ", \n\nThis is an email report of your link. \n\nYour link is " + link + " \n\nKind regards,\nName ", {
                     from: "my@email.com",
                     name: "My name"});
  sheet.getRange(startRow + i, 16).setValue(EMAIL_SENT);
  sheet.getRange(startRow + i, 17).setValue(now);
  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
}
}
}

1 个答案:

答案 0 :(得分:0)

我猜你的值是列M是布尔值true / false而不是文本字符串。您正在将它与文本字符串进行比较。尝试更改

var SENDYESNO = "FALSE";

var SENDYESNO = false;

测试布尔值。我还会更改逻辑以测试为true,然后将if测试从!=更改为===

var SENDYESNO = true;
//more code...
if (yesNo === SENDYESNO) { //continue with your code...