如何实现IF不正确

时间:2017-02-13 15:54:15

标签: javascript arrays google-sheets

情况:

以下脚本将检查N列是否有数据,如果从N列第3行开始的任何单元格有数据,脚本将提取数据并通过电子邮件发送给我。
但是,如果从N列的ROW3开始的任何单元格都没有数据,那么在所述"亲爱的团队的电子邮件正文中显示,目前的任何问题都是"

我该怎么做?我尝试使用,如果没有,但我认为这不是正确的方法。

我的脚本如下:

function emailv1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var Support = ss.getSheetByName("Shift");
  var lastrow = Support.getLastRow();

  var data1 = Support.getRange(3, 1, lastrow).getValues(); // XML ID
  var data2 = Support.getRange(3, 2, lastrow).getValues(); // SEQ NAME
  var data3 = Support.getRange(3, 14, lastrow).getValues(); // Notes
  var date2 = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Funtion Date + Format

  //var name = Browser.inputBox('Monitor', 'Ingrese el Horario Monitoreado', Browser.Buttons.OK_CANCEL);

/////////////////////// FOLLOWUP TABLE /////////////////////////
  var report = " "
  var supportTable ="";
  for(var i in data3)
  {
     if(data3[i]!="") 

      { supportTable += "<tr><td style='color:blue;padding:5px'>" + data1[i]
                                      + "</td><td style='padding:5px'>" + data2[i] 
                                      + "</td><td style='padding:5px'>" + data3[i] 
                                      + "</td></tr>";


  report ="Dears: <br><br><b>The following sequences were reported with faults:</b><br><br><table style='border-collapse:collapse;'border = 1 cellpadding = 5; align='center'><tr><th style='background-color:black;color:white'>XML ID</th><th style='background-color:black;color:white'>SEQUENCER</th><th style='background-color:black;color:white'>Notes</tr>" + supportTable + "</table>";
      }
  }   

/////////////////////////////////////////////////////////////
/////////////////////// URL SHIFT ////////////////////////////
  var report4 = "<br><H1 align='center'><a href=https://docs.google.com/spreadsheets/d/XXXXXX>URL SHIFT</a></H1>"

/////////////////////////////////////////////////////////////

  MailApp.sendEmail("XXXX@gmail.com",
                    "[CUSTOMER NAME] Shift Change - " + date2, 
                    report,
                    {name: 'Automatic Emailer Script',
                    htmlBody: report +'<br><br><br><br>'+ report4});


  Support.getRange('N3:N133').clearContent(); // I want to put all cell in column N but I don't know what but for that razon I put a range.

}

我会感谢任何帮助,我尝试了以下

else if(data3[i]="") {report="text"]; //not working
else if(data3[i]=="") {report="text"]; //not working
else if(data3[i]==="") {report="text"]; //not working

2 个答案:

答案 0 :(得分:1)

从您的评论中,您似乎想要为代码添加新功能。当N列为空时,您会收到一封电子邮件,上面写着“亲爱的团队,目前没有任何问题”(不知道这意味着什么!)。

如果你在supportTable中有任何数据,我将代码修改为cehck:

if (supportTable == ""){
   report = "Dear Team, any problem for the moment" 
  } else {

    report ="Dears: <br><br><b>The following sequences were reported with faults:</b><br><br><table style='border-collapse:collapse;'border = 1 cellpadding = 5; align='center'><tr><th style='background-color:black;color:white'>XML ID</th><th style='background-color:black;color:white'>SEQUENCER</th><th style='background-color:black;color:white'>Notes</tr>" + supportTable + "</table>";
  }

如果你愿意,你甚至可以在这里使用@AlGoreRythm的isEmpty(str)。如果该变量中没有数据,则将电子邮件的正文设置为上述字符串。 编辑:我之前错过的东西是getValues提供了一个2D数组数据,访问你必须使用两个索引的数据而不是之前的数据(例如:data [i] [0]而不是data [i])

 function emailv1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var Support = ss.getSheetByName("Shift");
  var lastrow = Support.getLastRow();
  Logger.log("LastRow: "+lastrow)
  var data1 = Support.getRange(3, 1, lastrow-2).getValues(); // XML ID
  var data2 = Support.getRange(3, 2, lastrow-2).getValues(); // SEQ NAME
  var data3 = Support.getRange(3, 14, lastrow-2).getValues(); // Notes
  var date2 = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Funtion Date + Format

  //var name = Browser.inputBox('Monitor', 'Ingrese el Horario Monitoreado', Browser.Buttons.OK_CANCEL);

/////////////////////// FOLLOWUP TABLE /////////////////////////
  var report = " "
  var supportTable ="";

  for(var i in data3)
  {  


     if(data3[i][0]!="") 

      { supportTable += "<tr><td style='color:blue;padding:5px'>" + data1[i][0]
                                      + "</td><td style='padding:5px'>" + data2[i][0]
                                      + "</td><td style='padding:5px'>" + data3[i][0] 
                                      + "</td></tr>";



      }
    else {

      Logger.log("Empty Row: " + i) 
    }
  }   

/////////////////////////////////////////////////////////////
/////////////////////// URL SHIFT ////////////////////////////
  // Check to see if supoortTable has any data in it, if not set the text to "Dear Team, any problem for the moment" 
  if (supportTable == ""){
   report = "Dear Team, any problem for the moment" 
  } else {

    report ="Dears: <br><br><b>The following sequences were reported with faults:</b><br><br><table style='border-collapse:collapse;'border = 1 cellpadding = 5; align='center'><tr><th style='background-color:black;color:white'>XML ID</th><th style='background-color:black;color:white'>SEQUENCER</th><th style='background-color:black;color:white'>Notes</tr>" + supportTable + "</table>";
  }
var report4 = "<br><H1 align='center'><a href=https://docs.google.com/spreadsheets/d/XXXXXX>URL SHIFT</a></H1>"
Logger.log(report)
/////////////////////////////////////////////////////////////

  MailApp.sendEmail("xxxx@gmail.com",
                    "[CUSTOMER NAME] Shift Change - " + date2, 
                    report,
                    {name: 'Automatic Emailer Script',
                    htmlBody: report +'<br><br><br><br>'+ report4});


  Support.getRange('N3:N133').clearContent(); // I want to put all cell in column N but I don't know what but for that razon I put a range.

}

答案 1 :(得分:0)

你可以有这个功能

function isEmpty(str) {
    return (!str || 0 === str.length);
}

将检查是否有任何字符串为空。如果您的字符串有" ",那么这将失败。如果您需要检查字符串是否为空或不包含空格,您可以这样做:

function isEmptyBrute(str) {
    str = str.replace(/\s/g, '');
    return (!str || 0 === str.length);
}

然后您可以简单地执行此操作:

//if it's empty it will return true
if(isEmpty(data3[i])) ...

工作示例: https://jsfiddle.net/dL0h93ew/