diff --git a/plugins/export-workbook/src/plugin.ts b/plugins/export-workbook/src/plugin.ts index f5ed3303..a8e3be71 100644 --- a/plugins/export-workbook/src/plugin.ts +++ b/plugins/export-workbook/src/plugin.ts @@ -66,6 +66,9 @@ export const exportRecords = async ( } : async (name: string) => name + const schemaKeys = new Set(sheet.config.fields.map((field) => field.key)) + const schemaFieldKeys = sheet.config.fields.map((field) => field.key) + try { let results = await processRecords( sheet.id, @@ -73,31 +76,47 @@ export const exportRecords = async ( const processedRecords = await Promise.all( records.map(async (record: Flatfile.RecordWithLinks) => { const { id: recordId, values: row } = record + + const formatCell = (cellValue?: Flatfile.CellValue) => { + if (!cellValue) { + return { + t: 's', + v: '', + c: [], + } as XLSX.CellObject + } + + const { value, messages } = cellValue + const cell: XLSX.CellObject = { + t: 's', + v: Array.isArray(value) ? value.join(', ') : value, + c: [], + } + if (options.excludeMessages) { + cell.c = [] + } else if (messages.length > 0) { + cell.c = messages.map((m) => ({ + a: 'Flatfile', + t: `[${m.type.toUpperCase()}]: ${m.message}`, + T: true, + })) + cell.c.hidden = true + } + + return cell + } + + const additionalKeys = Object.keys(row).filter( + (key) => !schemaKeys.has(key) + ) + + const allKeys = [...schemaFieldKeys, ...additionalKeys] + const rowEntries = await Promise.all( - Object.keys(row).map(async (colName: string) => { + allKeys.map(async (colName) => { if (options.excludeFields?.includes(colName)) { return null } - const formatCell = (cellValue: Flatfile.CellValue) => { - const { value, messages } = cellValue - const cell: XLSX.CellObject = { - t: 's', - v: Array.isArray(value) ? value.join(', ') : value, - c: [], - } - if (options.excludeMessages) { - cell.c = [] - } else if (messages.length > 0) { - cell.c = messages.map((m) => ({ - a: 'Flatfile', - t: `[${m.type.toUpperCase()}]: ${m.message}`, - T: true, - })) - cell.c.hidden = true - } - - return cell - } const transformedColName = await columnNameTransformer( colName,