mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 17:18:18 +00:00
b6bd822b9c
* 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.
71 lines
2.2 KiB
PHP
71 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class Issue2501Test extends TestCase
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private static $testbook = 'tests/data/Reader/XLSX/issue.2501.b.xlsx';
|
|
|
|
public function testPreliminaries(): void
|
|
{
|
|
$file = 'zip://';
|
|
$file .= self::$testbook;
|
|
$file .= '#xl/worksheets/sheet1.xml';
|
|
$data = file_get_contents($file);
|
|
// confirm that file contains expected merge ranges
|
|
if ($data === false) {
|
|
self::fail('Unable to read file');
|
|
} else {
|
|
self::assertStringContainsString('<mergeCells count="3"><mergeCell ref="A:A"/><mergeCell ref="B:D"/><mergeCell ref="E2:E4"/></mergeCells>', $data);
|
|
}
|
|
$file = 'zip://';
|
|
$file .= self::$testbook;
|
|
$file .= '#xl/worksheets/sheet2.xml';
|
|
$data = file_get_contents($file);
|
|
// confirm that file contains expected merged ranges
|
|
if ($data === false) {
|
|
self::fail('Unable to read file');
|
|
} else {
|
|
self::assertStringContainsString('<mergeCells count="3"><mergeCell ref="1:1"/><mergeCell ref="2:4"/><mergeCell ref="B5:D5"/></mergeCells>', $data);
|
|
}
|
|
}
|
|
|
|
public function testIssue2501(): void
|
|
{
|
|
// Merged cell range specified as 1:1"
|
|
$filename = self::$testbook;
|
|
$reader = IOFactory::createReader('Xlsx');
|
|
$spreadsheet = $reader->load($filename);
|
|
$sheet = $spreadsheet->getSheetByName('Columns');
|
|
$expected = [
|
|
'A1:A1048576',
|
|
'B1:D1048576',
|
|
'E2:E4',
|
|
];
|
|
if ($sheet === null) {
|
|
self::fail('Unable to find sheet Columns');
|
|
} else {
|
|
self::assertSame($expected, array_values($sheet->getMergeCells()));
|
|
}
|
|
$sheet = $spreadsheet->getSheetByName('Rows');
|
|
$expected = [
|
|
'A1:XFD1',
|
|
'A2:XFD4',
|
|
'B5:D5',
|
|
];
|
|
if ($sheet === null) {
|
|
self::fail('Unable to find sheet Rows');
|
|
} else {
|
|
self::assertSame($expected, array_values($sheet->getMergeCells()));
|
|
}
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|