From a9129f426556cf81b49a7c5f942f629b0a13079c Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Sun, 22 Sep 2024 18:34:32 +0400 Subject: [PATCH] fixed reading empty rows from excel and fixed date formats --- .../userarea/importify/import_auto_script.php | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/public/userarea/importify/import_auto_script.php b/public/userarea/importify/import_auto_script.php index 68e44a7..f8b016b 100644 --- a/public/userarea/importify/import_auto_script.php +++ b/public/userarea/importify/import_auto_script.php @@ -40,9 +40,9 @@ if(isset($_FILES['f_csv'])) { $arr_associate = $arr_associate_data->Results; if(count($arr_associate) > 0) { //check define columns - $spreadsheet = IOFactory::load($file, IReader::READ_DATA_ONLY); + $spreadsheet = IOFactory::load($file); $worksheet = $spreadsheet->getActiveSheet(); - $arr_info = $worksheet->toArray(); + $arr_info = $worksheet->toArray(); if(count($arr_info) > 1) { //check excel rows $arr_excel_columns = $arr_info[0]; $arr_need_columns = array(); @@ -50,6 +50,29 @@ if(isset($_FILES['f_csv'])) { array_push($arr_need_columns, "Report no."); array_push($arr_need_columns, "Part No."); + // remove empty rows + $arr_info = array_filter($arr_info, function($row) { + return !empty(array_filter($row)); + }); + + // find date date in $arr_info formatted as m/d/yyyy and convert to yyyy-mm-dd + $rowCount = count($arr_info); + for ($i = 0; $i < $rowCount; $i++) { + $colCount = count($arr_info[$i]); + for ($j = 0; $j < $colCount; $j++) { + $value = $arr_info[$i][$j]; + + // Check if the value is a string that matches the m/d/yyyy format + if (is_string($value) && preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $value)) { + // Convert to DateTime and format to Y-m-d + $dateTime = DateTime::createFromFormat('m/d/Y', $value); + if ($dateTime) { + $arr_info[$i][$j] = $dateTime->format('Y-m-d'); // Store the formatted date + } + } + } + } + foreach($arr_associate as $item) { array_push($arr_need_columns, $item['headerfile']); }