首页 > 解决方案 > 我怎样才能绕过任何日期或数字?

问题描述

我需要使用谷歌脚本自动发送电子邮件。如果我没有产品条形码或订单确认信息,或者两者都没有,我需要发送电子邮件。这个脚本在我的测试电子表格中工作:

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我尝试的最后一个语句是忽略所有内容。

标签: javascriptgoogle-apps-scriptgoogle-sheetsgmail

解决方案


推荐阅读