Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use case insentive comparison to get sheet name #3791

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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