Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google sheets Script to Hide Rows in Batch

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?

like image 325
SATH59 Avatar asked Feb 02 '26 22:02

SATH59


1 Answers

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);
}
like image 113
SATH59 Avatar answered Feb 05 '26 14:02

SATH59



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!