当H列中的“状态”下拉菜单设置为“已完成”时,我试图向A列中提供的地址发送电子邮件。这是我到目前为止的内容:
function onOpen() {
sendemail();
}
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';
var COMPLETED = 'Completed';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendemail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 400; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 3); //grabbing ranges
of values to get
var data = dataRange.getValues(); //getting values
var status = sheet.getRange(startRow, 8, numRows, 1); //grabbing ranges of
//values to get
var data_status = status.getValues(); //getting values
//logic: if a field is populated and both Column C isn't populated, and
//Status is Completed, populate corresponding row in column C and send email.
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = "";
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT && data_status == COMPLETED) { // Prevents
sending duplicates
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is
interrupted
SpreadsheetApp.flush();
}
}
}
此代码受Google的影响很大,我能够从H列中获取所有信息,我不确定自己在做什么错。问题是它不起作用。如果我取出if语句的and部分,它将正常工作,并且如果我调试代码,我可以看到H列给我的值数组。对于每个“完成”的值,我都需要它发送电子邮件,但是如果状态设置为“已完成”并且列C的值为EMAIL_SENT,则我不希望发送电子邮件
答案 0 :(得分:1)
我认为您的脚本已接近完成。我认为该脚本可以通过修改一部分来工作。那修改呢?
data_status
是二维数组。并且,当将每行的值与“ COMPLETED”进行比较时,整个数组都在进行比较。为了在脚本中反映出上述内容,请进行以下修改。
来自:if (emailSent != EMAIL_SENT && data_status == COMPLETED) {
至 :
if (emailSent != EMAIL_SENT && data_status[i][0] == COMPLETED) {
如果这不起作用,请告诉我。我想修改它。