From fdfd36cc04d502a7afcae5f5f54cdfd987889c9e Mon Sep 17 00:00:00 2001 From: David de Boer Date: Mon, 8 Sep 2014 17:16:31 +0200 Subject: [PATCH] Improve sheet index error handling Fix 'Undefined offset: -1' warning, thrown when worksheets could not be parsed and _activeSheetIndex equals -1. --- Classes/PHPExcel.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Classes/PHPExcel.php b/Classes/PHPExcel.php index e01581a..27aa77a 100644 --- a/Classes/PHPExcel.php +++ b/Classes/PHPExcel.php @@ -461,10 +461,12 @@ class PHPExcel * Get active sheet * * @return PHPExcel_Worksheet + * + * @throws PHPExcel_Exception */ public function getActiveSheet() { - return $this->_workSheetCollection[$this->_activeSheetIndex]; + return $this->getSheet($this->_activeSheetIndex); } /** @@ -570,16 +572,14 @@ class PHPExcel */ public function getSheet($pIndex = 0) { - - $numSheets = count($this->_workSheetCollection); - - if ($pIndex > $numSheets - 1) { + if (!isset($this->_workSheetCollection[$pIndex])) { + $numSheets = $this->getSheetCount(); throw new PHPExcel_Exception( - "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}." - ); - } else { - return $this->_workSheetCollection[$pIndex]; + "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}." + ); } + + return $this->_workSheetCollection[$pIndex]; } /** @@ -682,7 +682,7 @@ class PHPExcel */ public function setActiveSheetIndex($pIndex = 0) { - $numSheets = count($this->_workSheetCollection); + $numSheets = count($this->_workSheetCollection); if ($pIndex > $numSheets - 1) { throw new PHPExcel_Exception(