From 661bba8d833e3562754bfed384b538c8b7169bc3 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 29 Mar 2023 23:47:35 +0200 Subject: [PATCH] Improvements to AutoFilter Examples --- samples/Autofilter/10_Autofilter.php | 8 ++-- .../10_Autofilter_dynamic_dates.php | 11 ++--- .../Autofilter/10_Autofilter_selection_1.php | 46 +++++++++++-------- .../Autofilter/10_Autofilter_selection_2.php | 46 ++++++++++--------- .../10_Autofilter_selection_display.php | 40 ++++++++-------- 5 files changed, 83 insertions(+), 68 deletions(-) diff --git a/samples/Autofilter/10_Autofilter.php b/samples/Autofilter/10_Autofilter.php index db9de54aa..858dd071d 100644 --- a/samples/Autofilter/10_Autofilter.php +++ b/samples/Autofilter/10_Autofilter.php @@ -85,17 +85,19 @@ $dataArray = [ ['2011', 'Q4', 'Italy', 335], ]; $spreadsheet->getActiveSheet()->fromArray($dataArray, null, 'A2'); +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, false, true)); // Set title row bold $helper->log('Set title row bold'); $spreadsheet->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true); // Set autofilter -$helper->log('Set autofilter'); -// Always include the complete filter range! +$filterRange = $spreadsheet->getActiveSheet()->calculateWorksheetDimension(); +$helper->log("Set autofilter for cells {$filterRange}"); +// Always include the complete filter range if you can! // Excel does support setting only the caption // row, but that's not a best practise... -$spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension()); +$spreadsheet->getActiveSheet()->setAutoFilter($filterRange); // Save $helper->write($spreadsheet, __FILE__); diff --git a/samples/Autofilter/10_Autofilter_dynamic_dates.php b/samples/Autofilter/10_Autofilter_dynamic_dates.php index 959925c16..c79e17858 100644 --- a/samples/Autofilter/10_Autofilter_dynamic_dates.php +++ b/samples/Autofilter/10_Autofilter_dynamic_dates.php @@ -41,17 +41,16 @@ function createSheet(Spreadsheet $spreadsheet, string $rule): void $sheet->getColumnDimension('A')->setAutoSize(true); $sheet->getColumnDimension('B')->setAutoSize(true); $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter(); - $autoFilter->setRange("A1:A$row"); + $autoFilter->setRange("A1:A{$row}"); $columnFilter = $autoFilter->getColumn('A'); $columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); $columnFilter->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - '', - $rule - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, '', $rule) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); $sheet->setSelectedCell('B1'); + + $helper->log('Execute filtering (apply the filter rules)'); + $autoFilter->showHideRows(); } // Create new Spreadsheet object diff --git a/samples/Autofilter/10_Autofilter_selection_1.php b/samples/Autofilter/10_Autofilter_selection_1.php index fe4709722..f5b1729a1 100644 --- a/samples/Autofilter/10_Autofilter_selection_1.php +++ b/samples/Autofilter/10_Autofilter_selection_1.php @@ -100,35 +100,38 @@ $spreadsheet->getActiveSheet()->getStyle('E2:F' . $row)->getNumberFormat()->setF $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(14); $spreadsheet->getActiveSheet()->freezePane('A2'); +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, true, true)); + // Set autofilter range -$helper->log('Set autofilter range'); -// Always include the complete filter range! -// Excel does support setting only the caption -// row, but that's not a best practise... -$spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension()); +$filterRange = $spreadsheet->getActiveSheet()->calculateWorksheetDimension(); +$helper->log("Set autofilter for cells {$filterRange}"); +// Always include the complete filter range if you can! +// Excel does support setting only the caption row, but that's not a best practise... +$spreadsheet->getActiveSheet()->setAutoFilter($filterRange); // Set active filters $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter(); $helper->log('Set active filters'); + // Filter the Country column on a filter value of countries beginning with the letter U (or Japan) // We use * as a wildcard, so specify as U* and using a wildcard requires customFilter $autoFilter->getColumn('C') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - 'u*' - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'u*') ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); + +$helper->log('Set country code filter (Column C) to countries beginning with "U" ("United States" and "UK")'); + $autoFilter->getColumn('C') ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - 'japan' - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'japan') ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); + +$helper->log('Add "Japan" to the country code filter (Column C)'); + // Filter the Date column on a filter value of the last day of every period of the current year -// We us a dateGroup ruletype for this, although it is still a standard filter +// We use a dateGroup ruletype for this, although it is still a standard filter foreach ($periods as $period) { $dateString = sprintf('%04d-%02d-01T00:00:00', $currentYear, $period); $dateTime = new DateTime($dateString); @@ -147,15 +150,20 @@ foreach ($periods as $period) { ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DATEGROUP); } + +$helper->log('Add filter on the Date (Column D) to display only rows for the last day of each month'); + // Display only sales values that are blank -// Standard filter, operator equals, and value of NULL +// Standard filter, operator equals, and value of NULL or empty space $autoFilter->getColumn('E') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - '' - ); + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, ''); + +$helper->log('Add filter on Sales Values (Column E) to display only blank values'); + +$helper->log('NOTE: We don\'t apply the filter rules in this example, so we can\'t see the result here; although Excel will apply the rules when the file is loaded'); +$helper->log('See 10_Autofilter_selection_display.php for an example that actually executes the filter rules'); // Save $helper->write($spreadsheet, __FILE__); diff --git a/samples/Autofilter/10_Autofilter_selection_2.php b/samples/Autofilter/10_Autofilter_selection_2.php index bd08b1d12..4d5d891cf 100644 --- a/samples/Autofilter/10_Autofilter_selection_2.php +++ b/samples/Autofilter/10_Autofilter_selection_2.php @@ -100,52 +100,54 @@ $spreadsheet->getActiveSheet()->getStyle('E2:F' . $row)->getNumberFormat()->setF $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(14); $spreadsheet->getActiveSheet()->freezePane('A2'); +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, true, true)); + // Set autofilter range -$helper->log('Set autofilter range'); -// Always include the complete filter range! -// Excel does support setting only the caption -// row, but that's not a best practise... -$spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension()); +$filterRange = $spreadsheet->getActiveSheet()->calculateWorksheetDimension(); +$helper->log("Set autofilter for cells {$filterRange}"); +// Always include the complete filter range if you can! +// Excel does support setting only the caption row, but that's not a best practise... +$spreadsheet->getActiveSheet()->setAutoFilter($filterRange); // Set active filters $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter(); $helper->log('Set active filters'); + // Filter the Country column on a filter value of Germany // As it's just a simple value filter, we can use FILTERTYPE_FILTER $autoFilter->getColumn('C') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - 'Germany' - ); + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'Germany'); + +$helper->log('Set country code filter (Column C) to "Germany"'); + // Filter the Date column on a filter value of the year to date $autoFilter->getColumn('D') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - null, - Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, null, Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); + +$helper->log('Add filter on the Date (Column D) to display year to date'); + // Display only sales values that are between 400 and 600 $autoFilter->getColumn('E') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL, - 400 - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL, 400) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); + $autoFilter->getColumn('E') ->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL, - 600 - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL, 600) ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); +$helper->log('Add filter on Sales Values (Column E) between 400 and 600'); + +$helper->log('NOTE: We don\'t apply the filter rules in this example, so we can\'t see the result here; although Excel will apply the rules when the file is loaded'); +$helper->log('See 10_Autofilter_selection_display.php for an example that actually executes the filter rules inside PhpSpreadsheet'); + // Save $helper->write($spreadsheet, __FILE__); diff --git a/samples/Autofilter/10_Autofilter_selection_display.php b/samples/Autofilter/10_Autofilter_selection_display.php index c3995b3a9..39b41e924 100644 --- a/samples/Autofilter/10_Autofilter_selection_display.php +++ b/samples/Autofilter/10_Autofilter_selection_display.php @@ -100,12 +100,14 @@ $spreadsheet->getActiveSheet()->getStyle('E2:F' . $row)->getNumberFormat()->setF $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(14); $spreadsheet->getActiveSheet()->freezePane('A2'); +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, true, true)); + // Set autofilter range -$helper->log('Set autofilter range'); -// Always include the complete filter range! -// Excel does support setting only the caption -// row, but that's not a best practise... -$spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension()); +$filterRange = $spreadsheet->getActiveSheet()->calculateWorksheetDimension(); +$helper->log("Set autofilter for cells {$filterRange}"); +// Always include the complete filter range if you can! +// Excel does support setting only the caption row, but that's not a best practise... +$spreadsheet->getActiveSheet()->setAutoFilter($filterRange); // Set active filters $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter(); @@ -115,18 +117,18 @@ $helper->log('Set active filters'); $autoFilter->getColumn('C') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - 'u*' - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'u*') ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); + +$helper->log('Set country code filter (Column C) to countries beginning with "U" ("United States" and "UK")'); + $autoFilter->getColumn('C') ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - 'japan' - ) + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'japan') ->setRuleType(Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); + +$helper->log('Add "Japan" to the country code filter (Column C)'); + // Filter the Date column on a filter value of the last day of every period of the current year // We us a dateGroup ruletype for this, although it is still a standard filter foreach ($periods as $period) { @@ -147,18 +149,20 @@ foreach ($periods as $period) { ) ->setRuleType(Rule::AUTOFILTER_RULETYPE_DATEGROUP); } + +$helper->log('Add filter on the Date (Column D) to display only rows for the last day of each month'); + // Display only sales values that are blank // Standard filter, operator equals, and value of NULL $autoFilter->getColumn('E') ->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER) ->createRule() - ->setRule( - Rule::AUTOFILTER_COLUMN_RULE_EQUAL, - '' - ); + ->setRule(Rule::AUTOFILTER_COLUMN_RULE_EQUAL, ''); + +$helper->log('Add filter on Sales Values (Column E) to display only blank values'); // Execute filtering -$helper->log('Execute filtering'); +$helper->log('Execute filtering (apply the filter rules)'); $autoFilter->showHideRows(); // Set active sheet index to the first sheet, so Excel opens this as the first sheet