Skip to content

Commit

Permalink
Use case insentive comparison to get sheet name
Browse files Browse the repository at this point in the history
  • Loading branch information
kpn13 committed Nov 14, 2023
1 parent f9eb35d commit 2329e90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public function getSheetByName($worksheetName)
{
$worksheetCount = count($this->workSheetCollection);
for ($i = 0; $i < $worksheetCount; ++$i) {
if ($this->workSheetCollection[$i]->getTitle() === trim($worksheetName, "'")) {
if (strcasecmp($this->workSheetCollection[$i]->getTitle(), trim($worksheetName, "'")) === 0) {
return $this->workSheetCollection[$i];
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/PhpSpreadsheetTests/SpreadsheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,22 @@ public function testAddSheetDuplicateTitle(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage("Workbook already contains a worksheet named 'someSheet2'. Rename this worksheet first.");
$sheet = new Worksheet();
$sheet->setTitle('someSheet2');
$spreadsheet->addSheet($sheet);
}

public function testAddSheetDuplicateTitleWithDifferentCase(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage("Workbook already contains a worksheet named 'SomeSheet2'. Rename this worksheet first.");
$sheet = new Worksheet();
$sheet->setTitle('SomeSheet2');
$spreadsheet->addSheet($sheet);
}

public function testAddSheetNoAdjustActive(): void
{
$spreadsheet = $this->getSpreadsheet();
Expand All @@ -101,6 +112,7 @@ public function testRemoveSheetIndexTooHigh(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('You tried to remove a sheet by the out of bounds index: 4. The actual number of sheets is 3.');
$spreadsheet->removeSheetByIndex(4);
}

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

public function testGetIndexNonExistent(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('Sheet does not exist.');
$sheet = new Worksheet();
$sheet->setTitle('someSheet4');
$spreadsheet->getIndex($sheet);
Expand Down Expand Up @@ -178,13 +192,15 @@ public function testSetActiveSheetIndexTooHigh(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('You tried to set a sheet active by the out of bounds index: 4. The actual number of sheets is 3.');
$spreadsheet->setActiveSheetIndex(4);
}

public function testSetActiveSheetNoSuchName(): void
{
$spreadsheet = $this->getSpreadsheet();
$this->expectException(Exception::class);
$this->expectExceptionMessage('Workbook does not contain sheet:unknown');
$spreadsheet->setActiveSheetIndexByName('unknown');
}

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

0 comments on commit 2329e90

Please sign in to comment.