PrintArea Causes Exception
I wanted to investigate #1523, but I couldn't duplicate its results because the sample code in the issue caused an Exception to be thrown. The exception happened because, when the Print Range Set method is set to Insert (as oppposed to Overwrite), and the Print Range is empty, it is created with a leading or trailing comma, and Writer/Xlsx objects. This is, in a sense, a user error, but the software should be more resilient. This can be accomplished by ensuring that no leading or trailing comma is generated when Print Range is empty. This code change fixes that problem. Since I couldn't reproduce the original, I can't say for sure that it fixes it. However, with the sample code provided, I can write a spreadsheet which Excel reads without any problems, so it probably fixes the original. Closes #1544 Fixes #1523
This commit is contained in:
parent
234580323e
commit
7ddf6ccf41
|
@ -678,6 +678,9 @@ class PageSetup
|
|||
throw new PhpSpreadsheetException('Cell coordinate must not be absolute.');
|
||||
}
|
||||
$value = strtoupper($value);
|
||||
if (!$this->printArea) {
|
||||
$index = 0;
|
||||
}
|
||||
|
||||
if ($method == self::SETPRINTRANGE_OVERWRITE) {
|
||||
if ($index == 0) {
|
||||
|
@ -695,7 +698,7 @@ class PageSetup
|
|||
}
|
||||
} elseif ($method == self::SETPRINTRANGE_INSERT) {
|
||||
if ($index == 0) {
|
||||
$this->printArea .= ($this->printArea == '') ? $value : ',' . $value;
|
||||
$this->printArea = $this->printArea ? ($this->printArea . ',' . $value) : $value;
|
||||
} else {
|
||||
$printAreas = explode(',', $this->printArea);
|
||||
if ($index < 0) {
|
||||
|
|
|
@ -22,7 +22,7 @@ class PrintAreaTest extends AbstractFunctional
|
|||
*/
|
||||
public function testPageSetup($format): void
|
||||
{
|
||||
// Create new workbook with 3 sheets and different print areas
|
||||
// Create new workbook with 6 sheets and different print areas
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet1 = $spreadsheet->getActiveSheet()->setTitle('Sheet 1');
|
||||
$worksheet1->getPageSetup()->setPrintArea('A1:B1');
|
||||
|
@ -34,16 +34,25 @@ class PrintAreaTest extends AbstractFunctional
|
|||
|
||||
$worksheet4 = $spreadsheet->createSheet()->setTitle('Sheet 4');
|
||||
$worksheet4->getPageSetup()->setPrintArea('A4:B4,D1:E4');
|
||||
$worksheet5 = $spreadsheet->createSheet()->setTitle('Sheet 5');
|
||||
$worksheet5->getPageSetup()->addPrintAreaByColumnAndRow(1, 1, 10, 10, 1);
|
||||
$worksheet6 = $spreadsheet->createSheet()->setTitle('Sheet 6');
|
||||
$worksheet6->getPageSetup()->addPrintAreaByColumnAndRow(1, 1, 10, 10, 1);
|
||||
$worksheet6->getPageSetup()->addPrintAreaByColumnAndRow(12, 1, 12, 10, 1);
|
||||
|
||||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader): void {
|
||||
$reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3', 'Sheet 4']);
|
||||
$reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3', 'Sheet 4', 'Sheet 5', 'Sheet 6']);
|
||||
});
|
||||
|
||||
$actual1 = $reloadedSpreadsheet->getSheetByName('Sheet 1')->getPageSetup()->getPrintArea();
|
||||
$actual3 = $reloadedSpreadsheet->getSheetByName('Sheet 3')->getPageSetup()->getPrintArea();
|
||||
$actual4 = $reloadedSpreadsheet->getSheetByName('Sheet 4')->getPageSetup()->getPrintArea();
|
||||
$actual5 = $reloadedSpreadsheet->getSheetByName('Sheet 5')->getPageSetup()->getPrintArea();
|
||||
$actual6 = $reloadedSpreadsheet->getSheetByName('Sheet 6')->getPageSetup()->getPrintArea();
|
||||
self::assertSame('A1:B1', $actual1, 'should be able to write and read normal page setup');
|
||||
self::assertSame('A3:B3', $actual3, 'should be able to write and read page setup even when skipping sheets');
|
||||
self::assertSame('A4:B4,D1:E4', $actual4, 'should be able to write and read page setup with multiple print areas');
|
||||
self::assertSame('A1:J10', $actual5, 'add by column and row');
|
||||
self::assertSame('A1:J10,L1:L10', $actual6, 'multiple add by column and row');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue