Working with worksheet tabs
Worksheet tabs in Excel allow the user to differentiate between different worksheets.
Worksheets in a workbook can be highlighted via the tab name, color, position or the fact that it is active when the user opens the workbook.
The rust_xlsxwriter
library provides a number of methods, explained below, to
emulate this functionality.
Worksheet names
The worksheet name can be set with Worksheet::set_name()
:
Which gives the following output:
Excel applies various rules to worksheet names such as:
- The name must be less than 32 characters.
- The name cannot be blank.
- The name cannot contain any of the characters:
[ ] : * ? / \
. - The name cannot start or end with an apostrophe.
- The name shouldn't be "History" (case-insensitive) since that is reserved by Excel.
- The name must not be a duplicate (case-insensitive) of another worksheet name used in the workbook.
The rules for worksheet names in Excel are explained in the Microsoft Office documentation.
Setting the active worksheet
In Excel the visible worksheet in a group of worksheets is known as the active worksheet. Since only one worksheet is in the foreground at any one time there can only be one active worksheet.
With rust_xlsxwriter
the Worksheet::set_active()
method is used to specify
which worksheet is active. If no worksheet is set as the active worksheet then
the default is to have the first one active, like in Excel.
Here is an example of making the second worksheet active:
Which gives the following output:
If you have a lot of worksheets then they may not all fit on the screen at the
same time. In cases like that the active worksheet will still be visible but its
tab may not be. In those, rare, cases you can use the
Worksheet::set_first_tab()
method to set the first visible tab (not
worksheet) in a group of worksheets.
Setting worksheet tab colors
Another way of highlighting one or more worksheets in Excel is to set the tab
color. With rust_xlsxwriter
this is achieved with
Worksheet::set_tab_color()
and a Color
color:
Which gives the following output:
Hiding worksheets
Sometimes it is desirable to hide worksheets if they contain a lot of
intermediate data or calculations that end user doesn't need to see. With
rust_xlsxwriter
this is achieved with the Worksheet::set_hidden()
method:
Which gives the following output:
In Excel a hidden worksheet can not be activated or selected so
Worksheet::set_hidden()
is mutually exclusive with the Worksheet::set_active()
and Worksheet::set_selected()
methods. In addition, since the first worksheet
will default to being the active worksheet, you cannot hide the first worksheet
without activating another sheet.
Selecting worksheets
A selected worksheet has its tab highlighted. Selecting worksheets is a way of grouping them together so that, for example, several worksheets could be printed in one go.
The Worksheet::set_selected()
method is used to indicate that a
worksheet is selected in a multi-sheet workbook.
A worksheet that has been activated via the Worksheet::set_active()
method will
also appear as selected.
Which gives the following output. Note that Sheet 1 and Sheet2 are selected but Sheet3 isn't: