How to Hide Rows Based on Cell Value using Apps Script in Google Sheets

You can hide rows based on cell value using the Apps Script feature of Google Sheets. This is one way of filtering out data from your spreadsheet.

When working with a spreadsheet with lots of data, there are times that you need to hide some records based on cell value. These could be records that have been processed or those that are no longer needed. Google Sheets offers various ways to do this, and the most common is to create a filter that you can use to filter out your records manually.

While this technique gets the job done, it could be cumbersome to use it over time as you make changes to your spreadsheet. There’s actually a more seamless way to achieve this—using the Apps Script extension of Google Sheets.

 

 

What is Apps Script in Google Sheets?

Apps Script is a Google Sheets extension that lets you create your own programs. You can use Apps Script to create menus, dialogs, and custom functions to automate processes. This extension is completely free, and it’s available out of the box when you launch Google Sheets.

 

 

A Real Example of Hiding Rows Using Apps Script

For you to appreciate what Apps Script can do, let’s use it in a real-world scenario. Let’s take the spreadsheet below as an example.

A Real Example of Hiding Rows Using Apps Script

Based on the data, we can assume that the spreadsheet serves as a tracker for people who have a remaining balance. Suppose we want to hide the records that have been paid, we can create a custom menu similar to the one below using the Apps Script extension.

Filter Rows Custom Menu

Ideally, when the Filter rows command is clicked, it will automatically hide all those whose remaining balance has been paid.

Hiding rows using Apps Script in Google Sheets

Notice that some of the records above, particularly those that were checked earlier, have been hidden from the spreadsheet.

Hiding rows based on cell value is just one of the many things you can do in Google Sheets using its Apps Script extension. In the next section, we’ll tackle the step-by-step process on how we can achieve this. Click the link below to make a copy of our example spreadsheet.


How to Hide Rows Based on Cell Value using Apps Script in Google Sheets

For this tutorial, you’ll learn how to create a custom menu that contains two commands using Apps Script. The first command filters out or hides the records that have been marked as paid, while the other command shows all the records.

  1. Start by opening a copy of the example spreadsheet provided earlier. Upon opening, you should have this dataset:
    Example spreadsheet
  2. You’ll notice that the third column (column C) already contains checkboxes. Perhaps you’re already aware that when you use a checkbox in Google Sheets, you’ll be dealing with two states—check and uncheck. Behind the scenes, Google Sheets treat them as True and False. So, when you check a checkbox, Google Sheets will tag it as True, and False if otherwise. Keep this information in mind as we will use it later for this tutorial.
  3. Next, take note also of the name of the sheet where you intend to place the custom menu. In our case below, it’s ‘Sheet1’.
    Sheet name
  4. At this point, we are now ready to create our custom menu. To do this, click the Extensions menu, and choose Apps Script.
    Accessing the Apps Script extension in Google Sheets
  5. Upon clicking, your browser will open a new tab that looks like this:
    Apps Script interface

    As mentioned earlier, Apps Script is used for creating programs, so, expect that we’ll be dealing with codes for the succeeding steps. Don’t worry if you’re not quite familiar with coding in Apps Scripts as you’ll be guided every step of the way.
  6. Within the Apps Script interface, you’ll see that there’s already a premade function named ‘myFunction()’.
    Hide rows using Apps Script in Google Sheets

    In the world of programming, a function is simply a block of codes that can be used anytime to accomplish a specific task. You’ll notice that the default function doesn’t contain anything, so it’s useless now. Delete the default function to make space for the functions that we will create.
    Hide rows using Apps Script in Google Sheets
  7. Since we aim for two commands (hiding and showing records), we’ll need to create two separate functions. Each function will have its own set of codes that will do a specific task.
  8. For the first function, we’ll name it ‘filterRows()’. This function will hide or filter out the records that are tagged as paid.
    Type or copy the following codes on your editor:

    function filterRows() {  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1")
    
      var data = sheet.getDataRange().getValues();
    
      for(var x=1; x<data.length; x++) {
    
        if (data[x][2]===true) {
    
          sheet.hideRows(x+1);
    
        }
    
      }
    
    }

    Once you’re done typing or pasting, your editor should already look like this:
    Hide rows using Apps Script in Google Sheets


    So, what does this block of codes means?
    For the first line, we referenced the Sheet’s name, Sheet1, and assigned it to a variable named ‘sheet’. This is where we want our function to operate.
    Assigning the current sheet to variable sheet

    Next, we tried to get all the values from Sheet1 and assigned them to the variable called ‘data’.
    Hide rows using Apps Script in Google Sheets

    Afterwards, we used the for loop to iterate through all the elements or rows of our data. Within this part, we tested if the third column of each element contains ‘true’, which is signified by a check mark on its checkbox. If it does, the ‘hideRows()’ method automatically hides the entire row from the spreadsheet.
    Hide rows using Apps Script in Google Sheets

    We are now done with the function to hide a row based on the value of its checkbox.
  9. Now, let’s create another function that we can call anytime we want to unhide all rows and name it as showAll(). Below the function we have created earlier, type or copy the following codes:
    function showAll() {  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
    
      sheet.showRows(1, sheet.getMaxRows());
    
    }

    Your editor should now look like this:
    Hide rows using Apps Script in Google Sheets


    You might be wondering, how does the ‘showAll()’ function work?

    Like our previous function, we referenced the sheet where we want the ‘showAll()’ function to work in the first line.
    Referencing the current sheet to variable sheet


    After that, we used the ‘showRows()’ method to unhide all rows within the sheet.
    Using the showRows method

    Good job! We are now done with our two functions. But, there’s one more thing we need to do. We need to develop a way for users to access these functions. In our case, the best way to achieve this is to have a custom menu. That’s what we are going to do in the next steps.
  10. In order for our spreadsheet to have a custom menu, we’ll need to create another function, called ‘onOpen()’. This function will contain all the codes needed to create a functional menu. Below the two functions we created earlier, copy or type the following codes:
    function onOpen() {  SpreadsheetApp.getUi().createMenu("Filter Rows")
    
        .addItem("Filter rows", "filterRows")
    
        .addItem("Show all", "showAll")
    
        .addToUi();
    
    }

    Your editor should now be similar to this:
    The onOpen function in Apps Script

  11. At this point, you should now have three functions in your editor. Save all your changes by clicking the Save project button located on top of your editor.
    Saving the project

Great! We just created a simple program that will hide rows based on a cell value and show them anytime we want. In the next section, you will learn how to use this program.

 

 

How to Hide or Show Rows using the Custom Menu

It’s time to test the Custom Menu program we have created earlier to hide or show rows in our spreadsheet.

  1. First, make the Remaining Balance spreadsheet active by clicking its tab on your browser.
  2. Then, check if your spreadsheet already has the Filter Rows custom menu, as shown below. If it doesn’t exist yet, try refreshing your web page.
    The Filter Rows custom menu
  3. To hide rows based on cell value, simply click the Filter Rows menu, and choose the Filter Rows command.
    The Filter rows command

    As you click on the command, notice that Google Sheets will automatically hide the records that have been tagged as paid on your spreadsheet.
    Hide rows using Apps Script in Google Sheets
  4. If you want to unhide all rows, click the Filter Rows menu, and select Show All.
    Hide rows using Apps Script in Google Sheets

    Upon clicking, Google Sheets will show all hidden rows in your spreadsheet.
    Hide rows using Apps Script in Google Sheets

Perfect! Now you know how to hide rows based on a cell value and show them anytime using Apps Script in Google Sheets.

Learn more awesome techniques by checking out our other articles about Google Sheets.

Subscribe to our newsletter if you want to receive more useful articles like this one about Google Sheets.

Get emails from us about Excel.

Our goal this year is to create lots of rich, bite-sized tutorials for Excel users like you. If you liked this one, you'd love what we are working on! Readers receive ✨ early access ✨ to new content.

2 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like