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