David Mckee David Mckee - 6 months ago 82
Javascript Question

How can I parse an Excel file using SheetJS from an external link (Amazon S3)

I am trying to parse an excel file that I already have the URL for. I keep getting different errors when trying to access the file so that it can be readable. Right now, here is my code:

const input_file = doc.input_file;
const extension = input_file.split('.').pop();



let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let oReq = new XMLHttpRequest();
oReq.open("GET", input_file, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
let arraybuffer = oReq.responseText;
/* convert data to binary string */
let data = new Uint8Array(arraybuffer);
let arr = new Array();
for(let i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
let bstr = arr.join("");

/* Call XLSX */
let workbook = XLSX.read(bstr, {type:"binary"});

/* DO SOMETHING WITH workbook HERE */
let firstSheet = workbook.SheetNames[0];
let parsed = XLSX.utils.sheet_to_csv(firstSheet);
console.log(parsed);
}

oReq.send();


The current error I am getting is:
Error: Unsupported file NaN
at the when I try to read the file at:
let workbook = XLSX.read(bstr, {type:"binary"});


I'm unsure on the easiest way to read that external link. Any ideas? If it helps, I am using Meteor.

Answer

I ended up using a combination of a few of these answers. I want to post it here just in case it helps anyone else.

I started at using the Meteor HTTP package as mentioned by Achal.

meteor add http

I also added an additional package from the Meteor community that allowed for response type to be added in.

meteor add aldeed:http

Then, I used the following code to convert to binary and could proceed with reading the sheet:

HTTP.get(input_file, {responseType: 'arraybuffer'}, function(error, result) {
  let data = new Uint8Array(result.content);
  let arr = new Array();
  for(let i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
  let bstr = arr.join("");

  let workbook = XLSX.read(bstr, {type:"binary"});
  var first_sheet_name = workbook.SheetNames[0];
  let sheet = workbook.Sheets[first_sheet_name];
  let parsed = XLSX.utils.sheet_to_json(sheet);
});
Comments