Skip to content

Commit 55b9520

Browse files
authored
Merge pull request #2141 from oleibman/moredatefilter
Autofilter Part 1
2 parents 66cd68d + 9b6e4f9 commit 55b9520

File tree

11 files changed

+799
-188
lines changed

11 files changed

+799
-188
lines changed

docs/topics/autofilters.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,14 @@ $columnFilter->setFilterType(
327327
```
328328

329329
When defining the rule for a dynamic filter, we don't define a value (we
330-
can simply set that to NULL) but we do specify the dynamic filter
330+
can simply set that to null string) but we do specify the dynamic filter
331331
category.
332332

333333
```php
334334
$columnFilter->createRule()
335335
->setRule(
336336
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
337-
NULL,
337+
'',
338338
\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE
339339
)
340340
->setRuleType(

phpstan-baseline.neon

+1-41
Original file line numberDiff line numberDiff line change
@@ -5392,7 +5392,7 @@ parameters:
53925392

53935393
-
53945394
message: "#^Parameter \\#1 \\$excelTimestamp of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Date\\:\\:excelToTimestamp\\(\\) expects float\\|int, float\\|int\\|string given\\.$#"
5395-
count: 2
5395+
count: 1
53965396
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
53975397

53985398
-
@@ -5405,36 +5405,6 @@ parameters:
54055405
count: 1
54065406
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
54075407

5408-
-
5409-
message: "#^Parameter \\#2 \\$now of function strtotime expects int, int\\|false given\\.$#"
5410-
count: 6
5411-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5412-
5413-
-
5414-
message: "#^Parameter \\#5 \\$day of function gmmktime expects int, string given\\.$#"
5415-
count: 8
5416-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5417-
5418-
-
5419-
message: "#^Parameter \\#6 \\$year of function gmmktime expects int, string given\\.$#"
5420-
count: 13
5421-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5422-
5423-
-
5424-
message: "#^Parameter \\#4 \\$mon of function gmmktime expects int, string given\\.$#"
5425-
count: 2
5426-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5427-
5428-
-
5429-
message: "#^Parameter \\#5 \\$day of function gmmktime expects int, float given\\.$#"
5430-
count: 2
5431-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5432-
5433-
-
5434-
message: "#^Parameter \\#1 \\$attributes of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\\\Column\\:\\:setAttributes\\(\\) expects array\\<string\\>, array\\<string, float\\|int\\|null\\> given\\.$#"
5435-
count: 1
5436-
path: src/PhpSpreadsheet/Worksheet/AutoFilter.php
5437-
54385408
-
54395409
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\:\\:calculateTopTenValue\\(\\) has no return typehint specified\\.$#"
54405410
count: 1
@@ -7415,21 +7385,11 @@ parameters:
74157385
count: 1
74167386
path: tests/PhpSpreadsheetTests/Worksheet/AutoFilter/Column/RuleTest.php
74177387

7418-
-
7419-
message: "#^Parameter \\#1 \\$attributes of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\\\Column\\:\\:setAttributes\\(\\) expects array\\<string\\>, array\\<string, int\\> given\\.$#"
7420-
count: 3
7421-
path: tests/PhpSpreadsheetTests/Worksheet/AutoFilter/ColumnTest.php
7422-
74237388
-
74247389
message: "#^Parameter \\#2 \\$pValue of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\\\Column\\:\\:setAttribute\\(\\) expects string, int given\\.$#"
74257390
count: 1
74267391
path: tests/PhpSpreadsheetTests/Worksheet/AutoFilter/ColumnTest.php
74277392

7428-
-
7429-
message: "#^Parameter \\#1 \\$pColumn of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\:\\:setColumn\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\AutoFilter\\\\Column\\|string, float given\\.$#"
7430-
count: 1
7431-
path: tests/PhpSpreadsheetTests/Worksheet/AutoFilterTest.php
7432-
74337393
-
74347394
message: "#^Parameter \\#1 \\$im of function imagecolorallocate expects resource, resource\\|false given\\.$#"
74357395
count: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column;
5+
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule;
6+
7+
require __DIR__ . '/../Header.php';
8+
9+
// Sample can be slightly off if processing begins just before midnight
10+
// and does not complete till after midnight.
11+
// This possibility is accounted for in unit tests,
12+
// but seems unneccesarily complicated for the sample.
13+
14+
function createSheet(Spreadsheet $spreadsheet, string $rule): void
15+
{
16+
$sheet = $spreadsheet->createSheet();
17+
$sheet->setTitle($rule);
18+
$sheet->getCell('A1')->setValue('Date');
19+
$row = 1;
20+
$date = new DateTime();
21+
$year = (int) $date->format('Y');
22+
$month = (int) $date->format('m');
23+
$day = (int) $date->format('d');
24+
$yearMinus2 = $year - 2;
25+
$sheet->getCell('B1')->setValue("=DATE($year, $month, $day)");
26+
// Each day for two weeks before today through 2 weeks after
27+
for ($dayOffset = -14; $dayOffset < 14; ++$dayOffset) {
28+
++$row;
29+
$sheet->getCell("A$row")->setValue("=B1+($dayOffset)");
30+
}
31+
// First and last day of each month, starting with January 2 years before,
32+
// through December 2 years after.
33+
for ($monthOffset = 0; $monthOffset < 48; ++$monthOffset) {
34+
++$row;
35+
$sheet->getCell("A$row")->setValue("=DATE($yearMinus2, $monthOffset, 1)");
36+
++$row;
37+
$sheet->getCell("A$row")->setValue("=DATE($yearMinus2, $monthOffset + 1, 0)");
38+
}
39+
$sheet->getStyle("A2:A$row")->getNumberFormat()->setFormatCode('yyyy-mm-dd');
40+
$sheet->getStyle('B1')->getNumberFormat()->setFormatCode('yyyy-mm-dd');
41+
$sheet->getColumnDimension('A')->setAutoSize(true);
42+
$sheet->getColumnDimension('B')->setAutoSize(true);
43+
$autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
44+
$autoFilter->setRange("A1:A$row");
45+
$columnFilter = $autoFilter->getColumn('A');
46+
$columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER);
47+
$columnFilter->createRule()
48+
->setRule(
49+
Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
50+
'',
51+
$rule
52+
)
53+
->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER);
54+
$sheet->setSelectedCell('B1');
55+
}
56+
57+
// Create new Spreadsheet object
58+
$helper->log('Create new Spreadsheet object');
59+
$spreadsheet = new Spreadsheet();
60+
61+
// Set document properties
62+
$helper->log('Set document properties');
63+
$spreadsheet->getProperties()->setCreator('Owen Leibman')
64+
->setLastModifiedBy('Owen Leibman')
65+
->setTitle('PhpSpreadsheet Test Document')
66+
->setSubject('PhpSpreadsheet Test Document')
67+
->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
68+
->setKeywords('office PhpSpreadsheet php')
69+
->setCategory('Test result file');
70+
71+
$ruleNames = [
72+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTMONTH,
73+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTQUARTER,
74+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK,
75+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTYEAR,
76+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTMONTH,
77+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTQUARTER,
78+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTWEEK,
79+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_NEXTYEAR,
80+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISMONTH,
81+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISQUARTER,
82+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISWEEK,
83+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_THISYEAR,
84+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_TODAY,
85+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_TOMORROW,
86+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE,
87+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_YESTERDAY,
88+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_2,
89+
Rule::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_3,
90+
];
91+
92+
// Create the worksheets
93+
foreach ($ruleNames as $ruleName) {
94+
$helper->log("Add data and filter for $ruleName");
95+
createSheet($spreadsheet, $ruleName);
96+
}
97+
$spreadsheet->removeSheetByIndex(0);
98+
$spreadsheet->setActiveSheetIndex(0);
99+
// Save
100+
$helper->write($spreadsheet, __FILE__);

0 commit comments

Comments
 (0)