libxlsxwriter
|
An autofilter in Excel is a way of filtering a 2D range of data based on some simple criteria.
The first step is to apply an autofilter to a cell range in a worksheet using the worksheet_autofilter() function:
Or more explicitly using the RANGE() macro:
The worksheet_autofilter()
function defines the cell range that the filter applies to and creates drop-down selectors in the header row.
However, in order to apply a filter condition it is necessary to add filter rules to the columns using the worksheet_filter_column()
, worksheet_filter_column2()
or worksheet_filter_list()
functions:
worksheet_filter_column()
: filter on a single criterion such as "Column ==
East". More complex conditions such as "<=" or ">=" can also be used.worksheet_filter_column2()
: filter on two criteria such as "Column == East
or Column == West". Complex conditions can also be used.worksheet_filter_list()
: filter on a list of values such as "Column in (East, West,
North)".For example you could create a filter like "Column A == East" using a lxw_filter_rule rule and the worksheet_filter_column()
function like this:
Unfortunately, it isn't sufficient to just specify the filter condition. You must also hide the rows that don't match the criteria since Excel doesn't do that automatically when reading a file. With libxlsxwriter you can hide rows using the worksheet_set_row_opt()
function with the lxw_row_col_options hidden
parameter.
The following is an example of how you might filter a data range to match an autofilter criteria:
Note, the if()
statement above is written to match the logic of the criteria in the rule. However you could get the same results with the following simpler, but reversed, logic:
The worksheet_filter_column()
and worksheet_filter_column2()
functions can be used to filter columns in a autofilter range based on simple conditions:
The col
parameter, used in both these functions, is a zero indexed column number and must refer to a column in an existing autofilter created with worksheet_autofilter()
.
The criteria
parameter in lxw_filter_rule can have one of the following values:
.value_string
or .value
value..value_string
or .value
value.The value
parameter is used to set a numeric matching condition while value_string
is used to set a string matching condition. Excel also allows some simple string matching operations:
You can use "*" to match any character or number and "?" to match any single character or number. No other regular expression quantifier is supported by Excel's filters. Excel's regular expression characters can be escaped using "~".
Prior to Excel 2007 it was only possible to have either 1 or 2 filter conditions such as the ones shown above with the worksheet_filter_column()
and worksheet_filter_column()
functions.
Excel 2007 introduced a new list style filter where it is possible to specify one or more "or" style criteria. For example if your column contained data for the months of the year you could filter the data based on certain months:
The worksheet_filter_list()
function can be used to represent these types of filters:
To filter blanks as part of the list use Blanks
as a list item:
As explained above, it isn't sufficient to just specify filters. You must also hide any rows that don't match the filter condition.
For a detailed working example with several different filter types see autofilter.c.