From 65e857609a1b2ea908471e811262f195f85917c0 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Tue, 15 Mar 2022 12:03:16 +0000 Subject: [PATCH] fix button action export for csv --- packages/client/src/utils/buttonActions.js | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 25659cea98..17121f3ee6 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -241,6 +241,24 @@ const s3UploadHandler = async action => { } } +const convertToCsv = (headers, rows) => { + let csv = headers.map(key => `"${key}"`).join(",") + + for (let row of rows) { + csv = `${csv}\n${headers + .map(header => { + let val = row[header] + val = + typeof val === "object" + ? `"${JSON.stringify(val).replace(/"/g, "'")}"` + : `"${val}"` + return val.trim() + }) + .join(",")}` + } + return csv +} + const exportDataHandler = async action => { let selection = rowSelectionStore.actions.getSelection( action.parameters.tableComponentId @@ -252,7 +270,14 @@ const exportDataHandler = async action => { rows: selection.selectedRows, }) - download(JSON.stringify(data), `export.${action.parameters.type}`) + let dataToExport + const headers = Object.keys(data[0]) + if (action.parameters.type === "csv") { + dataToExport = convertToCsv(headers, data) + } else { + dataToExport = JSON.stringify(data) + } + download(dataToExport, `export.${action.parameters.type}`) } catch (error) { notificationStore.actions.error("There was an error exporting the data") }