Hide or show a sheet in Google Sheets using Apps Script
In this tutorial, I will show you how to hide a sheet in a Google Sheets spreadsheet using Apps Script. I will also show you how to show or activate a previously hidden sheet.
Prerequisites
This tutorial assumes that you're familiar with the following concepts:
Note
To hide a sheet in a Google Sheets spreadsheet, we will use the hideSheet()
method of the Sheet object. To show a previously hidden sheet, we will use either the showSheet()
method or the activate()
method of the Sheet object.
The function hideSheet()
below accepts one parameter, which is the name of the sheet to hide.
First, we get a reference to the sheet by using the getSheetByName(sheetName)
method of the Sheet class and then we hide it using the hideSheet()
method.
function hideSheet(sheetName) {
SpreadsheetApp.getActive().getSheetByName(sheetName).hideSheet();
}
To use the function, just call it with the name of the sheet to hide.
// Hide a sheet named "WIP Analysis"
hideSheet("WIP Analysis");
To show a previously hidden sheet, you can either use the showSheet()
method or the activate()
method. If you use the showSheet()
method, then the sheet will be shown but it will not be activated. If on the other hand, you use the activate()
method, the sheet will be shown and will also be made active.
function showSheet(sheetName) {
SpreadsheetApp.getActive().getSheetByName(sheetName).showSheet();
}
function activateSheet(sheetName) {
SpreadsheetApp.getActive().getSheetByName(sheetName).activate();
}
You cannot hide all of the sheets in a Google Sheets spreadsheet
Google Sheets will not let you hide all of the sheets in the spreadsheet. Apps Script enforces this rule. So, you'll get an error if you try and hide all of the sheets.
Conclusion
In this tutorial, I showed you how to hide or show a sheet in Google Sheets using Apps Script.
Thanks for reading!
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!