diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php index bbd41f8..c2b5bb6 100644 --- a/public/userarea/mapping_template_xls_scheme2.php +++ b/public/userarea/mapping_template_xls_scheme2.php @@ -575,6 +575,10 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t while (headers.length > 0 && headers[headers.length - 1] === '') { headers.pop(); } + headers = headers.map((h, i) => { + h = h.replace(/[\r\n\t]+/g, ' ').trim(); + return h === '' ? `__empty_${i + 1}__` : h; + }); console.log("Intestazioni estratte (manual):", headers); availableXlsColumns = [...headers]; usedColumnsFromDB = []; @@ -707,8 +711,11 @@ $xlsHeaders = $template['xls_headers'] ? json_decode($template['xls_headers'], t while (headers.length > 0 && headers[headers.length - 1] === '') { headers.pop(); } - // Final clean: ensure no whitespace-only entries sneak through - headers = headers.map(h => h.replace(/[\r\n\t]+/g, ' ').trim()); + // Final clean + name empty columns to match __empty_N__ convention + headers = headers.map((h, i) => { + h = h.replace(/[\r\n\t]+/g, ' ').trim(); + return h === '' ? `__empty_${i + 1}__` : h; + }); console.log("Logical headers:", headers, `(${headers.length} columns from ${rawRow.length} physical)`); availableXlsColumns = [...headers]; usedColumnsFromDB = []; diff --git a/public/userarea/process_import_xls2.php b/public/userarea/process_import_xls2.php index 1b9c91c..afd0cf0 100644 --- a/public/userarea/process_import_xls2.php +++ b/public/userarea/process_import_xls2.php @@ -74,6 +74,16 @@ try { $startRow = max(1, $header_row); $startColumn = max(1, $start_column); + // Advance startColumn to first non-empty cell in header row (match JS behavior) + for ($sc = $startColumn; $sc <= $highestColumnIndex; $sc++) { + $cl = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($sc); + $cv = trim((string)($worksheet->getCell($cl . $header_row)->getCalculatedValue() ?? '')); + if ($cv !== '') { + $startColumn = $sc; + break; + } + } + // Debug dei parametri error_log("Processing - template_id: $template_id, startRow: $startRow, startColumn: $startColumn, highestRow: $highestRow, highestColumn: $highestColumn, highestColumnIndex: $highestColumnIndex"); @@ -127,6 +137,9 @@ try { $headerRowData[] = ($cellValue !== '') ? $cellValue : '__empty_' . $logicalNum . '__'; } + error_log("Logical headers: " . json_encode($headerRowData)); + error_log("Logical cols (physical indices): " . json_encode($logicalCols)); + // Find which logical columns have real headers $headerFilledIndices = []; foreach ($headerRowData as $idx => $hVal) {