Skip to content

Commit 2329e90

Browse files
committed
Use case insentive comparison to get sheet name
1 parent f9eb35d commit 2329e90

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/PhpSpreadsheet/Spreadsheet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ public function getSheetByName($worksheetName)
701701
{
702702
$worksheetCount = count($this->workSheetCollection);
703703
for ($i = 0; $i < $worksheetCount; ++$i) {
704-
if ($this->workSheetCollection[$i]->getTitle() === trim($worksheetName, "'")) {
704+
if (strcasecmp($this->workSheetCollection[$i]->getTitle(), trim($worksheetName, "'")) === 0) {
705705
return $this->workSheetCollection[$i];
706706
}
707707
}

tests/PhpSpreadsheetTests/SpreadsheetTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,22 @@ public function testAddSheetDuplicateTitle(): void
7070
{
7171
$spreadsheet = $this->getSpreadsheet();
7272
$this->expectException(Exception::class);
73+
$this->expectExceptionMessage("Workbook already contains a worksheet named 'someSheet2'. Rename this worksheet first.");
7374
$sheet = new Worksheet();
7475
$sheet->setTitle('someSheet2');
7576
$spreadsheet->addSheet($sheet);
7677
}
7778

79+
public function testAddSheetDuplicateTitleWithDifferentCase(): void
80+
{
81+
$spreadsheet = $this->getSpreadsheet();
82+
$this->expectException(Exception::class);
83+
$this->expectExceptionMessage("Workbook already contains a worksheet named 'SomeSheet2'. Rename this worksheet first.");
84+
$sheet = new Worksheet();
85+
$sheet->setTitle('SomeSheet2');
86+
$spreadsheet->addSheet($sheet);
87+
}
88+
7889
public function testAddSheetNoAdjustActive(): void
7990
{
8091
$spreadsheet = $this->getSpreadsheet();
@@ -101,6 +112,7 @@ public function testRemoveSheetIndexTooHigh(): void
101112
{
102113
$spreadsheet = $this->getSpreadsheet();
103114
$this->expectException(Exception::class);
115+
$this->expectExceptionMessage('You tried to remove a sheet by the out of bounds index: 4. The actual number of sheets is 3.');
104116
$spreadsheet->removeSheetByIndex(4);
105117
}
106118

@@ -126,13 +138,15 @@ public function testGetSheetIndexTooHigh(): void
126138
{
127139
$spreadsheet = $this->getSpreadsheet();
128140
$this->expectException(Exception::class);
141+
$this->expectExceptionMessage('Your requested sheet index: 4 is out of bounds. The actual number of sheets is 3.');
129142
$spreadsheet->getSheet(4);
130143
}
131144

132145
public function testGetIndexNonExistent(): void
133146
{
134147
$spreadsheet = $this->getSpreadsheet();
135148
$this->expectException(Exception::class);
149+
$this->expectExceptionMessage('Sheet does not exist.');
136150
$sheet = new Worksheet();
137151
$sheet->setTitle('someSheet4');
138152
$spreadsheet->getIndex($sheet);
@@ -178,13 +192,15 @@ public function testSetActiveSheetIndexTooHigh(): void
178192
{
179193
$spreadsheet = $this->getSpreadsheet();
180194
$this->expectException(Exception::class);
195+
$this->expectExceptionMessage('You tried to set a sheet active by the out of bounds index: 4. The actual number of sheets is 3.');
181196
$spreadsheet->setActiveSheetIndex(4);
182197
}
183198

184199
public function testSetActiveSheetNoSuchName(): void
185200
{
186201
$spreadsheet = $this->getSpreadsheet();
187202
$this->expectException(Exception::class);
203+
$this->expectExceptionMessage('Workbook does not contain sheet:unknown');
188204
$spreadsheet->setActiveSheetIndexByName('unknown');
189205
}
190206

@@ -213,6 +229,7 @@ public function testAddExternal(): void
213229
public function testAddExternalDuplicateName(): void
214230
{
215231
$this->expectException(Exception::class);
232+
$this->expectExceptionMessage("Workbook already contains a worksheet named 'someSheet1'. Rename the external sheet first.");
216233
$spreadsheet = new Spreadsheet();
217234
$sheet = $spreadsheet->createSheet()->setTitle('someSheet1');
218235
$sheet->getCell('A1')->setValue(1);

0 commit comments

Comments
 (0)