Sheetjs: the method XLSX.utils.sheet_to_txt is writing a random Asian Language character

Created on 3 Mar 2019  路  4Comments  路  Source: SheetJS/sheetjs

XLSX.utils.sheet_to_txt

Using the above mention method it writes a random Asian Language character in the first row. It doesn't when converting to CSV or XLSX.

file

Most helpful comment

If the goal is to manipulate the result from JS or the DOM (either putting the result in an element or passing the text to other functions), pass the option type: string:

var result = XLSX.utils.sheet_to_txt(worksheet, { type: "string" });

Without it, if the codepage library is available (nodejs, standalone xlsx.full.min.js script), the result is converted to UTF-16LE and a BOM is prepended

All 4 comments

I got the same problem when using sheet_to_txt

I got similar issue (only lets say less critical then from Bandito11): loosing or changing Polish special characters, even adding characters to the first cell. I could not set UTF-8 to be applied.
It is peculiar that json parsing XLSX.utils.sheet_to_json works OK but for my case is much much less convenient (I will not explain here why)

See my case the same sample to compare (I am using XLSX to import translation form translation company):

  1. XLSX.utils.sheet_to_txt sample:
    每镁text_key pl en
    passwordReset.change-password Zmiana HasBa Change Password
    passwordReset.password HasBo Password
    passwordReset.confirmPassword Potwierdz HasBo Confirm Password
    passwordReset.confirm Zatwierdz Let's Go
    passwordReset.invalidEmail BBdny email Invalid email
    passwordReset.invalidLogIn BBdny email lub hasBo Invalid login or password
    faq.start.slogan Pozw贸l mi Pom贸c! Let me Help you!
    faq.start.headline Czsto Zadawane Pytania ... & Odpowiedzi. Frequently Asked Questions (FAQ) ... & Answers.
  1. XLSX.utils.sheet_to_json sample:
    [
    {
    text_key: 'passwordReset.change-password',
    pl: 'Zmiana Has艂a',
    en: 'Change Password'
    },
    { text_key: 'passwordReset.password', pl: 'Has艂o', en: 'Password' },
    {
    text_key: 'passwordReset.confirmPassword',
    pl: 'Potwierd藕 Has艂o',
    en: 'Confirm Password'
    },
    {
    text_key: 'passwordReset.confirm',
    pl: 'Zatwierd藕',
    en: "Let's Go"
    },
    {
    text_key: 'passwordReset.invalidEmail',
    pl: 'B艂臋dny email',
    en: 'Invalid email'
    },
    {
    text_key: 'passwordReset.invalidLogIn',
    pl: 'B艂臋dny email lub has艂o',
    en: 'Invalid login or password'
    },
    {
    text_key: 'faq.start.slogan',
    pl: 'Pozw贸l mi Pom贸c!',
    en: 'Let me Help you!'
    },
    {
    text_key: 'faq.start.headline',
    pl: 'Cz臋sto Zadawane Pytania ... & Odpowiedzi.',
    en: 'Frequently Asked Questions (FAQ) ... & Answers.'
    }
    ]

If the goal is to manipulate the result from JS or the DOM (either putting the result in an element or passing the text to other functions), pass the option type: string:

var result = XLSX.utils.sheet_to_txt(worksheet, { type: "string" });

Without it, if the codepage library is available (nodejs, standalone xlsx.full.min.js script), the result is converted to UTF-16LE and a BOM is prepended

WORKS PERFECT! :+1:

I could only say for other people that I was only added that parameter {type: 'string'} @ reading file to workbook what was not enough:
const workBookXLSXContentFile = XLSX.readFile(filenameIn, {type: 'string', sheets: FILENAME_IN.split('.')[0]});

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamespan0 picture jamespan0  路  3Comments

seanmcilvenna picture seanmcilvenna  路  3Comments

mmancosu picture mmancosu  路  3Comments

thomasledoux1 picture thomasledoux1  路  3Comments

upasana-shah picture upasana-shah  路  4Comments