Files
oleibman b00dd47c28 Instance Variable for Array Return Type
Till now we have used a static variable/getter/setter to decide what type of result should be returned when a formula is evaluated and an array is the result. This is messy; it would be much better to use an instance variable instead. We cannot eliminate `setArrayReturnType` and `getArrayReturnType` because that would be a BC break. I am considering whether they should be deprecated. In the meantime, I have added a new instance property `instanceArrayReturnType` with getter and setter methods. The property is initially null, and, if it remains so when needed, the static property will be used instead. However, if it is set, its value will be used.
2024-07-10 09:19:51 -07:00

66 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class ArrayFunctionsCellTest extends TestCase
{
public function testArrayAndNonArrayOutput(): void
{
$spreadsheet = new Spreadsheet();
Calculation::getInstance($spreadsheet)->setInstanceArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray(
[
[1.0, 0.0, 1.0],
[0.0, 2.0, 0.0],
[0.0, 0.0, 1.0],
],
strictNullComparison: true
);
$sheet->setCellValue('E1', '=MINVERSE(A1:C3)');
$sheet->setCellValue('I1', '=E1#');
$sheet->setCellValue('M1', '=MMULT(E1#,I1#)');
$sheet->setCellValue('E6', '=SUM(E1)');
$sheet->setCellValue('E7', '=SUM(SINGLE(E1))');
$sheet->setCellValue('E8', '=SUM(E1#)');
$sheet->setCellValue('I6', '=E1+I1');
$sheet->setCellValue('J6', '=E1#+I1#');
$expectedE1 = [
[1.0, 0.0, -1.0],
[0.0, 0.5, 0.0],
[0.0, 0.0, 1.0],
];
self::assertSame($expectedE1, $sheet->getCell('E1')->getCalculatedValue(), 'MINVERSE function');
self::assertSame($expectedE1, $sheet->getCell('I1')->getCalculatedValue(), 'Assignment with spill operator');
$expectedM1 = [
[1.0, 0.0, -2.0],
[0.0, 0.25, 0.0],
[0.0, 0.0, 1.0],
];
self::assertSame($expectedM1, $sheet->getCell('M1')->getCalculatedValue(), 'MMULT with 2 spill operators');
self::assertSame(1.0, $sheet->getCell('E6')->getCalculatedValue(), 'SUM referring to anchor cell');
self::assertSame(1.0, $sheet->getCell('E7')->getCalculatedValue(), 'SUM referring to anchor cell wrapped in Single');
self::assertSame(1.5, $sheet->getCell('E8')->getCalculatedValue(), 'SUM referring to anchor cell with Spill Operator');
self::assertSame(2.0, $sheet->getCell('I6')->getCalculatedValue(), 'addition operator for 2 anchor cells');
$expectedJ6 = [
[2.0, 0.0, -2.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 2.0],
];
self::assertSame($expectedJ6, $sheet->getCell('J6')->getCalculatedValue(), 'addition operator for 2 anchor cells with Spill operators');
$spreadsheet->disconnectWorksheets();
}
}