From 299d2032bccd1838c8d3815a36f9f16069bfd7f5 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Thu, 5 Feb 2026 08:44:33 +0100 Subject: [PATCH] feat: Improve Buy Box header detection and add Spanish aliases --- services/dataProcessor.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/services/dataProcessor.ts b/services/dataProcessor.ts index 052aa9e..b09c0ff 100644 --- a/services/dataProcessor.ts +++ b/services/dataProcessor.ts @@ -2180,7 +2180,23 @@ export const processBuyBoxExcel = async (fileOrBuffer: File | ArrayBuffer): Prom const jsonData: any[][] = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); if (jsonData.length === 0) continue; - const headers: any[] = jsonData[0] || []; + // Find the header row (first row with ASIN or similar) + let headerRowIdx = 0; + let headers: any[] = jsonData[0] || []; + for (let r = 0; r < Math.min(jsonData.length, 10); r++) { + const rowData = jsonData[r]; + if (!rowData) continue; + const isHeader = rowData.some(cell => { + const s = String(cell || '').toUpperCase().trim(); + return s === 'ASIN' || s.includes('AMAZON ASIN') || s.includes('PRODUCT ID') || s.includes('SKU'); + }); + if (isHeader) { + headerRowIdx = r; + headers = rowData; + break; + } + } + const asinIdx = headers.findIndex(h => { const s = String(h || '').toUpperCase().trim(); return s === 'ASIN' || s.includes('AMAZON ASIN') || s.includes('IDENTIFIER') || s.includes('PRODUCT ID') || s.includes('SKU'); @@ -2190,7 +2206,7 @@ export const processBuyBoxExcel = async (fileOrBuffer: File | ArrayBuffer): Prom // Dynamically find the "Issue Type" or "Reason" column let reasonIdx = headers.findIndex(h => { const s = String(h || '').toUpperCase(); - return s.includes('ISSUE TYPE') || s.includes('REASON') || s.includes('BUY BOX STATUS') || s.includes('LBB REASON') || s.includes('ESTADO BB') || s.includes('COMENTARIO'); + return s.includes('ISSUE TYPE') || s.includes('REASON') || s.includes('BUY BOX STATUS') || s.includes('LBB REASON') || s.includes('ESTADO BB') || s.includes('COMENTARIO') || s.includes('MOTIVO') || s.includes('CAUSA'); }); // Fallback to previous hardcoded indices if header search fails @@ -2203,10 +2219,10 @@ export const processBuyBoxExcel = async (fileOrBuffer: File | ArrayBuffer): Prom else reasonIdx = 13; } - console.log(`[BuyBox] Sheet "${sheetName}" (Country: ${config.country}): ASIN Col=${finalAsinIdx} ("${headers[finalAsinIdx]}"), Reason Col=${reasonIdx} ("${headers[reasonIdx] || 'N/A'}")`); + console.log(`[BuyBox] Sheet "${sheetName}" (Country: ${config.country}): HeaderRow=${headerRowIdx}, ASIN Col=${finalAsinIdx} ("${headers[finalAsinIdx]}"), Reason Col=${reasonIdx} ("${headers[reasonIdx] || 'N/A'}")`); - // Skip header row (index 0) - for (let i = 1; i < jsonData.length; i++) { + // Skip header row and all rows above it + for (let i = headerRowIdx + 1; i < jsonData.length; i++) { const row = jsonData[i]; if (!row || row.length <= Math.max(finalAsinIdx, reasonIdx)) continue;