Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/MergedCellTest.php
T
oleibman b6bd822b9c Xlsx Reader Merge Range For Entire Column(s) or Row(s) (#2504)
* Xlsx Reader Merge Range For Entire Column(s) or Row(s)

Fix #2501. Merge range can be supplied as entire rows or columns, e.g. `1:1` or `A:C`. PhpSpreadsheet is expecting a row and a column to be specified for both parts of the range, and fails when the unexpected format shows up.

The code to clear cells within the merge range is very inefficient in terms of both memory and time, especially when the range is large (e.g. for an entire row or column). More efficient code is substituted. It is possible that we can get even more efficient by deleting the cleared cells rather than setting them to null. However, that needs more research, and there is no reason to delay this fix while I am researching.

When Xlsx Writer encounters a null cell, it writes it to the output file. For cell merges (especially involving whole rows or columns), this results in a lot of useless output. It is changed to skip the output of null cells when (a) the cell style matches its row's style, or (b) the row style is not specified and the cell style matches its column's style.

* Scrutinizer

See if these changes appease it.

* Improved CellIterators

Finally figured out how to improve efficiency here, meaning that there is no longer a reason to change Writer/Xlsx, so restore that.

* No Change for CellIterator

I had thought a change was needed for CellIterator, but it isn't.
2022-01-23 10:44:09 -08:00

122 lines
3.8 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Exception as SpreadException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class MergedCellTest extends TestCase
{
/**
* @param mixed $expectedResult
*
* @dataProvider providerWorksheetFormulaeColumns
*/
public function testMergedCellColumns(string $formula, $expectedResult): void
{
$spreadSheet = new Spreadsheet();
$dataSheet = $spreadSheet->getActiveSheet();
$dataSheet->setCellValue('A5', 3.3);
$dataSheet->setCellValue('A3', 3.3);
$dataSheet->setCellValue('A2', 2.2);
$dataSheet->setCellValue('A1', 1.1);
$dataSheet->setCellValue('B2', 2.2);
$dataSheet->setCellValue('B1', 1.1);
$dataSheet->setCellValue('C2', 4.4);
$dataSheet->setCellValue('C1', 3.3);
$dataSheet->mergeCells('A2:A4');
$dataSheet->mergeCells('B:B');
$worksheet = $spreadSheet->getActiveSheet();
$worksheet->setCellValue('A7', $formula);
$result = $worksheet->getCell('A7')->getCalculatedValue();
self::assertSame($expectedResult, $result);
$spreadSheet->disconnectWorksheets();
}
public function providerWorksheetFormulaeColumns(): array
{
return [
['=SUM(A1:A5)', 6.6],
['=COUNT(A1:A5)', 3],
['=COUNTA(A1:A5)', 3],
['=SUM(A3:A4)', 0],
['=A2+A3+A4', 2.2],
['=A2/A3', Functions::DIV0()],
['=SUM(B1:C2)', 8.8],
];
}
/**
* @param mixed $expectedResult
*
* @dataProvider providerWorksheetFormulaeRows
*/
public function testMergedCellRows(string $formula, $expectedResult): void
{
$spreadSheet = new Spreadsheet();
$dataSheet = $spreadSheet->getActiveSheet();
$dataSheet->setCellValue('A1', 1.1);
$dataSheet->setCellValue('B1', 2.2);
$dataSheet->setCellValue('C1', 3.3);
$dataSheet->setCellValue('E1', 3.3);
$dataSheet->setCellValue('A2', 1.1);
$dataSheet->setCellValue('B2', 2.2);
$dataSheet->setCellValue('A3', 3.3);
$dataSheet->setCellValue('B3', 4.4);
$dataSheet->mergeCells('B1:D1');
$dataSheet->mergeCells('A2:B2');
$worksheet = $spreadSheet->getActiveSheet();
$worksheet->setCellValue('A7', $formula);
$result = $worksheet->getCell('A7')->getCalculatedValue();
self::assertSame($expectedResult, $result);
$spreadSheet->disconnectWorksheets();
}
public function providerWorksheetFormulaeRows(): array
{
return [
['=SUM(A1:E1)', 6.6],
['=COUNT(A1:E1)', 3],
['=COUNTA(A1:E1)', 3],
['=SUM(C1:D1)', 0],
['=B1+C1+D1', 2.2],
['=B1/C1', Functions::DIV0()],
['=SUM(A2:B3)', 8.8],
];
}
private function setBadRange(Worksheet $sheet, string $range): void
{
try {
$sheet->mergeCells($range);
self::fail("Expected invalid merge range $range");
} catch (SpreadException $e) {
self::assertSame('Merge must be set on a range of cells.', $e->getMessage());
}
}
public function testMergedBadRange(): void
{
$spreadSheet = new Spreadsheet();
$dataSheet = $spreadSheet->getActiveSheet();
$this->setBadRange($dataSheet, 'B1');
$this->setBadRange($dataSheet, 'Invalid');
$this->setBadRange($dataSheet, '1');
$this->setBadRange($dataSheet, 'C');
$this->setBadRange($dataSheet, 'B1:C');
$this->setBadRange($dataSheet, 'B:C2');
$spreadSheet->disconnectWorksheets();
}
}