Google sheets Script to Hide Rows in Batch

回眸只為那壹抹淺笑 提交于 2019-12-07 20:45:12

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/46652618/google-sheets-script-to-hide-rows-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!