diff --git a/services/dataProcessor.ts b/services/dataProcessor.ts index c8e3da5..a42bbde 100644 --- a/services/dataProcessor.ts +++ b/services/dataProcessor.ts @@ -236,7 +236,7 @@ const normalizeMonth = (rawMonth: string): string => { }; // Robust CSV Column Value Extractor -const getColumnValue = (row: any, aliases: string[]): string => { +const getColumnValue = (row: any, aliases: (string | RegExp)[]): string => { const rowKeys = Object.keys(row); const normalizedRowKeys: Record = {}; @@ -248,14 +248,24 @@ const getColumnValue = (row: any, aliases: string[]): string => { }); for (const alias of aliases) { - const lookup = alias.toLowerCase().replace(/[^a-z0-9]/g, ''); - if (normalizedRowKeys[lookup]) { - const actualKey = normalizedRowKeys[lookup]; - const val = row[actualKey]; - if (val !== undefined && val !== null) { - const strVal = String(val).trim(); - if (strVal.length > 0) { - return strVal; + if (alias instanceof RegExp) { + // Find the first original key that matches the regex + const matchedKey = rowKeys.find(k => alias.test(k)); + if (matchedKey) { + const val = row[matchedKey]; + if (val !== undefined && val !== null) { + const strVal = String(val).trim(); + if (strVal.length > 0) return strVal; + } + } + } else { + const lookup = alias.toLowerCase().replace(/[^a-z0-9]/g, ''); + if (normalizedRowKeys[lookup]) { + const actualKey = normalizedRowKeys[lookup]; + const val = row[actualKey]; + if (val !== undefined && val !== null) { + const strVal = String(val).trim(); + if (strVal.length > 0) return strVal; } } } @@ -436,15 +446,15 @@ export const processAdsCSV = (file: File): Promise => { const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']); const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']); const asin = getColumnValue(row, ['asin']); - const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', 'ad spend']); - const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics']); - const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp']); - const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic']); - const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate']); - const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS']); - const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders']); - const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units']); - const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)']); + const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', /ad\s*spend/i, /cost/i]); + const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics', /click/i]); + const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp', /impression/i]); + const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic', /cost.*per.*click/i, /cpc/i]); + const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate', /click.*through.*rate/i, /ctr/i]); + const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS', /cost.*of.*sales/i, /acos/i]); + const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders', /order/i, /conversion/i]); + const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units', /unit/i, /einheit/i, /unidad/i]); + const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)', /sale/i, /umsatz/i, /ventas/i]); if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue; @@ -510,15 +520,15 @@ export const processAdsExcel = async (fileOrBuffer: File | ArrayBuffer): Promise const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']); const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']); const asin = getColumnValue(row, ['asin']); - const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', 'ad spend']); - const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics']); - const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp']); - const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic']); - const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate']); - const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS']); - const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders']); - const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units']); - const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)']); + const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', /ad\s*spend/i, /cost/i]); + const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics', /click/i]); + const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp', /impression/i]); + const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic', /cost.*per.*click/i, /cpc/i]); + const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate', /click.*through.*rate/i, /ctr/i]); + const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS', /cost.*of.*sales/i, /acos/i]); + const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders', /order/i, /conversion/i]); + const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units', /unit/i, /einheit/i, /unidad/i]); + const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)', /sale/i, /umsatz/i, /ventas/i]); // Skip if missing essential data if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue;