Arvind
Arvind

Reputation: 153

How do I hide all sheets except one in Google Sheets?

I have a spreadsheet which is used by others everyday. Everyday a script creates a number of additional sheets in it. The end user doesn't have access to create, delete, hide or unhide the sheets.

Due to this many sheets get piled up over the period of time and it becomes difficult to navigate through sheets. As part of cleanup I want to use Google Apps script to hide all sheets except the one named "Instructions". I came across this function in google documentation. Not sure how to use it to loop through all the sheets and hide them.

Upvotes: 2

Views: 8013

Answers (1)

erwan
erwan

Reputation: 111

You have to list all sheets and hide only which one have not the good name :

function main(){
   hideAllSheetsExcept('Instructions');
}
function hideAllSheetsExcept(sheetName) {
  var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();

  for(var i =0;i<sheets.length;i++){
    Logger.log(i);
    if(sheets[i].getName()!=sheetName){
      sheets[i].hideSheet();
    }
  }
}

Upvotes: 5

Related Questions