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

54 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class HtmlArrayTest extends AbstractFunctional
{
public function testArray(): void
{
$spreadsheet = new Spreadsheet();
Calculation::getInstance($spreadsheet)->setInstanceArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('A2')->setValue(1);
$sheet->getCell('A3')->setValue(3);
$sheet->getCell('B1')->setValue('=UNIQUE(A1:A3)');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Html');
$sheet = $reloadedSpreadsheet->getActiveSheet();
self::assertEquals('1', $sheet->getCell('A1')->getValue());
self::assertEquals('1', $sheet->getCell('A2')->getValue());
self::assertEquals('3', $sheet->getCell('A3')->getValue());
self::assertEquals('1', $sheet->getCell('B1')->getValue());
self::assertEquals('3', $sheet->getCell('B2')->getValue());
self::assertNull($sheet->getCell('B3')->getValue());
$spreadsheet->disconnectWorksheets();
$reloadedSpreadsheet->disconnectWorksheets();
}
public function testInlineArrays(): void
{
$spreadsheet = new Spreadsheet();
Calculation::getInstance($spreadsheet)->setInstanceArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('=UNIQUE({1;1;2;1;3;2;4;4;4})');
$sheet->getCell('D1')->setValue('=UNIQUE({1,1,2,1,3,2,4,4,4},true)');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Csv');
$sheet = $reloadedSpreadsheet->getActiveSheet();
$expected = [
['1', null, null, '1', '2', '3', '4'],
['2', null, null, null, null, null, null],
['3', null, null, null, null, null, null],
['4', null, null, null, null, null, null],
];
self::assertSame($expected, $sheet->toArray());
$spreadsheet->disconnectWorksheets();
$reloadedSpreadsheet->disconnectWorksheets();
}
}