Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Gnumeric/ArrayFormulaTest.php
T
oleibman 3a690a755c SINGLE Function, and Gnumeric
SINGLE function can be used to return first value from a dynamic array result, or to return the value of the cell which matches the current row (VALUE error if not match) for a range. Excel allows you to specify an at-sign unary operator rather than SINGLE function; this PR does not permit that.

Add support for reading CSE array functions for Gnumeric.

Throw an exception if setValueExplicit Formula is invalid (not a string, or doesn't begin with equal sign. This is equivalent to what happens when setValueExplicit Numeric specifies a non-numeric value.

Added a number of tests from PR #2787.
2024-06-21 07:08:22 -07:00

92 lines
2.7 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Gnumeric;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
use PHPUnit\Framework\TestCase;
class ArrayFormulaTest extends TestCase
{
private string $arrayReturnType;
protected function setUp(): void
{
$this->arrayReturnType = Calculation::getArrayReturnType();
}
protected function tearDown(): void
{
Calculation::setArrayReturnType($this->arrayReturnType);
}
/**
* @dataProvider arrayFormulaReaderProvider
*/
public function testArrayFormulaReader(
string $cellAddress,
string $expectedRange,
string $expectedFormula,
array $expectedValue
): void {
$filename = 'tests/data/Reader/Gnumeric/ArrayFormulaTest.gnumeric';
$reader = new Gnumeric();
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($cellAddress);
self::assertSame(DataType::TYPE_FORMULA, $cell->getDataType());
self::assertSame(['t' => 'array', 'ref' => $expectedRange], $cell->getFormulaAttributes());
self::assertSame($expectedFormula, strtoupper($cell->getValue()));
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$worksheet->calculateArrays();
$cell = $worksheet->getCell($cellAddress);
self::assertSame($expectedValue, $cell->getCalculatedValue());
self::assertSame($expectedValue, $worksheet->rangeToArray($expectedRange, formatData: false, reduceArrays: true));
$spreadsheet->disconnectWorksheets();
}
public static function arrayFormulaReaderProvider(): array
{
return [
[
'D1',
'D1:E2',
'=A1:B1*A1:A2',
[[4, 6], [8, 12]],
],
[
'G1',
'G1:J1',
'=SIN({-1,0,1,2})',
[[-0.8414709848078965, 0.0, 0.8414709848078965, 0.9092974268256817]],
],
[
'D4',
'D4:E5',
'=A4:B4*A4:A5',
[[9, 12], [15, 20]],
],
[
'D7',
'D7:E8',
'=A7:B7*A7:A8',
[[16, 20], [24, 30]],
],
[
'D10',
'D10:E11',
'=A10:B10*A10:A11',
[[25, 30], [35, 42]],
],
[
'D13',
'D13:E14',
'=A13:B13*A13:A14',
[[36, 42], [48, 56]],
],
];
}
}