Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIM-1947] Mim 1947 table export refactor #3073

Merged
merged 12 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 7 additions & 145 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"webpack": "~5.96.1",
"webpack-bundle-analyzer": "~4.10.2",
"webpack-cli": "~5.1.4",
"xlsx": "~0.18.5"
"xlsx": "file:src/main/resources/lib/vendor/xlsx-0.20.3.tgz"
},
"browserslist": [
"last 2 version",
Expand Down
43 changes: 43 additions & 0 deletions src/main/resources/lib/ssb/utils/tableExportUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as XLSX from 'xlsx'

interface TableExport {
tableElement: HTMLTableElement
fileName?: string
}

const sheetName = 'Sheet1'

const createTableWorkbook = (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 = createTableWorkbook(tableElement)

const worksheet = workbook.Sheets[sheetName]
Object.keys(worksheet).flatMap((cell) => {
// Check if SheetJS worksheet cell object has a value and that the cell isn't a SheetJS special property/metadata
if (cell && cell[0] !== '!') {
const cellValue = worksheet[cell].v.replace(/\s/g, '').replace(/,/g, '.')
if (cellValue !== '') {
if (!isNaN(cellValue)) {
worksheet[cell].v = parseFloat(cellValue)
worksheet[cell].t = 'n'
}
}
}
})

XLSX.writeFile(workbook, sheetFileName)
}

export const exportTableToCSV = ({ tableElement, fileName }: TableExport): void => {
const sheetFileName = fileName ? `${fileName}.csv` : 'tabell.csv'
const workbook = createTableWorkbook(tableElement)

XLSX.writeFile(workbook, sheetFileName, { bookType: 'csv', type: 'string' })
}
3 changes: 1 addition & 2 deletions src/main/resources/lib/types/partTypes/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ export interface TableProps {
downloadTableLabel: string
downloadTableTitle: object
downloadTableOptions: DropdownItems
displayName: string
table: Partial<TableView> & {
language: string | undefined
}
tableDraft: Partial<TableView>
standardSymbol: TableStandardSymbolLink | undefined
sources: SourceList
sourceLabel: string
iconUrl: string
showPreviewDraft: boolean
paramShowDraft: boolean
draftExist: boolean | undefined
Expand All @@ -24,6 +22,7 @@ export interface TableProps {
statBankWebUrl: string
hiddenTitle: string | undefined
checkIsOverflowing?: boolean
useNewTableExport: boolean
}

export interface TableStandardSymbolLink {
Expand Down
Binary file added src/main/resources/lib/vendor/xlsx-0.20.3.tgz
Binary file not shown.
4 changes: 4 additions & 0 deletions src/main/resources/main.es6
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ try {
feature: 'show-popup-survey',
enabled: false,
},
{
feature: 'new-table-export',
enabled: false,
},
],
},
])
Expand Down
Loading
Loading