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

sheet_to_json from remote file not working

arnigunnar opened this issue · comments

I am writing an Azure Function that will run periodically and get some data from a XLS file that is downloaded from a remote url. If I try to parse the downloaded file, I get an empty array. But if I parse the file directly from disk, all is good in the world.

My code (simplified):

const url = 'https://widgets.baskethotel.com/widget-service/export/view/schedule_and_results?api=a0d07178160bf749eb6e5e761fc623fe42e2bb57&season_id=124655&lang=is&month=all&type=all';

const { data } = await axios.get(url, {
  headers: {
    responseType: 'arraybuffer',
  },
});

const workbookRemote = xlsx.read(data); // <- DOES NOT WORK
const workboolLocal = xlsx.readFile('data.xls') // <- WORKS!

The library seems to find both the workbook and the sheet (they are both defined and not null) when I access the file remotely, but the function sheet_to_json returns no data if I use the workbookRemote, but correct data when I use the workbookLocal.

responseType is a property of the config object. See https://docs.sheetjs.com/docs/demos/network#axios for an example. It should be something like

const url = 'https://widgets.baskethotel.com/widget-service/export/view/schedule_and_results?api=a0d07178160bf749eb6e5e761fc623fe42e2bb57&season_id=124655&lang=is&month=all&type=all';

const { data } = await axios.get(url, { responseType: 'arraybuffer' });

const workbookRemote = xlsx.read(data);

If that fails, check if there are any CORS violations or if the response is a 404.

WOW ... how did I not see that! Jeeez, THX!