0

I am using the js-xlsx, here is a link to my excel sheet if needed. I'm using the following code:

/* set up XMLHttpRequest */
var url = "Test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
var arraybuffer = oReq.response;

/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] =String.fromCharCode(data[i]);
var bstr = arr.join("");

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

/* DO SOMETHING WITH workbook HERE */
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];

console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}
oReq.send();

In the console loge I get an output of:

(3) […]​0: Object { FirstName: "Mayuresh", MiddleName: "Dinkar ", LastName: "Joshi", … }​1: Object { FirstName: "Arun", MiddleName: "Vikas", LastName: "Pathak", … }​2: Object { FirstName: "Narendra", MiddleName: "Damodardas", LastName: "Modi", … }​length: 3​<prototype>: Array []

I'm sure not sure how to access that data. I don't know it's name to call it. I tried arr.length, but it only returned 1 and it should've returned 3. The JS files returns a length of 3, but I'm not sure where it's pulling that from. I just need some accessing that Array. Thanks

1 Answer 1

1

It looks like it's working. Try this to see...

let worksheet = workbook.Sheets[first_sheet_name];
let objects = XLSX.utils.sheet_to_json(worksheet,{raw:true});
let names = objects.map(object => `${object.FirstName} ${object.LastName}`);
console.log(names);
Sign up to request clarification or add additional context in comments.

3 Comments

I got "TypeError: workbook.map is not a function". I know it's working, I just don't know how to tap into that array
the new edit doesn't work, but let objects = XLSX.utils.sheet_to_json(worksheet,{raw:true}); let names = objects.map(object => ${object.FirstName} ${object.LastName}); did
yikes. sorry, your response was so quick, i thought you were referring to the prior edit. will fix back. "to_json" is a really non-sensical name for that utlitly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.