1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\Information; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\NamedRange; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
13
|
|
|
|
14
|
|
|
class Value |
15
|
|
|
{ |
16
|
|
|
use ArrayEnabled; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* IS_BLANK. |
20
|
|
|
* |
21
|
|
|
* @param mixed $value Value to check |
22
|
|
|
* Or can be an array of values |
23
|
|
|
* |
24
|
|
|
* @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
25
|
|
|
* with the same dimensions |
26
|
|
|
*/ |
27
|
19 |
|
public static function isBlank(mixed $value = null): array|bool |
28
|
|
|
{ |
29
|
19 |
|
if (is_array($value)) { |
30
|
2 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
31
|
|
|
} |
32
|
|
|
|
33
|
19 |
|
return $value === null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* IS_REF. |
38
|
|
|
* |
39
|
|
|
* @param mixed $value Value to check |
40
|
|
|
*/ |
41
|
14 |
|
public static function isRef(mixed $value, ?Cell $cell = null): bool |
42
|
|
|
{ |
43
|
14 |
|
if ($cell === null) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
14 |
|
$value = StringHelper::convertToString($value); |
48
|
14 |
|
$cellValue = Functions::trimTrailingRange($value); |
49
|
14 |
|
if (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/ui', $cellValue) === 1) { |
50
|
10 |
|
[$worksheet, $cellValue] = Worksheet::extractSheetTitle($cellValue, true, true); |
51
|
10 |
|
if (!empty($worksheet) && $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheet) === null) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
10 |
|
[$column, $row] = Coordinate::indexesFromString($cellValue ?? ''); |
55
|
10 |
|
if ($column > 16384 || $row > 1048576) { |
56
|
1 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
9 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$namedRange = $cell->getWorksheet()->getParentOrThrow()->getNamedRange($value); |
63
|
|
|
|
64
|
4 |
|
return $namedRange instanceof NamedRange; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* IS_EVEN. |
69
|
|
|
* |
70
|
|
|
* @param mixed $value Value to check |
71
|
|
|
* Or can be an array of values |
72
|
|
|
* |
73
|
|
|
* @return array|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array |
74
|
|
|
* with the same dimensions |
75
|
|
|
*/ |
76
|
29 |
|
public static function isEven(mixed $value = null): array|string|bool |
77
|
|
|
{ |
78
|
29 |
|
if (is_array($value)) { |
79
|
1 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
29 |
|
if ($value === null) { |
83
|
2 |
|
return ExcelError::NAME(); |
84
|
|
|
} |
85
|
27 |
|
if (!is_numeric($value)) { |
86
|
7 |
|
return ExcelError::VALUE(); |
87
|
|
|
} |
88
|
|
|
|
89
|
20 |
|
return ((int) fmod($value + 0, 2)) === 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* IS_ODD. |
94
|
|
|
* |
95
|
|
|
* @param mixed $value Value to check |
96
|
|
|
* Or can be an array of values |
97
|
|
|
* |
98
|
|
|
* @return array|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array |
99
|
|
|
* with the same dimensions |
100
|
|
|
*/ |
101
|
29 |
|
public static function isOdd(mixed $value = null): array|string|bool |
102
|
|
|
{ |
103
|
29 |
|
if (is_array($value)) { |
104
|
1 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
105
|
|
|
} |
106
|
|
|
|
107
|
29 |
|
if ($value === null) { |
108
|
2 |
|
return ExcelError::NAME(); |
109
|
|
|
} |
110
|
27 |
|
if (!is_numeric($value)) { |
111
|
7 |
|
return ExcelError::VALUE(); |
112
|
|
|
} |
113
|
|
|
|
114
|
20 |
|
return ((int) fmod($value + 0, 2)) !== 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* IS_NUMBER. |
119
|
|
|
* |
120
|
|
|
* @param mixed $value Value to check |
121
|
|
|
* Or can be an array of values |
122
|
|
|
* |
123
|
|
|
* @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
124
|
|
|
* with the same dimensions |
125
|
|
|
*/ |
126
|
19 |
|
public static function isNumber(mixed $value = null): array|bool |
127
|
|
|
{ |
128
|
19 |
|
if (is_array($value)) { |
129
|
2 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
19 |
|
if (is_string($value)) { |
133
|
10 |
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
11 |
|
return is_numeric($value); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* IS_LOGICAL. |
141
|
|
|
* |
142
|
|
|
* @param mixed $value Value to check |
143
|
|
|
* Or can be an array of values |
144
|
|
|
* |
145
|
|
|
* @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
146
|
|
|
* with the same dimensions |
147
|
|
|
*/ |
148
|
18 |
|
public static function isLogical(mixed $value = null): array|bool |
149
|
|
|
{ |
150
|
18 |
|
if (is_array($value)) { |
151
|
1 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
18 |
|
return is_bool($value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* IS_TEXT. |
159
|
|
|
* |
160
|
|
|
* @param mixed $value Value to check |
161
|
|
|
* Or can be an array of values |
162
|
|
|
* |
163
|
|
|
* @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
164
|
|
|
* with the same dimensions |
165
|
|
|
*/ |
166
|
35 |
|
public static function isText(mixed $value = null): array|bool |
167
|
|
|
{ |
168
|
35 |
|
if (is_array($value)) { |
169
|
1 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
170
|
|
|
} |
171
|
|
|
|
172
|
35 |
|
return is_string($value) && !ErrorValue::isError($value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* IS_NONTEXT. |
177
|
|
|
* |
178
|
|
|
* @param mixed $value Value to check |
179
|
|
|
* Or can be an array of values |
180
|
|
|
* |
181
|
|
|
* @return array|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
182
|
|
|
* with the same dimensions |
183
|
|
|
*/ |
184
|
18 |
|
public static function isNonText(mixed $value = null): array|bool |
185
|
|
|
{ |
186
|
18 |
|
if (is_array($value)) { |
187
|
1 |
|
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
18 |
|
return !self::isText($value); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* ISFORMULA. |
195
|
|
|
* |
196
|
|
|
* @param mixed $cellReference The cell to check |
197
|
|
|
* @param ?Cell $cell The current cell (containing this formula) |
198
|
|
|
*/ |
199
|
5 |
|
public static function isFormula(mixed $cellReference = '', ?Cell $cell = null): array|bool|string |
200
|
|
|
{ |
201
|
5 |
|
if ($cell === null) { |
202
|
1 |
|
return ExcelError::REF(); |
203
|
|
|
} |
204
|
4 |
|
$cellReference = StringHelper::convertToString($cellReference); |
205
|
|
|
|
206
|
4 |
|
$fullCellReference = Functions::expandDefinedName($cellReference, $cell); |
207
|
|
|
|
208
|
4 |
|
if (str_contains($cellReference, '!')) { |
209
|
4 |
|
$cellReference = Functions::trimSheetFromCellReference($cellReference); |
210
|
4 |
|
$cellReferences = Coordinate::extractAllCellReferencesInRange($cellReference); |
211
|
4 |
|
if (count($cellReferences) > 1) { |
212
|
2 |
|
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $cellReferences, $cell); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
4 |
|
$fullCellReference = Functions::trimTrailingRange($fullCellReference); |
217
|
|
|
|
218
|
4 |
|
$worksheetName = ''; |
219
|
4 |
|
if (1 == preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches)) { |
220
|
4 |
|
$fullCellReference = $matches[6] . $matches[7]; |
221
|
4 |
|
$worksheetName = str_replace("''", "'", trim($matches[2], "'")); |
222
|
|
|
} |
223
|
|
|
|
224
|
4 |
|
$worksheet = (!empty($worksheetName)) |
225
|
3 |
|
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName) |
226
|
2 |
|
: $cell->getWorksheet(); |
227
|
|
|
|
228
|
4 |
|
return ($worksheet !== null) ? $worksheet->getCell($fullCellReference)->isFormula() : ExcelError::REF(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* N. |
233
|
|
|
* |
234
|
|
|
* Returns a value converted to a number |
235
|
|
|
* |
236
|
|
|
* @param null|mixed $value The value you want converted |
237
|
|
|
* |
238
|
|
|
* @return number|string N converts values listed in the following table |
239
|
|
|
* If value is or refers to N returns |
240
|
|
|
* A number That number value |
241
|
|
|
* A date The Excel serialized number of that date |
242
|
|
|
* TRUE 1 |
243
|
|
|
* FALSE 0 |
244
|
|
|
* An error value The error value |
245
|
|
|
* Anything else 0 |
246
|
|
|
*/ |
247
|
21 |
|
public static function asNumber($value = null) |
248
|
|
|
{ |
249
|
21 |
|
while (is_array($value)) { |
250
|
9 |
|
$value = array_shift($value); |
251
|
|
|
} |
252
|
21 |
|
if (is_float($value) || is_int($value)) { |
253
|
9 |
|
return $value; |
254
|
|
|
} |
255
|
12 |
|
if (is_bool($value)) { |
256
|
1 |
|
return (int) $value; |
257
|
|
|
} |
258
|
11 |
|
if (is_string($value) && substr($value, 0, 1) === '#') { |
259
|
2 |
|
return $value; |
260
|
|
|
} |
261
|
|
|
|
262
|
9 |
|
return 0; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* TYPE. |
267
|
|
|
* |
268
|
|
|
* Returns a number that identifies the type of a value |
269
|
|
|
* |
270
|
|
|
* @param null|mixed $value The value you want tested |
271
|
|
|
* |
272
|
|
|
* @return int N converts values listed in the following table |
273
|
|
|
* If value is or refers to N returns |
274
|
|
|
* A number 1 |
275
|
|
|
* Text 2 |
276
|
|
|
* Logical Value 4 |
277
|
|
|
* An error value 16 |
278
|
|
|
* Array or Matrix 64 |
279
|
|
|
*/ |
280
|
17 |
|
public static function type($value = null): int |
281
|
|
|
{ |
282
|
17 |
|
$value = Functions::flattenArrayIndexed($value); |
283
|
17 |
|
if (count($value) > 1) { |
284
|
3 |
|
end($value); |
285
|
3 |
|
$a = key($value); |
286
|
|
|
// Range of cells is an error |
287
|
3 |
|
if (Functions::isCellValue($a)) { |
288
|
|
|
return 16; |
289
|
|
|
// Test for Matrix |
290
|
3 |
|
} elseif (Functions::isMatrixValue($a)) { |
291
|
3 |
|
return 64; |
292
|
|
|
} |
293
|
14 |
|
} elseif (empty($value)) { |
294
|
|
|
// Empty Cell |
295
|
2 |
|
return 1; |
296
|
|
|
} |
297
|
|
|
|
298
|
12 |
|
$value = Functions::flattenSingleValue($value); |
299
|
12 |
|
if (($value === null) || (is_float($value)) || (is_int($value))) { |
300
|
4 |
|
return 1; |
301
|
8 |
|
} elseif (is_bool($value)) { |
302
|
1 |
|
return 4; |
303
|
7 |
|
} elseif (is_array($value)) { |
304
|
|
|
return 64; |
305
|
7 |
|
} elseif (is_string($value)) { |
306
|
|
|
// Errors |
307
|
7 |
|
if (($value !== '') && ($value[0] == '#')) { |
308
|
2 |
|
return 16; |
309
|
|
|
} |
310
|
|
|
|
311
|
5 |
|
return 2; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return 0; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|