| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
|
7 |
|
|
|
8 |
class Functions
|
|
|
9 |
{
|
|
|
10 |
const PRECISION = 8.88E-016;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* 2 / PI.
|
|
|
14 |
*/
|
|
|
15 |
const M_2DIVPI = 0.63661977236758134307553505349006;
|
|
|
16 |
|
|
|
17 |
const COMPATIBILITY_EXCEL = 'Excel';
|
|
|
18 |
const COMPATIBILITY_GNUMERIC = 'Gnumeric';
|
|
|
19 |
const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
|
|
|
20 |
|
|
|
21 |
/** Use of RETURNDATE_PHP_NUMERIC is discouraged - not 32-bit Y2038-safe, no timezone. */
|
|
|
22 |
const RETURNDATE_PHP_NUMERIC = 'P';
|
|
|
23 |
/** Use of RETURNDATE_UNIX_TIMESTAMP is discouraged - not 32-bit Y2038-safe, no timezone. */
|
|
|
24 |
const RETURNDATE_UNIX_TIMESTAMP = 'P';
|
|
|
25 |
const RETURNDATE_PHP_OBJECT = 'O';
|
|
|
26 |
const RETURNDATE_PHP_DATETIME_OBJECT = 'O';
|
|
|
27 |
const RETURNDATE_EXCEL = 'E';
|
|
|
28 |
|
|
|
29 |
public const NOT_YET_IMPLEMENTED = '#Not Yet Implemented';
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Compatibility mode to use for error checking and responses.
|
|
|
33 |
*/
|
|
|
34 |
protected static string $compatibilityMode = self::COMPATIBILITY_EXCEL;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Data Type to use when returning date values.
|
|
|
38 |
*/
|
|
|
39 |
protected static string $returnDateType = self::RETURNDATE_EXCEL;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Set the Compatibility Mode.
|
|
|
43 |
*
|
|
|
44 |
* @param string $compatibilityMode Compatibility Mode
|
|
|
45 |
* Permitted values are:
|
|
|
46 |
* Functions::COMPATIBILITY_EXCEL 'Excel'
|
|
|
47 |
* Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
|
|
|
48 |
* Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
|
|
|
49 |
*
|
|
|
50 |
* @return bool (Success or Failure)
|
|
|
51 |
*/
|
|
|
52 |
public static function setCompatibilityMode(string $compatibilityMode): bool
|
|
|
53 |
{
|
|
|
54 |
if (
|
|
|
55 |
($compatibilityMode == self::COMPATIBILITY_EXCEL)
|
|
|
56 |
|| ($compatibilityMode == self::COMPATIBILITY_GNUMERIC)
|
|
|
57 |
|| ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)
|
|
|
58 |
) {
|
|
|
59 |
self::$compatibilityMode = $compatibilityMode;
|
|
|
60 |
|
|
|
61 |
return true;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return false;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Return the current Compatibility Mode.
|
|
|
69 |
*
|
|
|
70 |
* @return string Compatibility Mode
|
|
|
71 |
* Possible Return values are:
|
|
|
72 |
* Functions::COMPATIBILITY_EXCEL 'Excel'
|
|
|
73 |
* Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
|
|
|
74 |
* Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
|
|
|
75 |
*/
|
|
|
76 |
public static function getCompatibilityMode(): string
|
|
|
77 |
{
|
|
|
78 |
return self::$compatibilityMode;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP DateTime Object).
|
|
|
83 |
*
|
|
|
84 |
* @param string $returnDateType Return Date Format
|
|
|
85 |
* Permitted values are:
|
|
|
86 |
* Functions::RETURNDATE_UNIX_TIMESTAMP 'P'
|
|
|
87 |
* Functions::RETURNDATE_PHP_DATETIME_OBJECT 'O'
|
|
|
88 |
* Functions::RETURNDATE_EXCEL 'E'
|
|
|
89 |
*
|
|
|
90 |
* @return bool Success or failure
|
|
|
91 |
*/
|
|
|
92 |
public static function setReturnDateType(string $returnDateType): bool
|
|
|
93 |
{
|
|
|
94 |
if (
|
|
|
95 |
($returnDateType == self::RETURNDATE_UNIX_TIMESTAMP)
|
|
|
96 |
|| ($returnDateType == self::RETURNDATE_PHP_DATETIME_OBJECT)
|
|
|
97 |
|| ($returnDateType == self::RETURNDATE_EXCEL)
|
|
|
98 |
) {
|
|
|
99 |
self::$returnDateType = $returnDateType;
|
|
|
100 |
|
|
|
101 |
return true;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return false;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object).
|
|
|
109 |
*
|
|
|
110 |
* @return string Return Date Format
|
|
|
111 |
* Possible Return values are:
|
|
|
112 |
* Functions::RETURNDATE_UNIX_TIMESTAMP 'P'
|
|
|
113 |
* Functions::RETURNDATE_PHP_DATETIME_OBJECT 'O'
|
|
|
114 |
* Functions::RETURNDATE_EXCEL ' 'E'
|
|
|
115 |
*/
|
|
|
116 |
public static function getReturnDateType(): string
|
|
|
117 |
{
|
|
|
118 |
return self::$returnDateType;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* DUMMY.
|
|
|
123 |
*
|
|
|
124 |
* @return string #Not Yet Implemented
|
|
|
125 |
*/
|
|
|
126 |
public static function DUMMY(): string
|
|
|
127 |
{
|
|
|
128 |
return self::NOT_YET_IMPLEMENTED;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public static function isMatrixValue(mixed $idx): bool
|
|
|
132 |
{
|
|
|
133 |
return (substr_count($idx, '.') <= 1) || (preg_match('/\.[A-Z]/', $idx) > 0);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public static function isValue(mixed $idx): bool
|
|
|
137 |
{
|
|
|
138 |
return substr_count($idx, '.') === 0;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public static function isCellValue(mixed $idx): bool
|
|
|
142 |
{
|
|
|
143 |
return substr_count($idx, '.') > 1;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public static function ifCondition(mixed $condition): string
|
|
|
147 |
{
|
|
|
148 |
$condition = self::flattenSingleValue($condition);
|
|
|
149 |
|
|
|
150 |
if ($condition === '' || $condition === null) {
|
|
|
151 |
return '=""';
|
|
|
152 |
}
|
|
|
153 |
if (!is_string($condition) || !in_array($condition[0], ['>', '<', '='], true)) {
|
|
|
154 |
$condition = self::operandSpecialHandling($condition);
|
|
|
155 |
if (is_bool($condition)) {
|
|
|
156 |
return '=' . ($condition ? 'TRUE' : 'FALSE');
|
|
|
157 |
} elseif (!is_numeric($condition)) {
|
|
|
158 |
if ($condition !== '""') { // Not an empty string
|
|
|
159 |
// Escape any quotes in the string value
|
|
|
160 |
$condition = (string) preg_replace('/"/ui', '""', $condition);
|
|
|
161 |
}
|
|
|
162 |
$condition = Calculation::wrapResult(strtoupper($condition));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
return str_replace('""""', '""', '=' . $condition);
|
|
|
166 |
}
|
|
|
167 |
$operator = $operand = '';
|
|
|
168 |
if (1 === preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches)) {
|
|
|
169 |
[, $operator, $operand] = $matches;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
$operand = self::operandSpecialHandling($operand);
|
|
|
173 |
if (is_numeric(trim($operand, '"'))) {
|
|
|
174 |
$operand = trim($operand, '"');
|
|
|
175 |
} elseif (!is_numeric($operand) && $operand !== 'FALSE' && $operand !== 'TRUE') {
|
|
|
176 |
$operand = str_replace('"', '""', $operand);
|
|
|
177 |
$operand = Calculation::wrapResult(strtoupper($operand));
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return str_replace('""""', '""', $operator . $operand);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
private static function operandSpecialHandling(mixed $operand): mixed
|
|
|
184 |
{
|
|
|
185 |
if (is_numeric($operand) || is_bool($operand)) {
|
|
|
186 |
return $operand;
|
|
|
187 |
} elseif (strtoupper($operand) === Calculation::getTRUE() || strtoupper($operand) === Calculation::getFALSE()) {
|
|
|
188 |
return strtoupper($operand);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// Check for percentage
|
|
|
192 |
if (preg_match('/^\-?\d*\.?\d*\s?\%$/', $operand)) {
|
|
|
193 |
return ((float) rtrim($operand, '%')) / 100;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
// Check for dates
|
|
|
197 |
if (($dateValueOperand = Date::stringToExcel($operand)) !== false) {
|
|
|
198 |
return $dateValueOperand;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
return $operand;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Convert a multi-dimensional array to a simple 1-dimensional array.
|
|
|
206 |
*
|
|
|
207 |
* @param mixed $array Array to be flattened
|
|
|
208 |
*
|
|
|
209 |
* @return array Flattened array
|
|
|
210 |
*/
|
|
|
211 |
public static function flattenArray(mixed $array): array
|
|
|
212 |
{
|
|
|
213 |
if (!is_array($array)) {
|
|
|
214 |
return (array) $array;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
$flattened = [];
|
|
|
218 |
$stack = array_values($array);
|
|
|
219 |
|
|
|
220 |
while (!empty($stack)) {
|
|
|
221 |
$value = array_shift($stack);
|
|
|
222 |
|
|
|
223 |
if (is_array($value)) {
|
|
|
224 |
array_unshift($stack, ...array_values($value));
|
|
|
225 |
} else {
|
|
|
226 |
$flattened[] = $value;
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
return $flattened;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Convert a multi-dimensional array to a simple 1-dimensional array.
|
|
|
235 |
* Same as above but argument is specified in ... format.
|
|
|
236 |
*
|
|
|
237 |
* @param mixed $array Array to be flattened
|
|
|
238 |
*
|
|
|
239 |
* @return array Flattened array
|
|
|
240 |
*/
|
|
|
241 |
public static function flattenArray2(mixed ...$array): array
|
|
|
242 |
{
|
|
|
243 |
$flattened = [];
|
|
|
244 |
$stack = array_values($array);
|
|
|
245 |
|
|
|
246 |
while (!empty($stack)) {
|
|
|
247 |
$value = array_shift($stack);
|
|
|
248 |
|
|
|
249 |
if (is_array($value)) {
|
|
|
250 |
array_unshift($stack, ...array_values($value));
|
|
|
251 |
} else {
|
|
|
252 |
$flattened[] = $value;
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
return $flattened;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public static function scalar(mixed $value): mixed
|
|
|
260 |
{
|
|
|
261 |
if (!is_array($value)) {
|
|
|
262 |
return $value;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
do {
|
|
|
266 |
$value = array_pop($value);
|
|
|
267 |
} while (is_array($value));
|
|
|
268 |
|
|
|
269 |
return $value;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing.
|
|
|
274 |
*
|
|
|
275 |
* @param array|mixed $array Array to be flattened
|
|
|
276 |
*
|
|
|
277 |
* @return array Flattened array
|
|
|
278 |
*/
|
|
|
279 |
public static function flattenArrayIndexed($array): array
|
|
|
280 |
{
|
|
|
281 |
if (!is_array($array)) {
|
|
|
282 |
return (array) $array;
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
$arrayValues = [];
|
|
|
286 |
foreach ($array as $k1 => $value) {
|
|
|
287 |
if (is_array($value)) {
|
|
|
288 |
foreach ($value as $k2 => $val) {
|
|
|
289 |
if (is_array($val)) {
|
|
|
290 |
foreach ($val as $k3 => $v) {
|
|
|
291 |
$arrayValues[$k1 . '.' . $k2 . '.' . $k3] = $v;
|
|
|
292 |
}
|
|
|
293 |
} else {
|
|
|
294 |
$arrayValues[$k1 . '.' . $k2] = $val;
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
} else {
|
|
|
298 |
$arrayValues[$k1] = $value;
|
|
|
299 |
}
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
return $arrayValues;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Convert an array to a single scalar value by extracting the first element.
|
|
|
307 |
*
|
|
|
308 |
* @param mixed $value Array or scalar value
|
|
|
309 |
*/
|
|
|
310 |
public static function flattenSingleValue(mixed $value): mixed
|
|
|
311 |
{
|
|
|
312 |
while (is_array($value)) {
|
|
|
313 |
$value = array_shift($value);
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
return $value;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
public static function expandDefinedName(string $coordinate, Cell $cell): string
|
|
|
320 |
{
|
|
|
321 |
$worksheet = $cell->getWorksheet();
|
|
|
322 |
$spreadsheet = $worksheet->getParentOrThrow();
|
|
|
323 |
// Uppercase coordinate
|
|
|
324 |
$pCoordinatex = strtoupper($coordinate);
|
|
|
325 |
// Eliminate leading equal sign
|
|
|
326 |
$pCoordinatex = (string) preg_replace('/^=/', '', $pCoordinatex);
|
|
|
327 |
$defined = $spreadsheet->getDefinedName($pCoordinatex, $worksheet);
|
|
|
328 |
if ($defined !== null) {
|
|
|
329 |
$worksheet2 = $defined->getWorkSheet();
|
|
|
330 |
if (!$defined->isFormula() && $worksheet2 !== null) {
|
|
|
331 |
$coordinate = "'" . $worksheet2->getTitle() . "'!"
|
|
|
332 |
. (string) preg_replace('/^=/', '', str_replace('$', '', $defined->getValue()));
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
return $coordinate;
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
public static function trimTrailingRange(string $coordinate): string
|
|
|
340 |
{
|
|
|
341 |
return (string) preg_replace('/:[\w\$]+$/', '', $coordinate);
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
public static function trimSheetFromCellReference(string $coordinate): string
|
|
|
345 |
{
|
|
|
346 |
if (str_contains($coordinate, '!')) {
|
|
|
347 |
$coordinate = substr($coordinate, strrpos($coordinate, '!') + 1);
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
return $coordinate;
|
|
|
351 |
}
|
|
|
352 |
}
|