fix: détecter ligne d'en-têtes dynamiquement (row 1 vide sur OUTILLAGE etc, row 2 = vrais headers)
This commit is contained in:
@@ -32,7 +32,7 @@ export function extractSpecificFields(headers: string[], row: string[]): Record<
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseTabRows(tab: string, headers: string[], rows: string[][]): PendingProduct[] {
|
export function parseTabRows(tab: string, headers: string[], rows: string[][], dataStartRow = 2): PendingProduct[] {
|
||||||
const idx = {
|
const idx = {
|
||||||
id: findColIndex(headers, 'ID'),
|
id: findColIndex(headers, 'ID'),
|
||||||
sku: findColIndex(headers, 'UGS'),
|
sku: findColIndex(headers, 'UGS'),
|
||||||
@@ -60,7 +60,7 @@ export function parseTabRows(tab: string, headers: string[], rows: string[][]):
|
|||||||
if (id !== '' || publie !== '1') return null
|
if (id !== '' || publie !== '1') return null
|
||||||
return {
|
return {
|
||||||
tab,
|
tab,
|
||||||
rowNumber: rowIdx + 2,
|
rowNumber: dataStartRow + rowIdx,
|
||||||
sku: get(row, idx.sku),
|
sku: get(row, idx.sku),
|
||||||
title: get(row, idx.nom),
|
title: get(row, idx.nom),
|
||||||
priceMarket: get(row, idx.prixMarche).replace(',', '.'),
|
priceMarket: get(row, idx.prixMarche).replace(',', '.'),
|
||||||
@@ -106,8 +106,15 @@ export async function fetchPendingProducts(
|
|||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
const rows = (data.values ?? []) as string[][]
|
const rows = (data.values ?? []) as string[][]
|
||||||
if (rows.length < 2) continue
|
if (rows.length < 2) continue
|
||||||
const [headers, ...dataRows] = rows
|
// La ligne d'en-têtes est la première ligne qui contient "ID" en col A
|
||||||
const pending = parseTabRows(tab, headers, dataRows).filter(
|
// (certains onglets ont une ligne de section en row 1, les vrais en-têtes sont en row 2)
|
||||||
|
const headerRowIdx = rows.findIndex(r => (r[0] ?? '').trim() === 'ID')
|
||||||
|
if (headerRowIdx === -1) continue // pas un onglet produit
|
||||||
|
const headers = rows[headerRowIdx]
|
||||||
|
const dataRows = rows.slice(headerRowIdx + 1)
|
||||||
|
// Ignorer les onglets sans colonne "Publié" (COLLECTIONS, LIVRAISONS, etc.)
|
||||||
|
if (findColIndex(headers, 'Publié') === -1) continue
|
||||||
|
const pending = parseTabRows(tab, headers, dataRows, headerRowIdx + 2).filter(
|
||||||
p => !alreadyCreatedRows.has(`${p.tab}:${p.rowNumber}`),
|
p => !alreadyCreatedRows.has(`${p.tab}:${p.rowNumber}`),
|
||||||
)
|
)
|
||||||
all.push(...pending)
|
all.push(...pending)
|
||||||
|
|||||||
Reference in New Issue
Block a user