From 9e40bdf2b9baf78bd020e40dbca118860d931868 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 9 Jan 2023 17:51:53 +0100 Subject: [PATCH] wait #5672 @2 --- src/ExcelToArray.php | 97 ++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 53 deletions(-) diff --git a/src/ExcelToArray.php b/src/ExcelToArray.php index 55c218a..42ad31d 100644 --- a/src/ExcelToArray.php +++ b/src/ExcelToArray.php @@ -64,14 +64,37 @@ class ExcelToArray } } - public static function excelToArrayFirstSheet($file, $force = false) + /** + * @throws Exception + */ + public static function excelToArrayFirstSheet($file, $sheet = null, $force = false) { - $res = self::excelToArrayRaw($file, $force); - $res = array_shift($res); - return $res; + return self::_excelSheetToArray($file, $sheet, false, $force); } - public static function excelToArrayRaw($file, $force = false) + /** + * @throws Exception + */ + protected static function _excelSheetToArray($file, $sheet = null, $assoc = false, $force = false) + { + $res = self::_excelToArray($file, $assoc, $force); + $e = explode('#', $file); + if (count($e) >= 2 && !$sheet) { + $sheet = $e[1]; + } + if (!$sheet) { + return array_shift($res); + } + if (!isset($res[$sheet])) { + throw new Exception('Sheet ' . $sheet . ' not found'); + } + return $res[$sheet]; + } + + /** + * @throws Exception + */ + public static function excelToArrayRaw($file, $sheet = null, $force = false) { return self::_excelToArray($file, false, $force); } @@ -168,50 +191,21 @@ class ExcelToArray return $res; } - public static function excelToArrayKeyVal($file, $force = false) + public static function excelToArrayKeyVal($file, $sheet = null, $force = false) { - $worksheets = self::_excelToArray($file, false, $force); - $res = []; - foreach ($worksheets as $worksheet) { - foreach ($worksheet as $line) { - $res[trim($line[0])] = trim($line[1]); - } - break; + $worksheet = self::_excelSheetToArray($file, $sheet, false, $force); + foreach ($worksheet as $line) { + $res[trim($line[0])] = trim($line[1]); } return $res; } /** - * @param $file - * @param $force - * @return \PhpOffice\PhpSpreadsheet\Spreadsheet * @throws Exception */ - protected static function _excelToArrayKeyVarsCommon($file, $force = false) + public static function excelToArrayIndexKeyVars($file, $sheet = null, $force = false) { - $e = explode('#', $file); - $file = $e[0]; - - $worksheets = self::_excelToArray($file, false, $force); - - if (isset($e[1])) { - $sheetName = trim($e[1]); - } - - if (isset($sheetName, $worksheets[$sheetName])) { - $worksheet = $worksheets[$sheetName]; - } else { - $worksheet = array_shift($worksheets); - } - return $worksheet; - } - - /** - * @throws Exception - */ - public static function excelToArrayIndexKeyVars($file, $force = false) - { - $worksheet = self::_excelToArrayKeyVarsCommon($file, $force); + $worksheet = self::_excelSheetToArray($file, $sheet, false, $force); $res = []; foreach ($worksheet as $i => $line) { if ($i === 0) { @@ -237,9 +231,9 @@ class ExcelToArray /** * @throws Exception */ - public static function excelToArrayKeyVars($file, $skipEmptyId = false, $force = false) + public static function excelToArrayKeyVars($file, $sheet = null, $skipEmptyId = false, $force = false) { - $worksheet = self::_excelToArrayKeyVarsCommon($file, $force); + $worksheet = self::_excelSheetToArray($file, $sheet, false, $force); $res = []; foreach ($worksheet as $i => $line) { if ($i === 0) { @@ -278,20 +272,17 @@ class ExcelToArray /** * @throws Exception */ - public static function excelToArrayKeyValMulti($file, $force = false) + public static function excelToArrayKeyValMulti($file, $sheet = null, $force = false) { - $worksheets = self::_excelToArray($file, false, $force); + $worksheet = self::_excelSheetToArray($file, $sheet, false, $force); $res = []; - foreach ($worksheets as $worksheet) { - foreach ($worksheet as $line) { - $multi = []; - $n = count($line); - for ($i = 1; $i < $n; $i++) { - $multi[] = trim($line[$i]); - } - $res[trim($line[0])] = $multi; + foreach ($worksheet as $line) { + $multi = []; + $n = count($line); + for ($i = 1; $i < $n; $i++) { + $multi[] = trim($line[$i]); } - break; + $res[trim($line[0])] = $multi; } return $res; } -- 2.39.5