尝试将电子邮件发送到Google表格中的列表,但前提是列N标记为“是”。 N列中的可接受数据为“是”或“否”,因此我想测试它并仅将电子邮件发送到包含“是”的rowData。然后在同一行的相邻列中写入日期。我似乎无法弄清楚如何遍历对象数组,并找不到任何好的资源来解释这一点。非常感谢。我尽最大努力通过电子邮件发送所有行,然后填写日期,无论N列是什么(是/否/空白)。
function sendEmails() {
validateMySpreadsheet() //a function that checks for "Yes" in column N
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getActiveSheet();
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 16);
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth() + 1; //Months are zero based
var yyyy = d.getFullYear();
var date = mm + "/" + dd + "/" + yyyy;
var needsaYes = "Yes";
//Gets the email template
var templateSheet = ss.getSheetByName("Template");
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
//This is where I am stuck - how to check if column N contains a "Yes" before allowing the MailApp.SendEmail command to run.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
var values = dataRange.getValues();
for (var j = 0; j < values.length; ++j) {
var row = values[j];
var checkFirst = row[13]; //row J, Column N?
if (checkFirst = needsaYes) { //does column N contain "Yes"?
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "mySubject";
MailApp.sendEmail(rowData.email, emailSubject, emailText);
dataSheet.getRange(2 + i, 15).setValue(date); //then write the date
SpreadsheetApp.flush();
}
}
}
}
答案 0 :(得分:0)
我没有检查整个代码,但是
if (checkFirst = needsaYes)
这可能是问题所在,您需要使用==
答案 1 :(得分:0)
我想通了我只需要将.getValues()方法用于我的数据范围而不是整张表,并删除不必要的第二个for循环:
var rowData = objects[i]; var checkData = ss.getActiveSheet().getDataRange().getValues();
var row = checkData[i]
var colN = row[13] if (colN == needsaYes) { //etc.