2016-12-03 13:16:45 +00:00
|
|
|
|
# AutoFilter Reference
|
|
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Each worksheet in an Excel Workbook can contain a single autoFilter
|
|
|
|
|
range. Filtered data displays only the rows that meet criteria that you
|
|
|
|
|
specify and hides rows that you do not want displayed. You can filter by
|
|
|
|
|
more than one column: filters are additive, which means that each
|
|
|
|
|
additional filter is based on the current filter and further reduces the
|
|
|
|
|
subset of data.
|
|
|
|
|
|
|
|
|
|
![01-01-autofilter.png](./images/01-01-autofilter.png)
|
|
|
|
|
|
|
|
|
|
When an AutoFilter is applied to a range of cells, the first row in an
|
|
|
|
|
autofilter range will be the heading row, which displays the autoFilter
|
|
|
|
|
dropdown icons. It is not part of the actual autoFiltered data. All
|
|
|
|
|
subsequent rows are the autoFiltered data. So an AutoFilter range should
|
|
|
|
|
always contain the heading row and one or more data rows (one data row
|
|
|
|
|
is pretty meaningless), but PhpSpreadsheet won't actually stop you
|
|
|
|
|
specifying a meaningless range: it's up to you as a developer to avoid
|
|
|
|
|
such errors.
|
|
|
|
|
|
|
|
|
|
To determine if a filter is applied, note the icon in the column
|
|
|
|
|
heading. A drop-down arrow
|
|
|
|
|
(![01-03-filter-icon-1.png](./images/01-03-filter-icon-1.png)) means
|
|
|
|
|
that filtering is enabled but not applied. In MS Excel, when you hover
|
|
|
|
|
over the heading of a column with filtering enabled but not applied, a
|
|
|
|
|
screen tip displays the cell text for the first row in that column, and
|
|
|
|
|
the message "(Showing All)".
|
|
|
|
|
|
|
|
|
|
![01-02-autofilter.png](./images/01-02-autofilter.png)
|
|
|
|
|
|
|
|
|
|
A Filter button
|
|
|
|
|
(![01-03-filter-icon-2.png](./images/01-03-filter-icon-2.png)) means
|
|
|
|
|
that a filter is applied. When you hover over the heading of a filtered
|
|
|
|
|
column, a screen tip displays the filter that has been applied to that
|
|
|
|
|
column, such as "Equals a red cell color" or "Larger than 150".
|
|
|
|
|
|
|
|
|
|
![01-04-autofilter.png](./images/01-04-autofilter.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
## Setting an AutoFilter area on a worksheet
|
|
|
|
|
|
|
|
|
|
To set an autoFilter on a range of cells.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$spreadsheet->getActiveSheet()->setAutoFilter('A1:E20');
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
The first row in an autofilter range will be the heading row, which
|
|
|
|
|
displays the autoFilter dropdown icons. It is not part of the actual
|
|
|
|
|
autoFiltered data. All subsequent rows are the autoFiltered data. So an
|
|
|
|
|
AutoFilter range should always contain the heading row and one or more
|
|
|
|
|
data rows (one data row is pretty meaningless, but PhpSpreadsheet won't
|
|
|
|
|
actually stop you specifying a meaningless range: it's up to you as a
|
|
|
|
|
developer to avoid such errors.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
If you want to set the whole worksheet as an autofilter region
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$spreadsheet->getActiveSheet()->setAutoFilter(
|
|
|
|
|
$spreadsheet->getActiveSheet()
|
|
|
|
|
->calculateWorksheetDimension()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This enables filtering, but does not actually apply any filters.
|
|
|
|
|
|
|
|
|
|
## Autofilter Expressions
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
PHPEXcel 1.7.8 introduced the ability to actually create, read and write
|
|
|
|
|
filter expressions; initially only for Xlsx files, but later releases
|
|
|
|
|
will extend this to other formats.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
To apply a filter expression to an autoFilter range, you first need to
|
|
|
|
|
identify which column you're going to be applying this filter to.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
|
|
|
|
|
$columnFilter = $autoFilter->getColumn('C');
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
This returns an autoFilter column object, and you can then apply filter
|
|
|
|
|
expressions to that column.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
There are a number of different types of autofilter expressions. The
|
|
|
|
|
most commonly used are:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
- Simple Filters
|
|
|
|
|
- DateGroup Filters
|
|
|
|
|
- Custom filters
|
|
|
|
|
- Dynamic Filters
|
|
|
|
|
- Top Ten Filters
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
These different types are mutually exclusive within any single column.
|
|
|
|
|
You should not mix the different types of filter in the same column.
|
|
|
|
|
PhpSpreadsheet will not actively prevent you from doing this, but the
|
|
|
|
|
results are unpredictable.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Other filter expression types (such as cell colour filters) are not yet
|
|
|
|
|
supported.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
### Simple filters
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
In MS Excel, Simple Filters are a dropdown list of all values used in
|
|
|
|
|
that column, and the user can select which ones they want to display and
|
|
|
|
|
which ones they want to hide by ticking and unticking the checkboxes
|
|
|
|
|
alongside each option. When the filter is applied, rows containing the
|
|
|
|
|
checked entries will be displayed, rows that don't contain those values
|
|
|
|
|
will be hidden.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-01-simple-autofilter.png](./images/04-01-simple-autofilter.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
To create a filter expression, we need to start by identifying the
|
|
|
|
|
filter type. In this case, we're just going to specify that this filter
|
|
|
|
|
is a standard filter.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setFilterType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Now we've identified the filter type, we can create a filter rule and
|
|
|
|
|
set the filter values:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
When creating a simple filter in PhpSpreadsheet, you only need to
|
|
|
|
|
specify the values for "checked" columns: you do this by creating a
|
|
|
|
|
filter rule for each value.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
'France'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
'Germany'
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
This creates two filter rules: the column will be filtered by values
|
2017-03-13 05:57:37 +00:00
|
|
|
|
that match "France" OR "Germany". For Simple Filters, you can create as
|
2016-12-03 15:00:54 +00:00
|
|
|
|
many rules as you want
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Simple filters are always a comparison match of EQUALS, and multiple
|
|
|
|
|
standard filters are always treated as being joined by an OR condition.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
#### Matching Blanks
|
|
|
|
|
|
|
|
|
|
If you want to create a filter to select blank cells, you would use:
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### DateGroup Filters
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
In MS Excel, DateGroup filters provide a series of dropdown filter
|
|
|
|
|
selectors for date values, so you can specify entire years, or months
|
|
|
|
|
within a year, or individual days within each month.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-02-dategroup-autofilter.png](./images/04-02-dategroup-autofilter.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
DateGroup filters are still applied as a Standard Filter type.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setFilterType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Creating a dateGroup filter in PhpSpreadsheet, you specify the values
|
|
|
|
|
for "checked" columns as an associative array of year. month, day, hour
|
|
|
|
|
minute and second. To select a year and month, you need to create a
|
|
|
|
|
DateGroup rule identifying the selected year and month:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
array(
|
|
|
|
|
'year' => 2012,
|
|
|
|
|
'month' => 1
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The key values for the associative array are:
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
- year
|
|
|
|
|
- month
|
|
|
|
|
- day
|
|
|
|
|
- hour
|
|
|
|
|
- minute
|
|
|
|
|
- second
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Like Standard filters, DateGroup filters are always a match of EQUALS,
|
|
|
|
|
and multiple standard filters are always treated as being joined by an
|
|
|
|
|
OR condition.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Note that we alse specify a ruleType: to differentiate this from a
|
|
|
|
|
standard filter, we explicitly set the Rule's Type to
|
|
|
|
|
AUTOFILTER\_RULETYPE\_DATEGROUP. As with standard filters, we can create
|
|
|
|
|
any number of DateGroup Filters.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
### Custom filters
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
In MS Excel, Custom filters allow us to select more complex conditions
|
|
|
|
|
using an operator as well as a value. Typical examples might be values
|
|
|
|
|
that fall within a range (e.g. between -20 and +20), or text values with
|
|
|
|
|
wildcards (e.g. beginning with the letter U). To handle this, they
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-03-custom-autofilter-1.png](./images/04-03-custom-autofilter-1.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-03-custom-autofilter-2.png](./images/04-03-custom-autofilter-2.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Custom filters are limited to 2 rules, and these can be joined using
|
|
|
|
|
either an AND or an OR.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
We start by specifying a Filter type, this time a CUSTOMFILTER.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setFilterType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
And then define our rules.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
The following shows a simple wildcard filter to show all column entries
|
2017-03-13 05:57:37 +00:00
|
|
|
|
beginning with the letter `U`.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
'U*'
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
MS Excel uses \* as a wildcard to match any number of characters, and ?
|
|
|
|
|
as a wildcard to match a single character. 'U\*' equates to "begins with
|
|
|
|
|
a 'U'"; '\*U' equates to "ends with a 'U'"; and '\*U\*' equates to
|
|
|
|
|
"contains a 'U'"
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
If you want to match explicitly against a \* or a ? character, you can
|
|
|
|
|
escape it with a tilde (\~), so ?\~\*\* would explicitly match for a \*
|
|
|
|
|
character as the second character in the cell value, followed by any
|
|
|
|
|
number of other characters. The only other character that needs escaping
|
|
|
|
|
is the \~ itself.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
To create a "between" condition, we need to define two rules:
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL,
|
|
|
|
|
-20
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER
|
|
|
|
|
);
|
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL,
|
|
|
|
|
20
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
We also set the rule type to CUSTOMFILTER.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
This defined two rules, filtering numbers that are >= -20 OR <=
|
|
|
|
|
20, so we also need to modify the join condition to reflect AND rather
|
|
|
|
|
than OR.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setAndOr(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_COLUMN_ANDOR_AND
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
The valid set of operators for Custom Filters are defined in the
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule class, and
|
|
|
|
|
comprise:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
Operator Constant | Value
|
|
|
|
|
------------------------------------------|----------------------
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_EQUAL | 'equal'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_NOTEQUAL | 'notEqual'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_GREATERTHAN | 'greaterThan'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL | 'greaterThanOrEqual'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_LESSTHAN | 'lessThan'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL | 'lessThanOrEqual'
|
|
|
|
|
|
|
|
|
|
### Dynamic Filters
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Dynamic Filters are based on a dynamic comparison condition, where the
|
|
|
|
|
value we're comparing against the cell values is variable, such as
|
|
|
|
|
'today'; or when we're testing against an aggregate of the cell data
|
|
|
|
|
(e.g. 'aboveAverage'). Only a single dynamic filter can be applied to a
|
|
|
|
|
column at a time.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-04-dynamic-autofilter.png](./images/04-04-dynamic-autofilter.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
Again, we start by specifying a Filter type, this time a DYNAMICFILTER.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setFilterType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
When defining the rule for a dynamic filter, we don't define a value (we
|
|
|
|
|
can simply set that to NULL) but we do specify the dynamic filter
|
|
|
|
|
category.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
|
|
|
|
|
NULL,
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
We also set the rule type to DYNAMICFILTER.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
The valid set of dynamic filter categories is defined in the
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule class, and
|
|
|
|
|
comprises:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
Operator Constant | Value
|
|
|
|
|
-----------------------------------------|----------------
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_YESTERDAY | 'yesterday'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_TODAY | 'today'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_TOMORROW | 'tomorrow'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE | 'yearToDate'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_THISYEAR | 'thisYear'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_THISQUARTER | 'thisQuarter'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_THISMONTH | 'thisMonth'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_THISWEEK | 'thisWeek'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_LASTYEAR | 'lastYear'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_LASTQUARTER | 'lastQuarter'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_LASTMONTH | 'lastMonth'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK | 'lastWeek'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_NEXTYEAR | 'nextYear'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_NEXTQUARTER | 'nextQuarter'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_NEXTMONTH | 'nextMonth'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_NEXTWEEK | 'nextWeek'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_1 | 'M1'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_JANUARY | 'M1'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_2 | 'M2'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_FEBRUARY | 'M2'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_3 | 'M3'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MARCH | 'M3'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_4 | 'M4'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_APRIL | 'M4'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_5 | 'M5'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MAY | 'M5'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_6 | 'M6'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_JUNE | 'M6'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_7 | 'M7'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_JULY | 'M7'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_8 | 'M8'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_AUGUST | 'M8'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_9 | 'M9'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_SEPTEMBER | 'M9'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_10 | 'M10'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_OCTOBER | 'M10'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_11 | 'M11'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_NOVEMBER | 'M11'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_MONTH_12 | 'M12'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_DECEMBER | 'M12'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_1 | 'Q1'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_2 | 'Q2'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_3 | 'Q3'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_4 | 'Q4'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_ABOVEAVERAGE | 'aboveAverage'
|
|
|
|
|
AUTOFILTER_RULETYPE_DYNAMIC_BELOWAVERAGE | 'belowAverage'
|
|
|
|
|
|
|
|
|
|
We can only apply a single Dynamic Filter rule to a column at a time.
|
|
|
|
|
|
|
|
|
|
### Top Ten Filters
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Top Ten Filters are similar to Dynamic Filters in that they are based on
|
|
|
|
|
a summarisation of the actual data values in the cells. However, unlike
|
|
|
|
|
Dynamic Filters where you can only select a single option, Top Ten
|
|
|
|
|
Filters allow you to select based on a number of criteria:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-05-custom-topten-1.png](./images/04-05-topten-autofilter-1.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
![04-05-custom-topten-2.png](./images/04-05-topten-autofilter-2.png)
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
You can identify whether you want the top (highest) or bottom (lowest)
|
|
|
|
|
values.You can identify how many values you wish to select in the
|
|
|
|
|
filterYou can identify whether this should be a percentage or a number
|
|
|
|
|
of items.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Like Dynamic Filters, only a single Top Ten filter can be applied to a
|
|
|
|
|
column at a time.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
We start by specifying a Filter type, this time a DYNAMICFILTER.
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->setFilterType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then we create the rule:
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT,
|
|
|
|
|
5,
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This will filter the Top 5 percent of values in the column.
|
|
|
|
|
|
|
|
|
|
To specify the lowest (bottom 2 values), we would specify a rule of:
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$columnFilter->createRule()
|
|
|
|
|
->setRule(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE,
|
|
|
|
|
5,
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM
|
|
|
|
|
)
|
|
|
|
|
->setRuleType(
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
The option values for TopTen Filters top/bottom value/percent are all
|
|
|
|
|
defined in the
|
|
|
|
|
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule class, and
|
|
|
|
|
comprise:
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
Operator Constant | Value
|
|
|
|
|
---------------------------------------|-------------
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE | 'byValue'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT | 'byPercent'
|
|
|
|
|
|
|
|
|
|
and
|
|
|
|
|
|
|
|
|
|
Operator Constant | Value
|
|
|
|
|
-------------------------------------|----------
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_TOPTEN_TOP | 'top'
|
|
|
|
|
AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM | 'bottom'
|
|
|
|
|
|
|
|
|
|
## Executing an AutoFilter
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
When an autofilter is applied in MS Excel, it sets the row
|
|
|
|
|
hidden/visible flags for each row of the autofilter area based on the
|
|
|
|
|
selected criteria, so that only those rows that match the filter
|
|
|
|
|
criteria are displayed.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
PhpSpreadsheet will not execute the equivalent function automatically
|
|
|
|
|
when you set or change a filter expression, but only when the file is
|
|
|
|
|
saved.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
### Applying the Filter
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
If you wish to execute your filter from within a script, you need to do
|
2017-03-13 05:57:37 +00:00
|
|
|
|
this manually. You can do this using the autofilters `showHideRows()`
|
2016-12-03 15:00:54 +00:00
|
|
|
|
method.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
$autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
|
|
|
|
|
$autoFilter->showHideRows();
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
This will set all rows that match the filter criteria to visible, while
|
|
|
|
|
hiding all other rows within the autofilter area.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
|
|
|
|
### Displaying Filtered Rows
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
Simply looping through the rows in an autofilter area will still access
|
|
|
|
|
ever row, whether it matches the filter criteria or not. To selectively
|
|
|
|
|
access only the filtered rows, you need to test each row’s visibility
|
|
|
|
|
settings.
|
2016-12-03 13:16:45 +00:00
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
``` php
|
2016-12-03 13:16:45 +00:00
|
|
|
|
foreach ($spreadsheet->getActiveSheet()->getRowIterator() as $row) {
|
|
|
|
|
if ($spreadsheet->getActiveSheet()
|
|
|
|
|
->getRowDimension($row->getRowIndex())->getVisible()) {
|
|
|
|
|
echo ' Row number - ' , $row->getRowIndex() , ' ';
|
|
|
|
|
echo $spreadsheet->getActiveSheet()
|
|
|
|
|
->getCell(
|
|
|
|
|
'C'.$row->getRowIndex()
|
|
|
|
|
)
|
|
|
|
|
->getValue(), ' ';
|
|
|
|
|
echo $spreadsheet->getActiveSheet()
|
|
|
|
|
->getCell(
|
|
|
|
|
'D'.$row->getRowIndex()
|
|
|
|
|
)->getFormattedValue(), ' ';
|
2017-10-01 08:48:59 +00:00
|
|
|
|
echo PHP_EOL;
|
2016-12-03 13:16:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## AutoFilter Sorting
|
|
|
|
|
|
2016-12-03 15:00:54 +00:00
|
|
|
|
In MS Excel, Autofiltering also allows the rows to be sorted. This
|
|
|
|
|
feature is ***not*** supported by PhpSpreadsheet.
|