From fb379385e07a8500792694ca0c7ec4f5ff43aa0a Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Tue, 14 Jan 2020 19:44:06 -0800 Subject: [PATCH] Fix active cell when freeze pane is used When freeze pane is in use on a worksheet, PhpSpreadsheet saves to Xlsx in such a way that the active cell is always set to the top left cell below the freeze pane. I find it difficult to understand why: 1. You have given users the setSelectedCells function, but then choose to ignore it. 2. Excel itself does not act in this manner. 3. PHPExcel did not act in this manner. 4. PhpSpreadsheet when writing to Xls does not act in this manner. This is especially emphasized because the one test in FreezePaneTest which would expose the difference is the only test in that member which is not made for both Xls and Xlsx. 5. It is *really* useful to be able to open a spreadsheet anywhere, even when it has header rows. Closes #1323 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 2 - .../Functional/FreezePaneTest.php | 69 ++++++++++++++++--- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 541e9279..4286fa9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix Xlsx Writer's handling of decimal commas [#1282](https://github.com/PHPOffice/PhpSpreadsheet/pull/1282) - Fix for issue by removing test code mistakenly left in [#1328](https://github.com/PHPOffice/PhpSpreadsheet/pull/1328) - Fix for Xls writer wrong selected cells and active sheet [#1256](https://github.com/PHPOffice/PhpSpreadsheet/pull/1256) +- Fix active cell when freeze pane is used [#1323](https://github.com/PHPOffice/PhpSpreadsheet/pull/1323) ## [1.10.1] - 2019-12-02 diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index bf811bdc..1d5a995a 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -262,8 +262,6 @@ class Worksheet extends WriterPart --$ySplit; $topLeftCell = $pSheet->getTopLeftCell(); - $activeCell = $topLeftCell; - $sqref = $topLeftCell; // pane $pane = 'topRight'; diff --git a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php index fda67231..38709716 100644 --- a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php +++ b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php @@ -38,15 +38,8 @@ class FreezePaneTest extends AbstractFunctional self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); } - public function providerFormatsInvalidSelectedCells() - { - return [ - ['Xlsx'], - ]; - } - /** - * @dataProvider providerFormatsInvalidSelectedCells + * @dataProvider providerFormats * * @param string $format */ @@ -70,6 +63,64 @@ 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'); - self::assertSame('A24', $reloadedActive->getSelectedCells(), 'selected cell should default to be first cell after the freeze pane'); + self::assertSame('F5', $reloadedActive->getSelectedCells()); + } + + /** + * @dataProvider providerFormats + * + * @param string $format + */ + public function testFreezePaneUserSelectedCell($format) + { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + $worksheet->setCellValue('A1', 'Header1'); + $worksheet->setCellValue('B1', 'Header2'); + $worksheet->setCellValue('C1', 'Header3'); + $worksheet->setCellValue('A2', 'Data1'); + $worksheet->setCellValue('B2', 'Data2'); + $worksheet->setCellValue('C2', 'Data3'); + $worksheet->setCellValue('A3', 'Data4'); + $worksheet->setCellValue('B3', 'Data5'); + $worksheet->setCellValue('C3', 'Data6'); + $worksheet->freezePane('A2'); + $worksheet->setSelectedCells('C3'); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + // Read written file + $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); + + $expected = 'C3'; + self::assertSame($expected, $reloadedActive->getSelectedCells()); + } + + /** + * @dataProvider providerFormats + * + * @param string $format + */ + public function testNoFreezePaneUserSelectedCell($format) + { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + $worksheet->setCellValue('A1', 'Header1'); + $worksheet->setCellValue('B1', 'Header2'); + $worksheet->setCellValue('C1', 'Header3'); + $worksheet->setCellValue('A2', 'Data1'); + $worksheet->setCellValue('B2', 'Data2'); + $worksheet->setCellValue('C2', 'Data3'); + $worksheet->setCellValue('A3', 'Data4'); + $worksheet->setCellValue('B3', 'Data5'); + $worksheet->setCellValue('C3', 'Data6'); + //$worksheet->freezePane('A2'); + $worksheet->setSelectedCells('C3'); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + // Read written file + $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); + + $expected = 'C3'; + self::assertSame($expected, $reloadedActive->getSelectedCells()); } }