3
3
namespace PhpOffice \PhpSpreadsheetTests \Calculation ;
4
4
5
5
use PhpOffice \PhpSpreadsheet \Calculation \Calculation ;
6
+ use PhpOffice \PhpSpreadsheet \NamedRange ;
7
+ use PhpOffice \PhpSpreadsheet \Spreadsheet ;
6
8
use PHPUnit \Framework \TestCase ;
7
9
8
10
class ParseFormulaTest extends TestCase
@@ -12,7 +14,11 @@ class ParseFormulaTest extends TestCase
12
14
*/
13
15
public function testParseOperations (array $ expectedStack , string $ formula ): void
14
16
{
15
- $ parser = Calculation::getInstance ();
17
+ $ spreadsheet = new Spreadsheet ();
18
+ $ spreadsheet ->addNamedRange (new NamedRange ('GROUP1 ' , $ spreadsheet ->getActiveSheet (), 'B2:D4 ' ));
19
+ $ spreadsheet ->addNamedRange (new NamedRange ('GROUP2 ' , $ spreadsheet ->getActiveSheet (), 'D4:F6 ' ));
20
+
21
+ $ parser = Calculation::getInstance ($ spreadsheet );
16
22
$ stack = $ parser ->parseFormula ($ formula );
17
23
self ::assertSame ($ expectedStack , $ stack );
18
24
}
@@ -80,6 +86,36 @@ public function providerBinaryOperations(): array
80
86
],
81
87
'=-DEFINED_NAME% ' ,
82
88
],
89
+ 'Integer Numbers with Operator ' => [
90
+ [
91
+ ['type ' => 'Value ' , 'value ' => 2 , 'reference ' => null ],
92
+ ['type ' => 'Value ' , 'value ' => 3 , 'reference ' => null ],
93
+ ['type ' => 'Binary Operator ' , 'value ' => '* ' , 'reference ' => null ],
94
+ ],
95
+ '=2*3 ' ,
96
+ ],
97
+ 'Float Numbers with Operator ' => [
98
+ [
99
+ ['type ' => 'Value ' , 'value ' => 2.5 , 'reference ' => null ],
100
+ ['type ' => 'Value ' , 'value ' => 3.5 , 'reference ' => null ],
101
+ ['type ' => 'Binary Operator ' , 'value ' => '* ' , 'reference ' => null ],
102
+ ],
103
+ '=2.5*3.5 ' ,
104
+ ],
105
+ 'Strings with Operator ' => [
106
+ [
107
+ ['type ' => 'Value ' , 'value ' => '"HELLO" ' , 'reference ' => null ],
108
+ ['type ' => 'Value ' , 'value ' => '"WORLD" ' , 'reference ' => null ],
109
+ ['type ' => 'Binary Operator ' , 'value ' => '& ' , 'reference ' => null ],
110
+ ],
111
+ '="HELLO"&"WORLD" ' ,
112
+ ],
113
+ 'Error ' => [
114
+ [
115
+ ['type ' => 'Value ' , 'value ' => '#DIV0! ' , 'reference ' => null ],
116
+ ],
117
+ '=#DIV0! ' ,
118
+ ],
83
119
'Cell Range ' => [
84
120
[
85
121
['type ' => 'Cell Reference ' , 'value ' => 'A1 ' , 'reference ' => 'A1 ' ],
@@ -88,6 +124,16 @@ public function providerBinaryOperations(): array
88
124
],
89
125
'=A1:C3 ' ,
90
126
],
127
+ 'Chained Cell Range ' => [
128
+ [
129
+ ['type ' => 'Cell Reference ' , 'value ' => 'A1 ' , 'reference ' => 'A1 ' ],
130
+ ['type ' => 'Cell Reference ' , 'value ' => 'C3 ' , 'reference ' => 'C3 ' ],
131
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
132
+ ['type ' => 'Cell Reference ' , 'value ' => 'E5 ' , 'reference ' => 'E5 ' ],
133
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
134
+ ],
135
+ '=A1:C3:E5 ' ,
136
+ ],
91
137
'Cell Range Intersection ' => [
92
138
[
93
139
['type ' => 'Cell Reference ' , 'value ' => 'A1 ' , 'reference ' => 'A1 ' ],
@@ -100,6 +146,40 @@ public function providerBinaryOperations(): array
100
146
],
101
147
'=A1:C3 B2:D4 ' ,
102
148
],
149
+ 'Row Range ' => [
150
+ [
151
+ ['type ' => 'Row Reference ' , 'value ' => 'A2 ' , 'reference ' => 'A2 ' ],
152
+ ['type ' => 'Row Reference ' , 'value ' => 'XFD3 ' , 'reference ' => 'XFD3 ' ],
153
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
154
+ ],
155
+ '=2:3 ' ,
156
+ ],
157
+ 'Column Range ' => [
158
+ [
159
+ ['type ' => 'Column Reference ' , 'value ' => 'B1 ' , 'reference ' => 'B1 ' ],
160
+ ['type ' => 'Column Reference ' , 'value ' => 'C1048576 ' , 'reference ' => 'C1048576 ' ],
161
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
162
+ ],
163
+ '=B:C ' ,
164
+ ],
165
+ 'Range with Defined Names ' => [
166
+ [
167
+ ['type ' => 'Defined Name ' , 'value ' => 'GROUP1 ' , 'reference ' => 'GROUP1 ' ],
168
+ ['type ' => 'Defined Name ' , 'value ' => 'D4 ' , 'reference ' => 'GROUP2 ' ],
169
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
170
+ ['type ' => 'Defined Name ' , 'value ' => 'F6 ' , 'reference ' => 'GROUP2 ' ],
171
+ ['type ' => 'Binary Operator ' , 'value ' => ': ' , 'reference ' => null ],
172
+ ],
173
+ '=GROUP1:GROUP2 ' ,
174
+ ],
175
+ 'Named Range with Binary Operator ' => [
176
+ [
177
+ ['type ' => 'Defined Name ' , 'value ' => 'DEFINED_NAME_1 ' , 'reference ' => 'DEFINED_NAME_1 ' ],
178
+ ['type ' => 'Defined Name ' , 'value ' => 'DEFINED_NAME_2 ' , 'reference ' => 'DEFINED_NAME_2 ' ],
179
+ ['type ' => 'Binary Operator ' , 'value ' => '/ ' , 'reference ' => null ],
180
+ ],
181
+ '=DEFINED_NAME_1/DEFINED_NAME_2 ' ,
182
+ ],
103
183
'Named Range Intersection ' => [
104
184
[
105
185
['type ' => 'Defined Name ' , 'value ' => 'DEFINED_NAME_1 ' , 'reference ' => 'DEFINED_NAME_1 ' ],
@@ -108,6 +188,22 @@ public function providerBinaryOperations(): array
108
188
],
109
189
'=DEFINED_NAME_1 DEFINED_NAME_2 ' ,
110
190
],
191
+ // 'Structured Reference Arithmetic' => [
192
+ // [
193
+ // ['type' => 'Structured Reference', 'value' => '[@Quantity]', 'reference' => null],
194
+ // ['type' => 'Structured Reference', 'value' => '[@[Unit Price]]', 'reference' => null],
195
+ // ['type' => 'Binary Operator', 'value' => '*', 'reference' => null],
196
+ // ],
197
+ // '=[@Quantity]*[@[Unit Price]]',
198
+ // ],
199
+ // 'Structured Reference Intersection' => [
200
+ // [
201
+ // ['type' => 'Structured Reference', 'value' => 'DeptSales[[Sales Person]:[Sales Amount]]', 'reference' => null],
202
+ // ['type' => 'Structured Reference', 'value' => 'DeptSales[[Region]:[% Commission]]', 'reference' => null],
203
+ // ['type' => 'Binary Operator', 'value' => '∩', 'reference' => null],
204
+ // ],
205
+ // '=DeptSales[[Sales Person]:[Sales Amount]] DeptSales[[Region]:[% Commission]]',
206
+ // ],
111
207
// 'Cell Range Union' => [
112
208
// [
113
209
// ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'],
0 commit comments