简体   繁体   中英

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. 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:

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. 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.

You can try using the debugger and set a breakpoint at the IF, so you can check the values it is comparing. or put a Logger.log call to list your comparisons.

    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);
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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