简体   繁体   English

IF函数-Google脚本-多个条件

[英]IF Function - Google Scripts - multiple criteria

I'm trying to run an IF function to match the date in the first column to "last month" and the date in the last column to "newest date" and copy and paste all of the rows matching this criteria (excluding the first and last column) to the bottom of the list. 我正在尝试运行IF函数,以将第一列中的日期与“上个月”匹配,将最后一列中的日期与“最新日期”匹配,然后复制并粘贴与该条件匹配的所有行(不包括第一行和第二行)最后一栏)。 This is the script I'm running and it isn't finding any matches when I know for a fact there are at least 100 rows matching this criteria: 这是我正在运行的脚本,当我知道至少有100行符合此条件时,找不到任何匹配项:

function myFunction() {
  var MCS = SpreadsheetApp.openById('[ID REMOVED FOR THIS Q]');
  var MRB = MCS.getSheetByName('Media Rates Back');
  var MRBrange = MRB.getRange(1,1,MRB.getLastRow(),1).getValues();
  var dest = MRBrange.filter(String).length + 1;
  var LM = new Date();
  LM.setDate(1);
  LM.setMonth(LM.getMonth()-1);
  var LMs = Date.parse(LM);
  var Datenew = MRB.getRange(MRB.getLastRow(),MRB.getLastColumn()).getValue();
  var Datecol = MRB.getRange(1,6,MRB.getLastRow(),1).getValues();
  var Datenews = Date.parse(Datenew);

for(var i=0; i<MRBrange.length; i++) {

if(Date.parse(MRBrange[i])==LMs && Date.parse(Datecol[i])==Datenews ) {
  var NewRange = MRB.getRange(i,2,(MRB.getLastRow()-i),5);
  var NewRangeV = NewRange.getValues();
  var destination = MRB.getRange(MRB.getLastRow()+1,2);
Logger.log(NewRange);                                                  

NewRange.copyTo(destination);


}else{
  Logger.log(i);
}
}}

Any help would be appreciated! 任何帮助,将不胜感激!

I think the problem may be that MRBrange is a 2d Array. 我认为问题可能在于MRBrange是2d阵列。 So I used another loop to convert it to a 1d array. 因此,我使用了另一个循环将其转换为一维数组。 Hope this helps. 希望这可以帮助。

  function myFunction() {
  var MCS = SpreadsheetApp.openById('[ID REMOVED FOR THIS Q]');
  var MRB = MCS.getSheetByName('Media Rates Back');
  var MRBrangeA = MRB.getRange(1,1,MRB.getLastRow(),1).getValues();//2d array
  var MRBrange=[];
  for(var i=0;i<MRBrangeA.length;i++)
  {
    MRBrange.push(MRBrangA[i][0]);//1d array
  }

  var dest = MRBrange.filter(String).length + 1;
  var LM = new Date();//current day
  LM.setDate(1);//first day of month
  LM.setMonth(LM.getMonth()-1);//first day of last month
  var LMs = Date.parse(LM);
  var Datenew = MRB.getRange(MRB.getLastRow(),MRB.getLastColumn()).getValue();
  var Datecol = MRB.getRange(1,6,MRB.getLastRow(),1).getValues();
  var Datenews = Date.parse(Datenew);

for(var i=0; i<MRBrange.length; i++) {

if(Date.parse(MRBrange[i])==LMs && Date.parse(Datecol[i])==Datenews ) {
  var NewRange = MRB.getRange(i,2,(MRB.getLastRow()-i),5);
  var NewRangeV = NewRange.getValues();
  var destination = MRB.getRange(MRB.getLastRow()+1,2);
Logger.log(NewRange);                                                  

NewRange.copyTo(destination);


}else{
  Logger.log(i);
}
}}

Rather than get the columns as separate ranges, I would get the entire range as one array, then loop over that and check the two columns. 与其将列作为单独的范围获取,不如将整个范围作为一个数组获取,然后循环遍历并检查两列。

I'm also assuming your values are formatted as dates in the Sheet, in which case you don't need to use Date.parse(), and that your actual date logic is correct. 我还假设您的值在工作表中被格式化为日期,在这种情况下,您无需使用Date.parse(),并且您的实际日期逻辑是正确的。

You can try using the debugger and set a breakpoint at the IF, so you can check the values it is comparing. 您可以尝试使用调试器并在IF处设置断点,以便可以检查它正在比较的值。 or put a Logger.log call to list your comparisons. 或发出Logger.log调用以列出您的比较。

    var last_month_column = 1;
    var newest_date_column = MRB.getLastColumn();

    var MRBrange = MRB.getRange(1,1,MRB.getLastRow(),newest_date_column).getValues();

    for(var row in MRBrange) {
       if(MRBrange[row][last_month_column]==LMs && Datecol[row][newest_date_column] ==Datenews ) {
           /* your copy logic here */
       }else{
           Logger.log(i);
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM