forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Xlsx Reader Merge Range For Entire Column(s) or Row(s)
Fix PHPOffice#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.
- Loading branch information
Showing
5 changed files
with
217 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?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(); | ||
} | ||
} |
Binary file not shown.