-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIM-1947] Mim 1947 table export refactor (#3073)
* Use newest version of SheetJS to replace tableExport.es6 Add feature toggling for table export utils MIM-1947 * Convert string to numbers for cell value MIM-1974 * Add .xlsx for sheets file name for correct file format MIM-1947 * Add SheetJS package tarball to our vendors and npm install that instead for security measures etc * Add conditional for old tableExport code to prevent double downloads when useNewTableExport feature toggle is on * Add option to download table as csv and use table caption instead of display name for file name * Minor code refactoring; use flatMap instead * Destructure props and remove unused props * Typo * Minor code refactoring; if-guards for some conditional (wip) * Fix file name for tables without caption content More minor refactoring
- Loading branch information
1 parent
01c613c
commit 3a83cdf
Showing
9 changed files
with
134 additions
and
201 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as XLSX from 'xlsx' | ||
import { Title } from '/lib/types/xmlParser' | ||
|
||
interface TableExport { | ||
tableElement: HTMLTableElement | ||
fileName?: Title | 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' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.