mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 22:16:46 +00:00
be25c44313
This has come up a number of times, most recently with issue #3901, and also issue #3659. It will certainly come up more often in days to come. Excel is changing formulas which PhpSpreadsheet has output as `=UNIQUE(A1:A19)`; Excel is processing the formula as it were `=@UNIQUE(A1:A19)`. This behavior is explained, in part, by https://github.com/PHPOffice/PhpSpreadsheet/pull/3659#issuecomment-1663040464. It is doing so in order to ensure that the function returns only a single value rather than an array of values, in case the spreadsheet is being processed (or possibly was created) by a less current version of Excel which cannot handle the array result. PhpSpreadsheet follows Excel to a certain extent; it defaults to returning a single calculated value when an array would be returned. Further, its support for outputting an array even when that default is overridden is incomplete. I am not prepared to do everything that Excel does for the array functions (details below), but this PR is a start in that direction. If the default is changed via: ```php use PhpOffice\PhpSpreadsheet\Calculation\Calculation; Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY); ``` When that is done, `getCalculatedValue` will return an array (no code change necessary). However, Writer/Xlsx will now be updated to look at that value, and if an array is returned in that circumstance, will indicate in the Xml that the result is an array *and* will include a reference to the bounds of the array. This gets us close, although not completely there, to what Excel does, and may be good enough for now. Excel will still mess with the formula, but now it will treat it as `{=UNIQUE(A1:A19)}`. This means that the spreadsheet will now look correct; there will be superficial differences, but all cells will have the expected value. Technically, the major difference between what PhpSpreadsheet will output now, and what Excel does on its own, is that Excel supplies values in the xml for all the cells in the range. That would be difficult for PhpSpreadsheet to do; that could be a project for another day. Excel will treat the output from PhpSpreadsheet as "Array Formulas" (a.k.a. CSE (control shift enter) formulas because you need to use that combination of keys to manually enter them in older versions of Excel). Current versions of Excel will instead use "Dynamic Array Formulas". Dynamic Array Formulas can be changed by the user; Array Formulas need to be deleted and re-entered if you want to change them. I don't know what else might have to change to get Excel to use the latter for PhpSpreadsheet formulas, and I will probably not even try to look now, saving it for a future date. Unit testing of this change uncovered a bug in Calculation::calculateCellValue. That routine saves off ArrayReturnType, and may change it, and is supposed to restore it. But it does not do the restore if the calculation throws an exception. It is changed to do so.
109 lines
4.1 KiB
PHP
109 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
|
|
|
|
class Issue3659Test extends SetupTeardown
|
|
{
|
|
private string $arrayReturnType;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->arrayReturnType = Calculation::getArrayReturnType();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
Calculation::setArrayReturnType($this->arrayReturnType);
|
|
}
|
|
|
|
public function testTableOnOtherSheet(): void
|
|
{
|
|
$spreadsheet = $this->getSpreadsheet();
|
|
$sheet = $this->getSheet();
|
|
$sheet->setTitle('Feuil1');
|
|
$tableSheet = $spreadsheet->createSheet();
|
|
$tableSheet->setTitle('sheet_with_table');
|
|
$tableSheet->fromArray(
|
|
[
|
|
['MyCol', 'Colonne2', 'Colonne3'],
|
|
[10, 20],
|
|
[2],
|
|
[3],
|
|
[4],
|
|
],
|
|
null,
|
|
'B1',
|
|
true
|
|
);
|
|
$table = new Table('B1:D5', 'Tableau1');
|
|
$tableSheet->addTable($table);
|
|
$sheet->setSelectedCells('F7');
|
|
$tableSheet->setSelectedCells('F8');
|
|
self::assertSame($sheet, $spreadsheet->getActiveSheet());
|
|
$sheet->getCell('A1')->setValue('=SUM(Tableau1[MyCol])');
|
|
$sheet->getCell('A2')->setValue('=SUM(Tableau1[])');
|
|
$sheet->getCell('A3')->setValue('=SUM(Tableau1)');
|
|
$sheet->getCell('A4')->setValue('=CONCAT(Tableau1)');
|
|
self::assertSame(19, $sheet->getCell('A1')->getCalculatedValue());
|
|
self::assertSame(39, $sheet->getCell('A2')->getCalculatedValue());
|
|
self::assertSame(39, $sheet->getCell('A3')->getCalculatedValue());
|
|
self::assertSame('1020234', $sheet->getCell('A4')->getCalculatedValue(), 'Header row not included');
|
|
self::assertSame('F7', $sheet->getSelectedCells());
|
|
self::assertSame('F8', $tableSheet->getSelectedCells());
|
|
self::assertSame($sheet, $spreadsheet->getActiveSheet());
|
|
}
|
|
|
|
public function testTableAsArray(): void
|
|
{
|
|
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
|
|
$spreadsheet = $this->getSpreadsheet();
|
|
$sheet = $this->getSheet();
|
|
$sheet->setTitle('Feuil1');
|
|
$tableSheet = $spreadsheet->createSheet();
|
|
$tableSheet->setTitle('sheet_with_table');
|
|
$tableSheet->fromArray(
|
|
[
|
|
['MyCol', 'Colonne2', 'Colonne3'],
|
|
[10, 20],
|
|
[2],
|
|
[3],
|
|
[4],
|
|
],
|
|
null,
|
|
'B1',
|
|
true
|
|
);
|
|
$table = new Table('B1:D5', 'Tableau1');
|
|
$tableSheet->addTable($table);
|
|
$sheet->setSelectedCells('F7');
|
|
$tableSheet->setSelectedCells('F8');
|
|
self::assertSame($sheet, $spreadsheet->getActiveSheet());
|
|
$sheet->getCell('F1')->setValue('=Tableau1[MyCol]');
|
|
$sheet->getCell('H1')->setValue('=Tableau1[]');
|
|
$sheet->getCell('F9')->setValue('=Tableau1');
|
|
$sheet->getCell('J9')->setValue('=CONCAT(Tableau1)');
|
|
$sheet->getCell('J11')->setValue('=SUM(Tableau1[])');
|
|
$expectedResult = [2 => ['B' => 10], ['B' => 2], ['B' => 3], ['B' => 4]];
|
|
self::assertSame($expectedResult, $sheet->getCell('F1')->getCalculatedValue());
|
|
$expectedResult = [
|
|
2 => ['B' => 10, 'C' => 20, 'D' => null],
|
|
['B' => 2, 'C' => null, 'D' => null],
|
|
['B' => 3, 'C' => null, 'D' => null],
|
|
['B' => 4, 'C' => null, 'D' => null],
|
|
];
|
|
self::assertSame($expectedResult, $sheet->getCell('H1')->getCalculatedValue());
|
|
self::assertSame($expectedResult, $sheet->getCell('F9')->getCalculatedValue());
|
|
self::assertSame('1020234', $sheet->getCell('J9')->getCalculatedValue(), 'Header row not included');
|
|
self::assertSame(39, $sheet->getCell('J11')->getCalculatedValue(), 'Header row not included');
|
|
self::assertSame('F7', $sheet->getSelectedCells());
|
|
self::assertSame('F8', $tableSheet->getSelectedCells());
|
|
self::assertSame($sheet, $spreadsheet->getActiveSheet());
|
|
}
|
|
}
|