1
votes

I found this solution but am struggling to get it to work on my sheet.

The user who submitted that question had 3 header rows and wanted the script to only work on row 4 and down. I have 1 header, and as such need the script to work on row 2 and down.

I've got it leaving row 1 alone - but it ONLY hides rows 2 and 3. I can't figure out where I'm going wrong.

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuItems=[{name: 'HideRows', functionName: 'hideRows'}];
  ss.addMenu('Hide Rows', menuItems);    
};

function hideRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Responses");
  var v = s.getRange("B:B").getValues();
  var today = new Date();
  var m = today.getMonth();
  for(var i=3;i<v.length;i++)
    if(v[i][0]=="" || v[i][0].getMonth()>=m) break;
  if(i>1) s.hideRows(2,i-1)
};

ETA: Here's a link to my sheet/script: https://docs.google.com/spreadsheets/d/1PkB1_hlJoI-iFYTAN8to_ES9R8QyUxEgPsWtSTUmj8U/edit?usp=sharing

3

3 Answers

0
votes

You have a syntax error on your for loop as well as the if statements. There should be {} the code to be worked on when in these portions. Then there is an error in your logic. The month of October in row 2 is greater than the current month, February. So you may want to first compare Years for the highest year and then compare months:

function hideRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Responses");
  var v = s.getRange("B:B").getValues();
  var today = new Date();
  var m = today.getMonth();
  var y = today.getYear();
  for(var i=3;i<v.length;i++){
    if(v[i][0] === "" || v[i][0].getYear() >= y){
      if(v[i][0].getMonth() >= m) {
        var stophere = 0;
        break;
      }
    }
  }
  if(i>1) {
    s.hideRows(2,i-1);
  }
};
0
votes
function hideRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Responses');
  // start range from B2 skipping header
  // join() + split(',') converts 2D array into plain array
  // filter(Boolean) skips all the blank cells in column
  var v = s.getRange('B2:B').getValues().join().split(',').filter(Boolean);
  // today in milliseconds for date comparison
  var today = new Date().getTime();
  // one month in milliseconds
  var oneMonth = 2629746000;
  Logger.log(v.length);
  for (var i=0;i<v.length;i++) {
    // Date Object from cell in B2:B in milliseconds  
    var vDate = new Date(v[i]).getTime();
    // exit for loop when first date less than one month is found 
    if (today - vDate <= oneMonth) {
      break;
    }
  }
  Logger.log(i+1);
  s.hideRows(2, i);
}
0
votes
function onOpen(e) {
var ss =  SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Responses');
var today= new Date();
var today=today.setDate(today.getDate()-30); //minus 30 days

for ( var i = sheet.getLastRow(); i >= 2 ; i-- ) { 
   var filter = sheet.getRange(i, 2).getValue();//Evaluates Column B
  if ( filter.valueOf() < today.valueOf()) {
  sheet.hideRows(i);  }
  }
}