Skip to content

Commit fb37938

Browse files
oleibmanPowerKiKi
authored andcommitted
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
1 parent 06d9dc0 commit fb37938

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2020
- Fix Xlsx Writer's handling of decimal commas [#1282](https://github.com/PHPOffice/PhpSpreadsheet/pull/1282)
2121
- Fix for issue by removing test code mistakenly left in [#1328](https://github.com/PHPOffice/PhpSpreadsheet/pull/1328)
2222
- Fix for Xls writer wrong selected cells and active sheet [#1256](https://github.com/PHPOffice/PhpSpreadsheet/pull/1256)
23+
- Fix active cell when freeze pane is used [#1323](https://github.com/PHPOffice/PhpSpreadsheet/pull/1323)
2324

2425
## [1.10.1] - 2019-12-02
2526

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $
262262
--$ySplit;
263263

264264
$topLeftCell = $pSheet->getTopLeftCell();
265-
$activeCell = $topLeftCell;
266-
$sqref = $topLeftCell;
267265

268266
// pane
269267
$pane = 'topRight';

tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php

+60-9
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,8 @@ public function testFreezePane($format)
3838
self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell');
3939
}
4040

41-
public function providerFormatsInvalidSelectedCells()
42-
{
43-
return [
44-
['Xlsx'],
45-
];
46-
}
47-
4841
/**
49-
* @dataProvider providerFormatsInvalidSelectedCells
42+
* @dataProvider providerFormats
5043
*
5144
* @param string $format
5245
*/
@@ -70,6 +63,64 @@ public function testFreezePaneWithInvalidSelectedCells($format)
7063

7164
self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane');
7265
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');
66+
self::assertSame('F5', $reloadedActive->getSelectedCells());
67+
}
68+
69+
/**
70+
* @dataProvider providerFormats
71+
*
72+
* @param string $format
73+
*/
74+
public function testFreezePaneUserSelectedCell($format)
75+
{
76+
$spreadsheet = new Spreadsheet();
77+
$worksheet = $spreadsheet->getActiveSheet();
78+
$worksheet->setCellValue('A1', 'Header1');
79+
$worksheet->setCellValue('B1', 'Header2');
80+
$worksheet->setCellValue('C1', 'Header3');
81+
$worksheet->setCellValue('A2', 'Data1');
82+
$worksheet->setCellValue('B2', 'Data2');
83+
$worksheet->setCellValue('C2', 'Data3');
84+
$worksheet->setCellValue('A3', 'Data4');
85+
$worksheet->setCellValue('B3', 'Data5');
86+
$worksheet->setCellValue('C3', 'Data6');
87+
$worksheet->freezePane('A2');
88+
$worksheet->setSelectedCells('C3');
89+
90+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
91+
// Read written file
92+
$reloadedActive = $reloadedSpreadsheet->getActiveSheet();
93+
94+
$expected = 'C3';
95+
self::assertSame($expected, $reloadedActive->getSelectedCells());
96+
}
97+
98+
/**
99+
* @dataProvider providerFormats
100+
*
101+
* @param string $format
102+
*/
103+
public function testNoFreezePaneUserSelectedCell($format)
104+
{
105+
$spreadsheet = new Spreadsheet();
106+
$worksheet = $spreadsheet->getActiveSheet();
107+
$worksheet->setCellValue('A1', 'Header1');
108+
$worksheet->setCellValue('B1', 'Header2');
109+
$worksheet->setCellValue('C1', 'Header3');
110+
$worksheet->setCellValue('A2', 'Data1');
111+
$worksheet->setCellValue('B2', 'Data2');
112+
$worksheet->setCellValue('C2', 'Data3');
113+
$worksheet->setCellValue('A3', 'Data4');
114+
$worksheet->setCellValue('B3', 'Data5');
115+
$worksheet->setCellValue('C3', 'Data6');
116+
//$worksheet->freezePane('A2');
117+
$worksheet->setSelectedCells('C3');
118+
119+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
120+
// Read written file
121+
$reloadedActive = $reloadedSpreadsheet->getActiveSheet();
122+
123+
$expected = 'C3';
124+
self::assertSame($expected, $reloadedActive->getSelectedCells());
74125
}
75126
}

0 commit comments

Comments
 (0)