diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index 09022bbc63..fc85757a37 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -608,7 +608,7 @@ public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null, bool $re */ public function removeSheetByIndex(int $sheetIndex): void { - $numSheets = count($this->workSheetCollection); + $numSheets = $this->getSheetCount(); if ($sheetIndex > $numSheets - 1) { throw new Exception( "You tried to remove a sheet by the out of bounds index: {$sheetIndex}. The actual number of sheets is {$numSheets}." @@ -660,9 +660,10 @@ public function getAllSheets(): array */ public function getSheetByName(string $worksheetName): ?Worksheet { - $worksheetCount = count($this->workSheetCollection); - for ($i = 0; $i < $worksheetCount; ++$i) { - if (strcasecmp($this->workSheetCollection[$i]->getTitle(), trim($worksheetName, "'")) === 0) { + $trimWorksheetName = trim($worksheetName, "'"); + + for ($i = 0; $i < $this->getSheetCount(); ++$i) { + if (strcasecmp($this->workSheetCollection[$i]->getTitle(), $trimWorksheetName) === 0) { return $this->workSheetCollection[$i]; } } @@ -790,8 +791,8 @@ public function setActiveSheetIndexByName(string $worksheetName): Worksheet public function getSheetNames(): array { $returnValue = []; - $worksheetCount = $this->getSheetCount(); - for ($i = 0; $i < $worksheetCount; ++$i) { + + for ($i = 0; $i < $this->getSheetCount(); ++$i) { $returnValue[] = $this->getSheet($i)->getTitle(); }