fix autodetect

This commit is contained in:
r.mubarakzyanov 2026-03-29 21:47:23 +03:00
parent aa1c32b7ed
commit 0bc2ff7e9d

View File

@ -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;