Skip to content

Commit

Permalink
Add option to download table as csv and use table caption instead of …
Browse files Browse the repository at this point in the history
…display name for file name
  • Loading branch information
johnnadeluy committed Dec 18, 2024
1 parent 16c74f8 commit c4074de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/main/resources/lib/ssb/utils/tableExportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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' })
}
11 changes: 7 additions & 4 deletions src/main/resources/react4xp/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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',
Expand All @@ -88,6 +88,9 @@ function Table(props: TableProps) {
tfootSelector: '',
})
}
if (tableRef?.current && useNewTableExport) {
exportTableToCSV({ tableElement: tableRef.current, fileName: table?.caption?.content })
}
}

function downloadTableAsExcel() {
Expand All @@ -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 })
}
}

Expand Down

0 comments on commit c4074de

Please sign in to comment.