我需要使用Google脚本自动发送电子邮件。如果我没有产品或订单确认的条形码,或者同时没有两者,则需要发送电子邮件。该脚本在我的测试电子表格中有效:
function sendEmails() {
var app = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orders").activate();
var lastRow = app.getLastRow();
//Get Stored Email Subject
var getBarcodeSubject = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(2, 2).getValue();
var getConfSubject = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(3, 2).getValue();
//Get Stored Email Template
var getBarcodeEmail = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(2, 1).getValue();
var getConfEmail = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(3, 1).getValue();
//Get Daily Email Quota
var emailQuota = MailApp.getRemainingDailyQuota();
//Get the number of rows in google sheet and take one off for headings row
var totalEmails = lastRow - 1;
//Check there is enough emails in quota
if(totalEmails > emailQuota){
Browser.msgBox("You have " + emailQuota + " emails left today and you're trying to send " + totalEmails + ". The emails were not sent.");
}else{
for(var i = 2; i<=lastRow; i++){
//loop through each row in spreadsheet and get corresponding email and name
var currentEmail = app.getRange(i, 20).getValue();
//check if there is an email available
if(currentEmail.match('@') === null){
continue;
}
var currentName = app.getRange(i, 1).getValue();
var barcode = app.getRange(i, 15).getValues();
var orderCon = app.getRange(i, 11).getValues();
//replace placeholder with the relative stored name
var getBarcodeBody = getBarcodeEmail.replace("{Name}", currentName);
var getConfBody = getConfEmail.replace("{Name}", currentName);
//Check if barcodes are not currently there
if(barcode.match("n")||barcode.match("N")){
//MailApp.sendEmail(currentEmail, getBarcodeSubject, getBarcodeBody);
Logger.log(getBarcodeBody);
}else if(orderCon.match("n")||orderCon.match("N")){
//MailApp.sendEmail(currentEmail, getConfSubject, getConfBody);
Logger.log(getConfBody);
}//close else if
}//close for loop
}Browser.msgBox("Emails have been sent!");//close else statement
}//close function
但是,将在现实环境中使用的电子表格在保存条形码和订单确认的列中包含日期。由于似乎.match()
不喜欢数字或日期,因此引发了错误。我已经尝试了以下方法来解决此问题,但是它也不起作用:
if(barcode !== 'n' || barcode !== 'N'){
continue;
}else if(orderCon !== 'n' || orderCon !== 'N'){
continue;
}else if(barcode == 'n' || barcode == 'N'){
Logger.log(getBarcodeBody);
//MailApp.sendEmail(currentEmail, getBarcodeSubject, getBarcodeBody);
}else if(orderCon == 'n' || orderCon == 'N'){
Logger.log(getConfBody);
//MailApp.sendEmail(currentEmail, getConfSubject, getConfBody);
}else{
Browser.msgBox("There was an error!");
}
我该如何克服?谢谢!
更新:看来我尝试的最后一个if
语句正在传递所有内容。