SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Home Page:https://sheetjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected behavior in the `sheet_to_csv` utility function

rmitra-chub opened this issue · comments

There's a possible bug in the utils.sheet_to_csv function (https://docs.sheetjs.com/docs/api/utilities#delimiter-separated-output)
Function arguments:

  • sheet =>
    { A1: { v: "sample header 1", t: "s", w: "sample header 1", }, B1: { v: "sample header 2", t: "s", w: "sample header 2", }, A2: { v: "\"sample text 1", t: "s", w: "\"sample text 1", }, B2: { v: "sample text 2", t: "s", w: "sample text 2", }, "!ref": "A1:B2", }
  • options => { FS: '\t' }

Output: "sample header 1\tsample header 2\n"""sample text 1"\tsample text 2\n"
Expected Output: "sample header 1\tsample header 2\n"sample text 1\tsample text 2\n"

The output is duplicating the double quote if there is one inside one of the values (in this example, A2).

The correct escaping of " is "". It doesn't use the JS \ character.

For example:

Excel generates the following CSV:

noquote,"""startquote","mid""quote","endquote"""

If you save that to a file and try to open in Excel, it will have the correct data.