From fdc224af7cc1d0d386fd91770869d99af0eafab3 Mon Sep 17 00:00:00 2001 From: Guillaume RODRIGUEZ Date: Tue, 23 Oct 2018 11:29:09 +0200 Subject: [PATCH] Fix print area parser for XLSX reader XLSX workbook references may not contains quotes in print area Fixes #733 Fixes #734 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Xlsx.php | 2 +- tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php | 7 ++++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f6cb5f..1c3ef129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Support numeric condition in SUMIF, SUMIFS, AVERAGEIF, COUNTIF, MAXIF and MINIF - [#683](https://github.com/PHPOffice/PhpSpreadsheet/issues/683) - SUMIFS containing multiple conditions - [#704](https://github.com/PHPOffice/PhpSpreadsheet/issues/704) - Csv reader avoid notice when the file is empty - [#743](https://github.com/PHPOffice/PhpSpreadsheet/pull/743) +- Fix print area parser for XLSX reader - [#734](https://github.com/PHPOffice/PhpSpreadsheet/pull/734) ## [1.5.0] - 2018-10-21 diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index dfb4b825..fa242a06 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -1830,7 +1830,7 @@ class Xlsx extends BaseReader break; case '_xlnm.Print_Area': - $rangeSets = preg_split("/'(.*?)'(?:![A-Z0-9]+:[A-Z0-9]+,?)/", $extractedRange, PREG_SPLIT_NO_EMPTY); + $rangeSets = preg_split("/('?(?:.*?)'?(?:![A-Z0-9]+:[A-Z0-9]+)),?/", $extractedRange, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $newRangeSets = []; foreach ($rangeSets as $rangeSet) { list($sheetName, $rangeSet) = Worksheet::extractSheetTitle($rangeSet, true); diff --git a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php index 1a6709c7..7c3a9112 100644 --- a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php +++ b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php @@ -32,13 +32,18 @@ class PrintAreaTest extends AbstractFunctional $sheet->getPageSetup()->setPrintArea("A$i:B$i"); } + $worksheet4 = $spreadsheet->createSheet()->setTitle('Sheet 4'); + $worksheet4->getPageSetup()->setPrintArea('A4:B4,D1:E4'); + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader) { - $reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3']); + $reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3', 'Sheet 4']); }); $actual1 = $reloadedSpreadsheet->getSheetByName('Sheet 1')->getPageSetup()->getPrintArea(); $actual3 = $reloadedSpreadsheet->getSheetByName('Sheet 3')->getPageSetup()->getPrintArea(); + $actual4 = $reloadedSpreadsheet->getSheetByName('Sheet 4')->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'); } }