Skip to content

Commit 762d73d

Browse files
committed
Addsheet May Leave Active Sheet Uninitialized
Fix #4112. Direct cause is that `applyStylesFromArray` tries to save and restore `activeSheetIndex`. However, if activeSheetIndex is -1, indicating no active sheet, the restore should not be attempted. Code is changed to test before attempting to restore. The actual problem, however, is that user specified a sheet number for `addSheet`. That method will set activeSheetIndex most of the time, but this was a gap - when the supplied sheet number (0 in this case) is greater than activeSheetIndex (-1 in this case), it was leaving activeSheetIndex as -1. It is changed to set activeSheetIndex to 0 when activeSheetIndex is negative.
1 parent b406367 commit 762d73d

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2525

2626
### Fixed
2727

28-
- Nothing
28+
- Add Sheet may leave Active Sheet uninitialized. [Issue #4112](https://github.com/PHPOffice/PhpSpreadsheet/issues/4112) [PR #4113](https://github.com/PHPOffice/PhpSpreadsheet/pull/4113)
2929

3030
## 2024-07-24 - 2.2.0
3131

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null): Workshe
558558
if ($this->activeSheetIndex >= $sheetIndex) {
559559
++$this->activeSheetIndex;
560560
}
561+
if ($this->activeSheetIndex < 0) {
562+
$this->activeSheetIndex = 0;
563+
}
561564
}
562565

563566
if ($worksheet->getParent() === null) {

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3684,7 +3684,9 @@ public function applyStylesFromArray(string $coordinate, array $styleArray): boo
36843684
$originalSelected = $this->selectedCells;
36853685
$this->getStyle($coordinate)->applyFromArray($styleArray);
36863686
$this->selectedCells = $originalSelected;
3687-
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
3687+
if ($activeSheetIndex >= 0) {
3688+
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
3689+
}
36883690

36893691
return true;
36903692
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10+
11+
class Issue4112Test extends AbstractFunctional
12+
{
13+
/**
14+
* Problem deleting all sheets then adding one.
15+
*
16+
* @dataProvider providerSheetNumber
17+
*/
18+
public function testIssue4112(?int $sheetNumber): void
19+
{
20+
$mySpreadsheet = new Spreadsheet();
21+
$mySpreadsheet->removeSheetByIndex(0);
22+
$worksheet = new Worksheet($mySpreadsheet, 'addedsheet');
23+
self::assertSame(-1, $mySpreadsheet->getActiveSheetIndex());
24+
$mySpreadsheet->addSheet($worksheet, 0);
25+
self::assertSame('addedsheet', $mySpreadsheet->getActiveSheet()->getTitle());
26+
$row = 1;
27+
$col = 1;
28+
$worksheet->getCell([$col, $row])->setValue('id_uti');
29+
self::assertSame('id_uti', $worksheet->getCell([$col, $row])->getValue());
30+
$mySpreadsheet->disconnectWorksheets();
31+
}
32+
33+
public static function providerSheetNumber(): array
34+
{
35+
return [
36+
'problem case' => [0],
37+
'normal case' => [null],
38+
'negative 1 (as if there were no sheets)' => [-1],
39+
'diffeent negative number' => [-4],
40+
'positive number' => [4],
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)