The purpose of this article is to describe what is Sheets Visibility. It contains the following sections:
Sheets Visibility is a mechanism to change the visibility of certain sheet, in order to be able to show and hide the relevant sheet(s);
There are two available options when hiding sheets. The first option is by setting the sheet Visibility property exposed both by the SheetCollection and WorksheetCollection classes. This property is of type SheetVisibility, an enum describing the types of visibility of the sheets:
- Visible: Visible sheet visibility.
- Hidden: Hidden sheet visibility.
VeryHidden: Very hidden sheet visibility.
The VeryHidden can be set only from the provided API, while the Hidden state is designed for UI purposes.
Example 1: Setting the SheetVisibility to Hidden/VeryHidden
// Change the visibility of the active sheet workbook.ActiveSheet.Visibility = SheetVisibility.Hidden; // OR change the visibility of the first sheet from the SheetCollection workbook.Sheets.Visibility = SheetVisibility.Hidden; // OR change the visibility of the first sheet from the WorksheetCollection workbook.Worksheets.Visibility = SheetVisibility.Hidden;
- a sheet at the specified index;
- a specified sheet;
- an active sheet. (available on SheetCollection only)
Example 2: Hiding sheets using the Hide method
Worksheet worksheet = workbook.Worksheets.First(); // Hiding the first sheet from the WorksheetCollection workbook.Worksheets.Hide(worksheet);
As with hiding, unhiding can be done both through setting the Sheet Visibility property, or by using the Unhide method exposed by the SheetCollection and WorksheetCollection classes.
The following code snippets exemplify the two approaches.
Example 3: Setting the SheetVisibility to Visible
// Change the visibility of the first sheet from the SheetCollection workbook.Sheets.Visibility = SheetVisibility.Visible;
- a sheet at the specified index.
- a specified sheet.
Example 4: Unhiding sheets using the Unhide method
// Unhiding sheet at the specified index workbook.Worksheets.Unhide(0); // OR unhiding the first sheet from the WorksheetCollection workbook.Worksheets.Unhide(workbook.Worksheets.First());