Skip to content

Commit ce78635

Browse files
authored
Fix for 1735 (Incorrect activeSheetIndex after RemoveSheetByIndex) (#1743)
This is a fix for issue #1735. It adds tests for this situation, and similar situations involving adding new sheets and accessing existing ones. Coverage for Spreadsheet.php increases from 69% to 75% as a result.
1 parent baa9c45 commit ce78635

File tree

2 files changed

+143
-2
lines changed

2 files changed

+143
-2
lines changed

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ public function removeSheetByIndex($pIndex): void
665665
// Adjust active sheet index if necessary
666666
if (
667667
($this->activeSheetIndex >= $pIndex) &&
668-
($pIndex > count($this->workSheetCollection) - 1)
668+
($this->activeSheetIndex > 0 || $numSheets <= 1)
669669
) {
670670
--$this->activeSheetIndex;
671671
}

tests/PhpSpreadsheetTests/SpreadsheetTest.php

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,147 @@ public function dataProviderForSheetNames()
5151
*/
5252
public function testGetSheetByName($index, $sheetName): void
5353
{
54-
self::assertEquals($this->object->getSheet($index), $this->object->getSheetByName($sheetName));
54+
self::assertSame($this->object->getSheet($index), $this->object->getSheetByName($sheetName));
55+
}
56+
57+
public function testAddSheetDuplicateTitle(): void
58+
{
59+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
60+
$sheet = new Worksheet();
61+
$sheet->setTitle('someSheet2');
62+
$this->object->addSheet($sheet);
63+
}
64+
65+
public function testAddSheetNoAdjustActive(): void
66+
{
67+
$this->object->setActiveSheetIndex(2);
68+
self::assertEquals(2, $this->object->getActiveSheetIndex());
69+
$sheet = new Worksheet();
70+
$sheet->setTitle('someSheet4');
71+
$this->object->addSheet($sheet);
72+
self::assertEquals(2, $this->object->getActiveSheetIndex());
73+
}
74+
75+
public function testAddSheetAdjustActive(): void
76+
{
77+
$this->object->setActiveSheetIndex(2);
78+
self::assertEquals(2, $this->object->getActiveSheetIndex());
79+
$sheet = new Worksheet();
80+
$sheet->setTitle('someSheet0');
81+
$this->object->addSheet($sheet, 0);
82+
self::assertEquals(3, $this->object->getActiveSheetIndex());
83+
}
84+
85+
public function testRemoveSheetIndexTooHigh(): void
86+
{
87+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
88+
$this->object->removeSheetByIndex(4);
89+
}
90+
91+
public function testRemoveSheetNoAdjustActive(): void
92+
{
93+
$this->object->setActiveSheetIndex(1);
94+
self::assertEquals(1, $this->object->getActiveSheetIndex());
95+
$this->object->removeSheetByIndex(2);
96+
self::assertEquals(1, $this->object->getActiveSheetIndex());
97+
}
98+
99+
public function testRemoveSheetAdjustActive(): void
100+
{
101+
$this->object->setActiveSheetIndex(2);
102+
self::assertEquals(2, $this->object->getActiveSheetIndex());
103+
$this->object->removeSheetByIndex(1);
104+
self::assertEquals(1, $this->object->getActiveSheetIndex());
105+
}
106+
107+
public function testGetSheetIndexTooHigh(): void
108+
{
109+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
110+
$this->object->getSheet(4);
111+
}
112+
113+
public function testGetIndexNonExistent(): void
114+
{
115+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
116+
$sheet = new Worksheet();
117+
$sheet->setTitle('someSheet4');
118+
$this->object->getIndex($sheet);
119+
}
120+
121+
public function testSetIndexByName(): void
122+
{
123+
$this->object->setIndexByName('someSheet1', 1);
124+
self::assertEquals('someSheet2', $this->object->getSheet(0)->getTitle());
125+
self::assertEquals('someSheet1', $this->object->getSheet(1)->getTitle());
126+
self::assertEquals('someSheet 3', $this->object->getSheet(2)->getTitle());
127+
}
128+
129+
public function testRemoveAllSheets(): void
130+
{
131+
$this->object->setActiveSheetIndex(2);
132+
self::assertEquals(2, $this->object->getActiveSheetIndex());
133+
$this->object->removeSheetByIndex(0);
134+
self::assertEquals(1, $this->object->getActiveSheetIndex());
135+
$this->object->removeSheetByIndex(0);
136+
self::assertEquals(0, $this->object->getActiveSheetIndex());
137+
$this->object->removeSheetByIndex(0);
138+
self::assertEquals(-1, $this->object->getActiveSheetIndex());
139+
$sheet = new Worksheet();
140+
$sheet->setTitle('someSheet4');
141+
$this->object->addSheet($sheet);
142+
self::assertEquals(0, $this->object->getActiveSheetIndex());
143+
}
144+
145+
public function testBug1735(): void
146+
{
147+
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
148+
$spreadsheet->createSheet()->setTitle('addedsheet');
149+
$spreadsheet->setActiveSheetIndex(1);
150+
$spreadsheet->removeSheetByIndex(0);
151+
$sheet = $spreadsheet->getActiveSheet();
152+
self::assertEquals('addedsheet', $sheet->getTitle());
153+
}
154+
155+
public function testSetActiveSheetIndexTooHigh(): void
156+
{
157+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
158+
$this->object->setActiveSheetIndex(4);
159+
}
160+
161+
public function testSetActiveSheetNoSuchName(): void
162+
{
163+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
164+
$this->object->setActiveSheetIndexByName('unknown');
165+
}
166+
167+
public function testAddExternal(): void
168+
{
169+
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
170+
$sheet = $spreadsheet->createSheet()->setTitle('someSheet19');
171+
$sheet->getCell('A1')->setValue(1);
172+
$sheet->getCell('A1')->getStyle()->getFont()->setBold(true);
173+
$sheet->getCell('B1')->getStyle()->getFont()->setSuperscript(true);
174+
$sheet->getCell('C1')->getStyle()->getFont()->setSubscript(true);
175+
self::assertCount(4, $spreadsheet->getCellXfCollection());
176+
self::assertEquals(1, $sheet->getCell('A1')->getXfIndex());
177+
$this->object->getActiveSheet()->getCell('A1')->getStyle()->getFont()->setBold(true);
178+
self::assertCount(2, $this->object->getCellXfCollection());
179+
$sheet3 = $this->object->addExternalSheet($sheet);
180+
self::assertCount(6, $this->object->getCellXfCollection());
181+
self::assertEquals('someSheet19', $sheet3->getTitle());
182+
self::assertEquals(1, $sheet3->getCell('A1')->getValue());
183+
self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getBold());
184+
// Prove Xf index changed although style is same.
185+
self::assertEquals(3, $sheet3->getCell('A1')->getXfIndex());
186+
}
187+
188+
public function testAddExternalDuplicateName(): void
189+
{
190+
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
191+
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
192+
$sheet = $spreadsheet->createSheet()->setTitle('someSheet1');
193+
$sheet->getCell('A1')->setValue(1);
194+
$sheet->getCell('A1')->getStyle()->getFont()->setBold(true);
195+
$this->object->addExternalSheet($sheet);
55196
}
56197
}

0 commit comments

Comments
 (0)