]> _ Git - cubist_excel.git/commitdiff
wait #5672 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 9 Jan 2023 16:51:53 +0000 (17:51 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 9 Jan 2023 16:51:53 +0000 (17:51 +0100)
src/ExcelToArray.php

index 55c218ae5696ff0803868b68738da5b4804f545e..42ad31d8897887997df497c2bbc27181eb39e281 100644 (file)
@@ -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;
     }