Skip to content

Commit

Permalink
Properly set selected cells for frozen panes
Browse files Browse the repository at this point in the history
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 PHPOffice#532
Closes PHPOffice#535
  • Loading branch information
billblume authored and Frederic Delaunay committed Oct 29, 2018
1 parent 9f514a4 commit 3d81c71
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Expand Up @@ -246,6 +246,7 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
}

$activeCell = $pSheet->getActiveCell();
$sqref = $pSheet->getSelectedCells();

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

$topLeftCell = $pSheet->getTopLeftCell();
$activeCell = $topLeftCell;
$sqref = $topLeftCell;

// pane
$pane = 'topRight';
Expand Down Expand Up @@ -292,7 +294,7 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
$objWriter->writeAttribute('pane', $pane);
}
$objWriter->writeAttribute('activeCell', $activeCell);
$objWriter->writeAttribute('sqref', $pSheet->getSelectedCells());
$objWriter->writeAttribute('sqref', $sqref);
$objWriter->endElement();

$objWriter->endElement();
Expand Down
35 changes: 35 additions & 0 deletions tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php
Expand Up @@ -37,4 +37,39 @@ public function testFreezePane($format)
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');
}
}

0 comments on commit 3d81c71

Please sign in to comment.