| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
|
|
8 |
|
|
|
9 |
class Formula
|
|
|
10 |
{
|
|
|
11 |
/**
|
|
|
12 |
* FORMULATEXT.
|
|
|
13 |
*
|
|
|
14 |
* @param mixed $cellReference The cell to check
|
|
|
15 |
* @param ?Cell $cell The current cell (containing this formula)
|
|
|
16 |
*/
|
|
|
17 |
public static function text(mixed $cellReference = '', ?Cell $cell = null): string
|
|
|
18 |
{
|
|
|
19 |
if ($cell === null) {
|
|
|
20 |
return ExcelError::REF();
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
$worksheet = null;
|
|
|
24 |
if (1 === preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches)) {
|
|
|
25 |
$cellReference = $matches[6] . $matches[7];
|
|
|
26 |
$worksheetName = trim($matches[3], "'");
|
|
|
27 |
$worksheet = (!empty($worksheetName))
|
|
|
28 |
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
|
|
|
29 |
: $cell->getWorksheet();
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
if (
|
|
|
33 |
$worksheet === null
|
|
|
34 |
|| !$worksheet->cellExists($cellReference)
|
|
|
35 |
|| !$worksheet->getCell($cellReference)->isFormula()
|
|
|
36 |
) {
|
|
|
37 |
return ExcelError::NA();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
return $worksheet->getCell($cellReference)->getValue();
|
|
|
41 |
}
|
|
|
42 |
}
|