diff --git a/src/PhpSpreadsheet/Worksheet/PageSetup.php b/src/PhpSpreadsheet/Worksheet/PageSetup.php index d8d5098f..d1a22a7b 100644 --- a/src/PhpSpreadsheet/Worksheet/PageSetup.php +++ b/src/PhpSpreadsheet/Worksheet/PageSetup.php @@ -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) { diff --git a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php index a335fd2f..584f6dd5 100644 --- a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php +++ b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php @@ -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'); } }