Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 2db1947

Browse files
Replace export-to-csv with json2csv
1 parent 7c60dd4 commit 2db1947

File tree

3 files changed

+82
-38
lines changed

3 files changed

+82
-38
lines changed

package-lock.json

Lines changed: 66 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"chart.js": "^3.7.1",
1616
"chartjs-adapter-moment": "^1.0.0",
1717
"chartjs-plugin-datalabels": "^2.0.0",
18-
"export-to-csv": "^0.2.1",
19-
"export-to-csv-fix-source-map": "^0.2.1",
18+
"json2csv": "^5.0.7",
2019
"moment": "^2.29.1",
2120
"prop-types": "^15.7.2",
2221
"rc-table": "^7.17.1",

src/views/Task.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
1515
import SotaChart from '../components/SotaChart'
1616
import { FacebookShareButton, TwitterShareButton, FacebookIcon, TwitterIcon } from 'react-share'
1717
import moment from 'moment'
18-
import { ExportToCsv } from 'export-to-csv'
18+
import { parse } from 'json2csv'
1919

2020
library.add(faEdit)
2121

@@ -239,19 +239,20 @@ class Task extends React.Component {
239239
}
240240

241241
handleCsvExport () {
242-
const options = {
243-
filename: this.state.item.name,
244-
fieldSeparator: ',',
245-
quoteStrings: '"',
246-
decimalSeparator: '.',
247-
showLabels: true,
248-
showTitle: false,
249-
useTextFile: false,
250-
useBom: true,
251-
useKeysAsHeaders: true
252-
}
253-
const csvExporter = new ExportToCsv(options)
254-
csvExporter.generateCsv(this.state.resultsJson)
242+
const fields = Object.keys(this.state.resultsJson[0])
243+
const opts = { fields }
244+
const csv = parse(this.state.resultsJson, opts)
245+
246+
const element = document.createElement('a')
247+
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv))
248+
element.setAttribute('download', this.state.item.name)
249+
250+
element.style.display = 'none'
251+
document.body.appendChild(element)
252+
253+
element.click()
254+
255+
document.body.removeChild(element)
255256
}
256257

257258
render () {

0 commit comments

Comments
 (0)