3
votes

I'm currently using this script to hide rows containing 0 on col K

function Hide() {

    var s = SpreadsheetApp.getActive()
         .getSheetByName('Sheet1');
        s.getRange('K:K')
        .getValues()
        .forEach(function (r, i) {
            if (r[0] !== '' && r[0].toString()
                .charAt(0) == 0) s.hideRows(i + 1)
        });   
}

Which works perfect, the only thing is that here when I run the script, it hides row by row (now that I have a lot of rows it takes so much time).

Is there a way to change it to work in batch?

2

2 Answers

3
votes

This is the script that makes the magic

  function Hide() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Ventas");
  var currentRange = ss.getRangeByName("RangeCalculation");
  var rangeStart = currentRange.getRow();
  var values = currentRange.getValues();

  var index = 0, rows = 1;
  var show = !(values[0][12] == "" );

  for (var i = 1, length = values.length; i < length; i++) {
    if (values[i][0] == 1 ) {
      if (show) {
        sheet.showRows(rangeStart + index, rows);
        show = false;
        index = i;
        rows = 1;
      } else
        rows++;
    } else {
      if (show)
        rows++;
      else {
        sheet.hideRows(rangeStart + index, rows);
        show = true;
        index = i;
        rows = 1;
      }
    }
  }

  if (show)
    sheet.showRows(rangeStart + index, rows);
  else
    sheet.hideRows(rangeStart + index, rows);
}
2
votes

Instead of hideRows(rowIndex), use hideRows(rowIndex, numRows)

The first form use only one parameter rowIndex, the second use two parameters, rowIndex and numbRows.

Obviously, using the suggested method implies to review the logic of your script.