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

fix: 导出 CSV 时特殊处理逗号等字符 #3091

Closed
wants to merge 2 commits into from

Conversation

Alexzjt
Copy link
Contributor

@Alexzjt Alexzjt commented Feb 6, 2025

👀 PR includes

✨ Feature

  • New feature

🎨 Enhance

  • Code style optimization
  • Refactoring
  • Change the UI
  • Improve the performance
  • Type optimization

🐛 Bugfix

🔧 Chore

  • Test case
  • Docs / demos update
  • CI / workflow
  • Release version
  • Other ()

📝 Description

导出CSV时,若数据本身带逗号,则用Excel打开时错列

🖼️ Screenshot

Before After
image image

🔗 Related issue link

🔍 Self-Check before the merge

  • Add or update relevant docs.
  • Add or update relevant demos.
  • Add or update test case.
  • Add or update relevant TypeScript definitions.

Copy link
Contributor

github-actions bot commented Feb 6, 2025

你好 @Alexzjt,非常感谢你的贡献.
为了让代码更健壮, 请根据 贡献指南 补充相应单元测试 (文档改动或其他特殊场景除外), 如果有 API 改动, 请修改 相应的文档.
同时请检查 linttest 相关 CI 是否通过, 如果失败, 请点击 [Details] 按钮查看, 并根据日志修复.

Hello, @Alexzjt, Thanks for your contribution. In order to make the code more robust, please add the corresponding unit tests, and update the docs if there are API changes.
At the same time, please check whether the CI related to lint and test passes. If the CI fails, please click [Details] button to check and repair according to the log.

@github-actions github-actions bot added pr(fix) bug fix next 2.0-next 版本的问题 labels Feb 6, 2025
Copy link

Walkthrough

This PR addresses issues with CSV export by implementing special handling for fields containing commas, quotes, newlines, or tabs. It introduces a function to escape these characters according to CSV standards, ensuring correct column alignment when opened in Excel. Additionally, test cases have been added to verify the functionality.

Changes

File Summary
packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts Added test case to verify CSV field escaping.
packages/s2-core/__tests__/unit/utils/export/method-spec.ts Added unit tests for escapeCSVField function.
packages/s2-core/src/utils/export/copy/base-data-cell-copy.ts Integrated escapeCSVField function to handle CSV field escaping.
packages/s2-core/src/utils/export/method.ts Implemented escapeCSVField function to escape special characters in CSV fields.

* 若字段中的双引号 → 转义为两个双引号 ""。
* @param field
*/
export const escapeCSVField = (field: SimpleData): SimpleData => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The escapeCSVField function is a critical addition to handle CSV field escaping. Ensure that this function is thoroughly tested and validated to prevent any data corruption during CSV export.

@@ -29,3 +30,50 @@ describe('method test', () => {
expect(trimTabSeparator(1 as unknown as string)).toBe(1);
});
});

type SimpleData = string | number | null | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要额外定义一次

* 若字段中的双引号 → 转义为两个双引号 ""。
* @param field
*/
export const escapeCSVField = (field: SimpleData): SimpleData => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议和 trimTabSeparator 统一下, trimTabSeparator 去掉,然后 escapeCSVField => escapeField

@@ -50,8 +56,14 @@ export abstract class BaseDataCellCopy {
dataMatrix: SimpleData[][],
separator: string,
): CopyablePlain {
let escapeDataMatrix: SimpleData[][] = dataMatrix;

if (separator === CSV_SEPARATOR) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑下是否去掉 convertString, 理论上刷选复制/全部复制/导出 可以走统一的转换

@Alexzjt Alexzjt closed this Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next 2.0-next 版本的问题 pr(fix) bug fix
Projects
None yet
2 participants