diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index fa8cbcca7a..bc1a103df6 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -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]; } } diff --git a/tests/PhpSpreadsheetTests/SpreadsheetTest.php b/tests/PhpSpreadsheetTests/SpreadsheetTest.php index fe4e986439..12fe4abcb1 100644 --- a/tests/PhpSpreadsheetTests/SpreadsheetTest.php +++ b/tests/PhpSpreadsheetTests/SpreadsheetTest.php @@ -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(); @@ -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); } @@ -126,6 +138,7 @@ 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); } @@ -133,6 +146,7 @@ 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); @@ -178,6 +192,7 @@ 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); } @@ -185,6 +200,7 @@ public function testSetActiveSheetNoSuchName(): void { $spreadsheet = $this->getSpreadsheet(); $this->expectException(Exception::class); + $this->expectExceptionMessage('Workbook does not contain sheet:unknown'); $spreadsheet->setActiveSheetIndexByName('unknown'); } @@ -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);