}
}
- 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);
}
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) {
/**
* @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) {
/**
* @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;
}