Skip to content

Commit 925c730

Browse files
author
MarkBaker
committed
Some more unit tests for array functions and spillage; and updates to documentation
1 parent 1ea119c commit 925c730

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

docs/topics/calculation-engine.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ $spreadsheet->getActiveSheet()->getCell('E11')->arrayFormulaRange();
4444
which returns a string containing a cell reference (e.g. `E11`) or a cell range reference (e.g. `E11:G13`).
4545

4646

47+
For more details on working with array formulae, see the [the recipes documentationn](./recipes.md/#array-formulae).
48+
4749
### Adjustments to formulae when Inserting/Deleting Columns/Rows
4850

4951
Another nice feature of PhpSpreadsheet's formula parser, is that it can

docs/topics/recipes.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,46 @@ $spreadsheet->getActiveSheet()
239239
);
240240
```
241241

242+
Simply setting an array formula in a cell and specifying the range won't populate the spillage area for that formula.
243+
```php
244+
$spreadsheet->getActiveSheet()
245+
->setCellValue(
246+
'A10',
247+
'=SEQUENCE(3,3)',
248+
true,
249+
'A1:C3'
250+
);
251+
252+
// Will return a null, because the formula for A1 hasn't been calculated to populate the spillage area
253+
$result = $spreadsheet->getActiveSheet()->getCell('C3')->getValue();
254+
```
255+
To do that, we need to retrieve the calculated value for the cell.
256+
```php
257+
$spreadsheet->getActiveSheet()
258+
->setCellValue(
259+
'A10',
260+
'=SEQUENCE(3,3)',
261+
true,
262+
'A1:C3'
263+
);
264+
265+
$spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
266+
267+
// Will return 9, because the formula for A1 has now been calculated, and the spillage area is populated
268+
$result = $spreadsheet->getActiveSheet()->getCell('C3')->getValue();
269+
```
270+
When we call `getCalculatedValue()` for a cell that contains an array formula, PhpSpreadsheet returns the single value that would appear in that cell in MS Excel.
271+
```php
272+
// Will return integer 1, the value for that cell within the array
273+
$a1result = $spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
274+
```
275+
276+
If we want to return the full array, then we need to call `getCalculatedValue()` with an additional argument, a boolean `true` to return the value as an array.
277+
```php
278+
// Will return an array [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
279+
$a1result = $spreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(true);
280+
```
281+
242282
---
243283

244284
Excel365 introduced a number of new functions that return arrays of results.

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SequenceTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ public function providerSEQUENCE(): array
2222
{
2323
return require 'tests/data/Calculation/MathTrig/SEQUENCE.php';
2424
}
25+
26+
public function testSequenceAsCellFormula()
27+
{
28+
$sheet = $this->getSheet();
29+
$sheet->getCell('B2')->setValue('=SEQUENCE(3,3,9,-1)', true, 'B2:D4');
30+
31+
$result = $sheet->getCell('B2')->getCalculatedValue();
32+
self::assertSame(9, $result);
33+
34+
// Check that spillage area has been populated
35+
self::assertSame(5, $sheet->getCell('C3')->getCalculatedValue());
36+
}
37+
38+
public function testSequenceAsCellFormulaAsArray()
39+
{
40+
$sheet = $this->getSheet();
41+
$sheet->getCell('B2')->setValue('=SEQUENCE(3,3,9,-1)', true, 'B2:D4');
42+
43+
$result = $sheet->getCell('B2')->getCalculatedValue(true);
44+
self::assertSame([[9, 8, 7], [6, 5, 4], [3, 2, 1]], $result);
45+
46+
// Check that spillage area has been populated
47+
self::assertSame(5, $sheet->getCell('C3')->getCalculatedValue());
48+
}
2549
}

0 commit comments

Comments
 (0)