From c4074deec5e34d5283de8f556957479af342d824 Mon Sep 17 00:00:00 2001 From: johnnadeluy Date: Wed, 18 Dec 2024 10:15:28 +0100 Subject: [PATCH] Add option to download table as csv and use table caption instead of display name for file name --- .../lib/ssb/utils/tableExportUtils.ts | 20 +++++++++++++++---- src/main/resources/react4xp/table/Table.tsx | 11 ++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/resources/lib/ssb/utils/tableExportUtils.ts b/src/main/resources/lib/ssb/utils/tableExportUtils.ts index 7809a43f5..a593e049d 100644 --- a/src/main/resources/lib/ssb/utils/tableExportUtils.ts +++ b/src/main/resources/lib/ssb/utils/tableExportUtils.ts @@ -5,13 +5,18 @@ interface TableExport { fileName?: string } -export const exportTableToExcel = ({ tableElement, fileName }: TableExport): void => { - const sheetFileName = fileName ? `${fileName}.xlsx` : 'tabell.xlsx' - const sheetName = 'Sheet1' - const workbook = XLSX.utils.table_to_book(tableElement, { +const sheetName = 'Sheet1' + +const createTableWorbook = (tableElement: TableExport['tableElement']) => { + return XLSX.utils.table_to_book(tableElement, { sheet: sheetName, raw: true, }) +} + +export const exportTableToExcel = ({ tableElement, fileName }: TableExport): void => { + const sheetFileName = fileName ? `${fileName}.xlsx` : 'tabell.xlsx' + const workbook = createTableWorbook(tableElement) const worksheet = workbook.Sheets[sheetName] for (const cell in worksheet) { @@ -29,3 +34,10 @@ export const exportTableToExcel = ({ tableElement, fileName }: TableExport): voi XLSX.writeFile(workbook, sheetFileName) } + +export const exportTableToCSV = ({ tableElement, fileName }: TableExport): void => { + const sheetFileName = fileName ? `${fileName}.csv` : 'tabell.csv' + const workbook = createTableWorbook(tableElement) + + XLSX.writeFile(workbook, sheetFileName, { bookType: 'csv', type: 'string' }) +} diff --git a/src/main/resources/react4xp/table/Table.tsx b/src/main/resources/react4xp/table/Table.tsx index 1b75a54f4..d57f1ab33 100644 --- a/src/main/resources/react4xp/table/Table.tsx +++ b/src/main/resources/react4xp/table/Table.tsx @@ -15,7 +15,7 @@ import { NumericFormat } from 'react-number-format' import { Alert } from 'react-bootstrap' import { type TableProps } from '/lib/types/partTypes/table' import { PreliminaryData, type TableCellUniform } from '/lib/types/xmlParser' -import { exportTableToExcel } from '/lib/ssb/utils/tableExportUtils' +import { exportTableToExcel, exportTableToCSV } from '/lib/ssb/utils/tableExportUtils' declare global { interface Window { @@ -24,7 +24,7 @@ declare global { } function Table(props: TableProps) { - const { useNewTableExport, displayName, table } = props + const { useNewTableExport, table } = props const [currentTable, setCurrentTable] = useState(props.paramShowDraft && props.draftExist ? props.tableDraft : table) const [fetchUnPublished, setFetchUnPublished] = useState(props.paramShowDraft) @@ -79,7 +79,7 @@ function Table(props: TableProps) { } function downloadTableAsCSV() { - if (window.downloadTableFile) { + if (window.downloadTableFile && !useNewTableExport) { window.downloadTableFile(tableWrapperRef.current, { type: 'csv', fileName: 'tabell', @@ -88,6 +88,9 @@ function Table(props: TableProps) { tfootSelector: '', }) } + if (tableRef?.current && useNewTableExport) { + exportTableToCSV({ tableElement: tableRef.current, fileName: table?.caption?.content }) + } } function downloadTableAsExcel() { @@ -108,7 +111,7 @@ function Table(props: TableProps) { }) } if (tableRef?.current && useNewTableExport) { - exportTableToExcel({ tableElement: tableRef.current, fileName: displayName }) + exportTableToExcel({ tableElement: tableRef.current, fileName: table?.caption?.content }) } }