From 1adc3a66881c174a420ad8fb1069611eda34a7c4 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 25 Feb 2018 19:38:53 +0100 Subject: [PATCH] Read printing area correctly when skipping some sheets Fixes #371 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Xlsx.php | 2 +- .../Functional/AbstractFunctional.php | 6 ++- .../Functional/PrintAreaTest.php | 44 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c6df07..82eff442 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) - Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167) - Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379) +- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371) ## [1.1.0] - 2018-01-28 diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index d6282615..b54622cd 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -1700,7 +1700,7 @@ class Xlsx extends BaseReader } // Some definedNames are only applicable if we are on the same sheet... - if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $sheetId) { + if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $oldSheetId) { // Switch on type switch ((string) $definedName['name']) { case '_xlnm._FilterDatabase': diff --git a/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php b/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php index e531bfb5..da9f76e0 100644 --- a/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php +++ b/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php @@ -17,16 +17,20 @@ abstract class AbstractFunctional extends TestCase * * @param Spreadsheet $spreadsheet * @param string $format + * @param null|callable $readerCustomizer * * @return Spreadsheet */ - protected function writeAndReload(Spreadsheet $spreadsheet, $format) + protected function writeAndReload(Spreadsheet $spreadsheet, $format, callable $readerCustomizer = null) { $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); $writer = IOFactory::createWriter($spreadsheet, $format); $writer->save($filename); $reader = IOFactory::createReader($format); + if ($readerCustomizer) { + $readerCustomizer($reader); + } $reloadedSpreadsheet = $reader->load($filename); unlink($filename); diff --git a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php new file mode 100644 index 00000000..1a6709c7 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php @@ -0,0 +1,44 @@ +getActiveSheet()->setTitle('Sheet 1'); + $worksheet1->getPageSetup()->setPrintArea('A1:B1'); + + for ($i = 2; $i < 4; ++$i) { + $sheet = $spreadsheet->createSheet()->setTitle("Sheet $i"); + $sheet->getPageSetup()->setPrintArea("A$i:B$i"); + } + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader) { + $reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3']); + }); + + $actual1 = $reloadedSpreadsheet->getSheetByName('Sheet 1')->getPageSetup()->getPrintArea(); + $actual3 = $reloadedSpreadsheet->getSheetByName('Sheet 3')->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'); + } +}