2
2
3
3
namespace PhpOffice \PhpSpreadsheetTests \Calculation \Functions \LookupRef ;
4
4
5
+ use PhpOffice \PhpSpreadsheet \Calculation \Calculation ;
5
6
use PhpOffice \PhpSpreadsheet \Calculation \Information \ExcelError ;
6
7
use PhpOffice \PhpSpreadsheet \Calculation \LookupRef \Filter ;
8
+ use PhpOffice \PhpSpreadsheet \Spreadsheet ;
7
9
use PHPUnit \Framework \TestCase ;
8
10
9
11
class FilterTest extends TestCase
@@ -15,7 +17,7 @@ public function testFilterByRow(): void
15
17
['East ' , 'Tom ' , 'Apple ' , 6830 ],
16
18
['East ' , 'Fritz ' , 'Apple ' , 4394 ],
17
19
['South ' , 'Sal ' , 'Apple ' , 1310 ],
18
- ['South ' , 'Hector ' , 'Apple ' , 98144 ],
20
+ ['South ' , 'Hector ' , 'Apple ' , 8144 ],
19
21
];
20
22
$ result = Filter::filter ($ this ->sampleDataForRow (), $ criteria );
21
23
self ::assertSame ($ expectedResult , $ result );
@@ -52,6 +54,50 @@ public function testFilterEmpty(): void
52
54
self ::assertSame ($ expectedResult , $ result );
53
55
}
54
56
57
+ public function testFilterWithAndLogic (): void
58
+ {
59
+ $ expectedResult = [
60
+ ['East ' , 'Tom ' , 'Apple ' , 6830 ],
61
+ ['East ' , 'Fritz ' , 'Banana ' , 6274 ],
62
+ ];
63
+
64
+ $ spreadsheet = new Spreadsheet ();
65
+ $ worksheet = $ spreadsheet ->getActiveSheet ();
66
+ $ worksheet ->fromArray ($ this ->sampleDataForRow (), null , 'C3 ' , true );
67
+
68
+ // East AND >6,000
69
+ $ formula = '=FILTER(C3:F18,(C3:C18="East")*(F3:F18>6000)) ' ;
70
+ $ worksheet ->setCellValue ('H1 ' , $ formula , true , 'H1:K2 ' );
71
+ $ result = $ worksheet ->getCell ('H1 ' )->getCalculatedValue (true );
72
+
73
+ self ::assertSame ($ expectedResult , $ result );
74
+ }
75
+
76
+ public function testFilterWithOrLogic (): void
77
+ {
78
+ $ expectedResult = [
79
+ ['East ' , 'Tom ' , 'Apple ' , 6830 ],
80
+ ['East ' , 'Fritz ' , 'Apple ' , 4394 ],
81
+ ['West ' , 'Sravan ' , 'Grape ' , 7195 ],
82
+ ['East ' , 'Tom ' , 'Banana ' , 4213 ],
83
+ ['North ' , 'Amy ' , 'Grape ' , 6420 ],
84
+ ['East ' , 'Fritz ' , 'Banana ' , 6274 ],
85
+ ['North ' , 'Xi ' , 'Grape ' , 7580 ],
86
+ ['South ' , 'Hector ' , 'Apple ' , 8144 ],
87
+ ];
88
+
89
+ $ spreadsheet = new Spreadsheet ();
90
+ $ worksheet = $ spreadsheet ->getActiveSheet ();
91
+ $ worksheet ->fromArray ($ this ->sampleDataForRow (), null , 'C3 ' , true );
92
+
93
+ // East OR >6,000
94
+ $ formula = '=FILTER(C3:F18,(C3:C18="East")+(F3:F18>6000)) ' ;
95
+ $ worksheet ->setCellValue ('H1 ' , $ formula , true , 'H1:K8 ' );
96
+ $ result = $ worksheet ->getCell ('H1 ' )->getCalculatedValue (true );
97
+
98
+ self ::assertSame ($ expectedResult , $ result );
99
+ }
100
+
55
101
protected function sampleDataForRow (): array
56
102
{
57
103
return [
@@ -70,7 +116,7 @@ protected function sampleDataForRow(): array
70
116
['East ' , 'Fritz ' , 'Banana ' , 6274 ],
71
117
['West ' , 'Sravan ' , 'Pear ' , 4894 ],
72
118
['North ' , 'Xi ' , 'Grape ' , 7580 ],
73
- ['South ' , 'Hector ' , 'Apple ' , 98144 ],
119
+ ['South ' , 'Hector ' , 'Apple ' , 8144 ],
74
120
];
75
121
}
76
122
0 commit comments