Home > database >  IF statement returning false even when it's true
IF statement returning false even when it's true

Time:01-20

I made a script to order a spreadsheet rows by ascending date and it should hide all the rows where the month is not the same as we are currently in. Here's the script:

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Diário de Caixa');
  sheet.getRange('A3:M').activate();
  sheet.getActiveRange().sort({column: 6, ascending: true});
  var r = sheet.getRange('C3:C');
  var date = new Date();
  var mesAtual = Utilities.formatDate(date, Session.getScriptTimeZone(), "M");
  var v = r.getValues(); 
  for(var i=v.length 1;i>=0;i--){
    if(v[0,i]!=mesAtual){
    sheet.hideRows(i);
    }
  }
}

By the time I'm writing this question, I have data filled till the row 36, so the script should've been hiding all rows below the row 36, but it's hiding the rows 34, 35 and 36 too, even when their formula and value are equal as the rows 3 to 33 and all of them returns "1", that stands for January.

What's missing?

Thanks in advance!

CodePudding user response:

I believe this v[0,i] should be v[i][0]

and this var r = sheet.getRange(3, 3, sheet.getLastRow() -2); would be better than this var r = sheet.getRange('C3:C'); ... Take a look at this in the debugger. You often get nulls between getLastRow() and maxRows.

You could also use var r = sheet.getRange('C3:C').getValues().filter(e => e[0]); and eliminate the the nulls with falsy and truthy

You can also do a lot of date comparisons with valueOf() and getTime() since they both return milliseconds from a reference date and so you can compare them numerically.

  •  Tags:  
  • Related