将 CSV 解析为 JS 应用程序、创建并让用户填充新列的最佳方法?

发布于 2025-01-12 23:30:44 字数 315 浏览 0 评论 0原文

我需要将 CSV 文件解析为 json (或数组,仍在尝试搜索最佳方法!),

  • 在容器中向用户显示数据,
  • 在同一 csv 上创建一个新列用于标签,并让用户填充它根据单击应用程序上的一些按钮,
  • 并将新架构保存到本地计算机。

我应该如何解决这个问题?

我一直在阅读 data-grid、handsontable 和 papaParser,但我仍然很困惑。我仍然是 javascript 的初学者。如果您能给我提供有关如何进行此操作的最佳方式的总体情况,那将是一个很大的帮助,我相信我可以自己解决技术细节问题。提前致谢!

I need to parse a CSV file to json (or array, still trying to search for the best method!),

  • show the data to the user in a container,
  • create a new column on that same csv for LABELLING, and letting the user populate it according to clicking some buttons on the app,
  • and save the new schema to local computer.

How should I approach this problem?

I've been reading into data-grid, handsontable and papaParser, but I'm still very confused. I'm still kind of a beginner at javascript. It would be a big help if you could give me the big picture on the best way in how proceed with this, I'm sure I could troubleshoot the technical details on my own. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-01-19 23:30:44

Handsontable 不允许您使用 CSV 作为数据源,因此您需要首先使用另一个库将其解析为 JSON。使用 JSON 本身需要一些额外的设置 - 特别是当您有嵌套的数据结构时。然后您可能需要使用columns选项来指定给定列的对象。

在该网格的能力范围内,您可以更改列,这些列将反映在 exportToFile 插件生成的新 CSV 文件中。

exportToFile 插件允许您设置一些值作为分隔符或显示/隐藏标题的能力,以保持与输入 CSV 结构的一致性。

例子

const exportPlugin = hot.getPlugin('exportFile');

button.addEventListener('click', () => {
  exportPlugin.downloadFile('csv', {
    bom: false,
    columnDelimiter: ',',
    columnHeaders: false,
    exportHiddenColumns: true,
    exportHiddenRows: true,
    fileExtension: 'csv',
    filename: 'Handsontable-CSV-file_[YYYY]-[MM]-[DD]',
    mimeType: 'text/csv',
    rowDelimiter: '\r\n',
    rowHeaders: true
  });
});

Handsontable does not allow you to use CSV as the datasource so you would need to parse it to JSON first using another library. Using JSON itself requires some additional setup - especially when you have a nested structure of data. Then you might need to use the columns option the specify objects for a given column.

Within the abilities of that grid, you can alter columns and those would be reflected in a new CSV file generated by the exportToFile plugin.

The exportToFile plugin allows you to set some values as the delimiter or ability to show/hide headers to keep the consistency with your input CSV structure.

Example

const exportPlugin = hot.getPlugin('exportFile');

button.addEventListener('click', () => {
  exportPlugin.downloadFile('csv', {
    bom: false,
    columnDelimiter: ',',
    columnHeaders: false,
    exportHiddenColumns: true,
    exportHiddenRows: true,
    fileExtension: 'csv',
    filename: 'Handsontable-CSV-file_[YYYY]-[MM]-[DD]',
    mimeType: 'text/csv',
    rowDelimiter: '\r\n',
    rowHeaders: true
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文