-
Notifications
You must be signed in to change notification settings - Fork 201
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
fix: 导出 CSV 时特殊处理逗号等字符 #3091
Conversation
你好 @Alexzjt,非常感谢你的贡献. 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. |
WalkthroughThis 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
|
* 若字段中的双引号 → 转义为两个双引号 ""。 | ||
* @param field | ||
*/ | ||
export const escapeCSVField = (field: SimpleData): SimpleData => { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑下是否去掉 convertString
, 理论上刷选复制/全部复制/导出 可以走统一的转换
👀 PR includes
✨ Feature
🎨 Enhance
🐛 Bugfix
🔧 Chore
📝 Description
导出CSV时,若数据本身带逗号,则用Excel打开时错列
🖼️ Screenshot
🔗 Related issue link
🔍 Self-Check before the merge