Skip to content

Commit 4c09d4f

Browse files
billblumePowerKiKi
authored andcommitted
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
1 parent e3fb160 commit 4c09d4f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2323
- Make newer Excel versions properly recalculate formulas on document open - [#456](https://github.com/PHPOffice/PhpSpreadsheet/issues/456)
2424
- `Coordinate::extractAllCellReferencesInRange()` throws an exception for an invalid range – [#519](https://github.com/PHPOffice/PhpSpreadsheet/issues/519)
2525
- Fixed parsing of conditionals in COUNTIF functions - [#526](https://github.com/PHPOffice/PhpSpreadsheet/issues/526)
26+
- Corruption errors for saved Xlsx docs with frozen panes - [#532](https://github.com/PHPOffice/PhpSpreadsheet/issues/532)
2627

2728
## [1.2.1] - 2018-04-10
2829

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
246246
}
247247

248248
$activeCell = $pSheet->getActiveCell();
249+
$sqref = $pSheet->getSelectedCells();
249250

250251
// Pane
251252
$pane = '';
@@ -257,6 +258,7 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
257258

258259
$topLeftCell = $pSheet->getTopLeftCell();
259260
$activeCell = $topLeftCell;
261+
$sqref = $topLeftCell;
260262

261263
// pane
262264
$pane = 'topRight';
@@ -292,7 +294,7 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
292294
$objWriter->writeAttribute('pane', $pane);
293295
}
294296
$objWriter->writeAttribute('activeCell', $activeCell);
295-
$objWriter->writeAttribute('sqref', $pSheet->getSelectedCells());
297+
$objWriter->writeAttribute('sqref', $sqref);
296298
$objWriter->endElement();
297299

298300
$objWriter->endElement();

tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,39 @@ public function testFreezePane($format)
3737
self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane');
3838
self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell');
3939
}
40+
41+
public function providerFormatsInvalidSelectedCells()
42+
{
43+
return [
44+
['Xlsx'],
45+
];
46+
}
47+
48+
/**
49+
* @dataProvider providerFormatsInvalidSelectedCells
50+
*
51+
* @param string $format
52+
*/
53+
public function testFreezePaneWithInvalidSelectedCells($format)
54+
{
55+
$cellSplit = 'A7';
56+
$topLeftCell = 'A24';
57+
58+
$spreadsheet = new Spreadsheet();
59+
$worksheet = $spreadsheet->getActiveSheet();
60+
61+
$worksheet->freezePane('A7', 'A24');
62+
$worksheet->setSelectedCells('F5');
63+
64+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
65+
66+
// Read written file
67+
$reloadedActive = $reloadedSpreadsheet->getActiveSheet();
68+
$actualCellSplit = $reloadedActive->getFreezePane();
69+
$actualTopLeftCell = $reloadedActive->getTopLeftCell();
70+
71+
self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane');
72+
self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell');
73+
self::assertSame('A24', $reloadedActive->getSelectedCells(), 'selected cell should default to be first cell after the freeze pane');
74+
}
4075
}

0 commit comments

Comments
 (0)