sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Error handling on readFile

Open simonh1000 opened this issue 11 years ago • 2 comments

I am trying to automate an xls file download and import in Mongo process. Every so often I think my file download is not working properly and I get erorrs from Xlsx [TypeError: Cannot read property '3' of undefined], but I am unable to find a way to catch these errors with code - can you advise?

function xls2Json(fname) {
    return new Promise( (resolve, reject) => {
        var wbook = XLSX.readFile(fname+'.xls');
        var worksheet = wbook.Sheets[SHEET_NAME];
        let json = XLSX.utils.sheet_to_json(worksheet, {header:headers, range:1});
        resolve(json);
    });
}

simonh1000 avatar Nov 17 '15 08:11 simonh1000

You can try to use try ... catch

function xls2Json(fname) {
    return new Promise( (resolve, reject) => {
        try {
            var wbook = XLSX.readFile(fname+'.xls');
        } catch (err) {
           console.log(err);
        }
        var worksheet = wbook.Sheets[SHEET_NAME];
        let json = XLSX.utils.sheet_to_json(worksheet, {header:headers, range:1});
        resolve(json);
    });
}

1ur11 avatar Aug 23 '16 15:08 1ur11

@simonh1000 this bug is most likely coming from trying to read an HTML file. Lots of so-called Excel exporters really generate HTML and expect Excel to process it. We have since added a very rudimentary parser. Test out the latest version and let us know if it works.

If there are parse errors, the best approach is to catch errors and then decide how to handle the error.

SheetJSDev avatar Mar 25 '17 16:03 SheetJSDev