google apps script - hide / unhide rows based on cell value

307 views
Skip to first unread message

Picloan Limited

unread,
Jun 28, 2021, 7:50:51 PM6/28/21
to Google Apps Script Community

I've got a spreadsheet I use for work and been struggling with a hide/show rows depending on a specific cell value. The code i used which is the one below is not working. please help me.


function HideSelect() 
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getSheetByName("kuly");
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var F16 =sh.getRange("C3").getValue();
  for(var i=0;i<vA.length;i++)
  {
    var row=i+1;
    switch(C3)
    {
      case ' REPORT1':
        if(row>=1 && row<=4){sh.showRows(row);}
        if(row>=5 && row<=8){sh.hideRows(row);}
       
        break;
      case ' REPORT2  ':
        if(row>=1 && row<=5){sh.hideRows(row);}
        if(row>=6 && row<=8){sh.showRows(row);}
   
        break;
      case ' REPORT3':
        if(row>=1 && row<=6){sh.hideRows(row);}
        if(row>=7 && row<=8){sh.hideRows(row);}
       
        break;
        case 'REPORT4':
        if(row>=1 && row<=7){sh.hideRows(row);}
        if(row>=8 &){sh.hideRows(row);}
    
        break;
        case 'REPORT5':
        if(row>=1 && row<=8){sh. showRows  (row);}
        
        break;
       
      default:
    }
  }
}












Dan Prendergast

unread,
Jun 28, 2021, 8:04:02 PM6/28/21
to google-apps-sc...@googlegroups.com
I think “switch(c3)” should be “switch(F16)”.  Looks like C3 is the cell you’re updating to REPORT1, etc, the value will be in F16.  

The cell might be case sensitive too.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/965f34f4-6e72-44ea-86b6-4debc1d30afen%40googlegroups.com.

Scott Bennett

unread,
Jun 28, 2021, 8:10:44 PM6/28/21
to google-apps-sc...@googlegroups.com
First thing I notice is you need to change the C3 on your switch statement to the F 16 which is the variable for that cell. Unless I’m missing something. 

Scott Bennett


Sent from my iPhone 

On Jun 28, 2021, at 7:04 PM, Dan Prendergast <danprend...@gmail.com> wrote:



Scott Bennett

unread,
Jun 28, 2021, 8:18:07 PM6/28/21
to google-apps-sc...@googlegroups.com
There also might be a problem in the report five section. There is a single & with nothing after it.  


Scott Bennett


Sent from my iPhone 

On Jun 28, 2021, at 7:10 PM, Scott Bennett <sben...@bbchs.org> wrote:

First thing I notice is you need to change the C3 on your switch statement to the F 16 which is the variable for that cell. Unless I’m missing something. 
Reply all
Reply to author
Forward
0 new messages