diff --git a/public/userarea/process_import_xls2.php b/public/userarea/process_import_xls2.php index 4f29d15..65ef60b 100644 --- a/public/userarea/process_import_xls2.php +++ b/public/userarea/process_import_xls2.php @@ -86,25 +86,31 @@ try { for ($scanRow = 1; $scanRow <= $scanLimit; $scanRow++) { $score = 0; - $firstMatchCol = $highestColumnIndex + 1; for ($col = 1; $col <= $highestColumnIndex; $col++) { $colLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col); $cellVal = $worksheet->getCell($colLetter . $scanRow)->getCalculatedValue(); $cellVal = mb_strtolower(trim((string)$cellVal)); if ($cellVal !== '' && in_array($cellVal, $templateLabels, true)) { $score++; - if ($col < $firstMatchCol) $firstMatchCol = $col; } } if ($score > $bestScore) { $bestScore = $score; $bestRow = $scanRow; - $bestCol = $firstMatchCol; } } $header_row = $bestRow; - $start_column = $bestCol; + + // Determine start_column: first non-empty cell in the detected header row + for ($col = 1; $col <= $highestColumnIndex; $col++) { + $colLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col); + $cellVal = $worksheet->getCell($colLetter . $header_row)->getCalculatedValue(); + if (trim((string)$cellVal) !== '') { + $start_column = $col; + break; + } + } error_log("Auto-detected header row: $header_row, start column: $start_column (score: $bestScore/" . count($templateLabels) . ")"); $response['auto_header_row'] = $header_row; $response['auto_header_score'] = $bestScore;