From 4c09d4f66852948679352515678af1c47beef2b0 Mon Sep 17 00:00:00 2001 From: Bill Blume Date: Tue, 5 Jun 2018 19:36:32 -0700 Subject: [PATCH] Properly set selected cells for frozen panes Properly set the selected cells for worksheets with frozen panes when writing Xlsx documents. Beforehand, the saved Xlsx documents were generating corruption warnings when opened in Excel. Fixes #532 Closes #535 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 4 ++- .../Functional/FreezePaneTest.php | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afc5716d..b3900ae3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Make newer Excel versions properly recalculate formulas on document open - [#456](https://github.com/PHPOffice/PhpSpreadsheet/issues/456) - `Coordinate::extractAllCellReferencesInRange()` throws an exception for an invalid range – [#519](https://github.com/PHPOffice/PhpSpreadsheet/issues/519) - Fixed parsing of conditionals in COUNTIF functions - [#526](https://github.com/PHPOffice/PhpSpreadsheet/issues/526) +- Corruption errors for saved Xlsx docs with frozen panes - [#532](https://github.com/PHPOffice/PhpSpreadsheet/issues/532) ## [1.2.1] - 2018-04-10 diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 9b8bb544..cb46a121 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -246,6 +246,7 @@ class Worksheet extends WriterPart } $activeCell = $pSheet->getActiveCell(); + $sqref = $pSheet->getSelectedCells(); // Pane $pane = ''; @@ -257,6 +258,7 @@ class Worksheet extends WriterPart $topLeftCell = $pSheet->getTopLeftCell(); $activeCell = $topLeftCell; + $sqref = $topLeftCell; // pane $pane = 'topRight'; @@ -292,7 +294,7 @@ class Worksheet extends WriterPart $objWriter->writeAttribute('pane', $pane); } $objWriter->writeAttribute('activeCell', $activeCell); - $objWriter->writeAttribute('sqref', $pSheet->getSelectedCells()); + $objWriter->writeAttribute('sqref', $sqref); $objWriter->endElement(); $objWriter->endElement(); diff --git a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php index 8c0644d5..fda67231 100644 --- a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php +++ b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php @@ -37,4 +37,39 @@ class FreezePaneTest extends AbstractFunctional self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane'); self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); } + + public function providerFormatsInvalidSelectedCells() + { + return [ + ['Xlsx'], + ]; + } + + /** + * @dataProvider providerFormatsInvalidSelectedCells + * + * @param string $format + */ + public function testFreezePaneWithInvalidSelectedCells($format) + { + $cellSplit = 'A7'; + $topLeftCell = 'A24'; + + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + + $worksheet->freezePane('A7', 'A24'); + $worksheet->setSelectedCells('F5'); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + + // Read written file + $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); + $actualCellSplit = $reloadedActive->getFreezePane(); + $actualTopLeftCell = $reloadedActive->getTopLeftCell(); + + self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane'); + self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); + self::assertSame('A24', $reloadedActive->getSelectedCells(), 'selected cell should default to be first cell after the freeze pane'); + } }