Hide Rows when a criteria is met

2019-09-21 09:34发布

I have the below script that is to search for 'Dead' or 'Booked' in range "S:S" and hide the row if it is found. If 'Live' is found in a hidden row it should unhide the Row. The below script is working however, it unhides all rows and then completes the function, which can take a while when over 1000 rows are being searched.

function RowHide(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sht2 = ss.getSheetByName("Log");
  var row = sht2.getRange("S:S").getDisplayValues();

  sht2.showRows(1, sht2.getMaxRows()); {
    for (var i = 0; i < row.length; i++) {
      if (row[i][0] == 'Dead') {
        sht2.hideRows(i + 1, 1);
      }
      if (row[i][0] == 'Booked') {
        sht2.hideRows(i + 1, 1);
      } else if (row[i][0] == 'Live') {
        sht2.showRows(i + 1, 1);
      }
    }
  }
}

I need to alter the script to only action rows which require action rather than all of them. Is this possible?

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-21 09:45

Hiding Rows with onEdit trigger

function rowHideProbablyOnEdit(e){
  var ss=e.source;
  var rg=e.range;
  var sh=rg.getSheet();
  if(sh.getName()=="Log"){
    var datR=sh.getDataRange();
    var valA=datR.getValues();
    for(var i=0;i<valA.length;i++){
      if(valA[i][18]=='Dead' || valA[i][18]=='Booked'){
        sh.hideRows(i+1);
      }
    }
  }
}

Hiding and Showing with onEdit Trigger

function rowShowHideProbablyOnEdit(e){
  var ss=e.source;
  var rg=e.range;
  var sh=rg.getSheet();
  if(sh.getName()=="Log"){
    var datR=sh.getDataRange();
    var valA=datR.getValues();
    for(var i=0;i<valA.length;i++){
      if(valA[i][18]=='Dead' || valA[i][18]=='Booked'){
        sh.hideRows(i+1);
      }
      if(valA[i][18]=='Live'){
        Logger.log('Sheet: %s',sh.getName());
        sh.showRows(i+1);
      }
    }
  }
}

Setup and Testing Functions:

function rowLiveSetup(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Log');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  for(var i=0;i<vA.length;i++){
    if(vA[i][18]=='Booked'){
      vA[i][18]='Live';
    }
  }
  rg.setValues(vA);
}

function rowBookedDeadSetup(){
   var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Log');
  var rg=sh.getRange(1,1,180,20);
  var vA=rg.getValues();
  for(var i=0;i<180;i++){
    if(i%3==0){
      vA[i][18]='Booked';
    }
    if(i%3==1){
      vA[i][18]='Dead';
    }
  }
  rg.setValues(vA);
}
查看更多
登录 后发表回答