Skip to content

Commit 5ea78ae

Browse files
author
MarkBaker
committed
Modifications to FORMULATEXT() to return correct values for cells in array formula (wrapped in {} braces) and for cells within the spillage area of an array formula that should return the array formula even though they're not the actual formula cell
1 parent 5f79b74 commit 5ea78ae

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/PhpSpreadsheet/Calculation/LookupRef/Formula.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
77
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8+
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
810

911
class Formula
1012
{
@@ -30,14 +32,29 @@ public static function text($cellReference = '', ?Cell $cell = null)
3032
? $cell->getWorksheet()->getParent()->getSheetByName($worksheetName)
3133
: $cell->getWorksheet();
3234

33-
if (
34-
$worksheet === null ||
35-
!$worksheet->cellExists($cellReference) ||
36-
!$worksheet->getCell($cellReference)->isFormula()
37-
) {
35+
if ($worksheet === null || $worksheet->cellExists($cellReference) === false) {
36+
return ExcelError::NA();
37+
}
38+
39+
$arrayFormulaRange = $worksheet->getCell($cellReference)->arrayFormulaRange();
40+
if ($arrayFormulaRange !== null) {
41+
return self::arrayFormula($worksheet, $arrayFormulaRange);
42+
}
43+
44+
if ($worksheet->getCell($cellReference)->isFormula() === false) {
3845
return ExcelError::NA();
3946
}
4047

4148
return $worksheet->getCell($cellReference)->getValue();
4249
}
50+
51+
private static function arrayFormula(Worksheet $worksheet, string $arrayFormulaRange): string
52+
{
53+
[$arrayFormulaRange] = Coordinate::splitRange($arrayFormulaRange);
54+
[$arrayFormulaCell] = $arrayFormulaRange;
55+
56+
$arrayFormula = $worksheet->getCell($arrayFormulaCell)->getValue();
57+
58+
return "{{$arrayFormula}}";
59+
}
4360
}

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/FormulaTextTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,21 @@ public function testNoCell(): void
3434
{
3535
self::assertSame('#REF!', Formula::text('B5'));
3636
}
37+
38+
public function testArrayFormula(): void
39+
{
40+
$sheet = $this->getSheet();
41+
$sheet->setCellValue('B2', '=SEQUENCE(3,3)', true, 'B2:D4');
42+
43+
// Execute the calculation to populate the spillage cells
44+
$sheet->getCell('B2')->getCalculatedValue(true);
45+
$sheet->setCellValue('A1', '=FORMULATEXT(B2)');
46+
$sheet->setCellValue('E5', '=FORMULATEXT(D4)');
47+
48+
$result1 = $sheet->getCell('A1')->getCalculatedValue();
49+
self::assertSame('{=SEQUENCE(3,3)}', $result1, 'Formula Cell');
50+
51+
$result2 = $sheet->getCell('E5')->getCalculatedValue();
52+
self::assertSame('{=SEQUENCE(3,3)}', $result2, 'Spill Range Cell');
53+
}
3754
}

0 commit comments

Comments
 (0)