3

I'm successfully reading my Excel file in React by following this SO thread as.

    var reader = new FileReader();
    reader.onload = function (e) {
        var data = e.target.result;
        let readedData = XLSX.read(data, {type: 'binary'});
        const wsname = readedData.SheetNames[0];
        const ws = readedData.Sheets[wsname];

        /* Converts a worksheet object to an array of JSON objects*/ 
        const parsedData = XLSX.utils.sheet_to_json(ws, {header:1});
        console.log(parsedData);
    }
    reader.readAsBinaryString(fileName)

But having a simple problem, i.e., it's reading empty rows as well and causing empty entries in array. Output of console.log(parsedData); in the above code is

enter image description here

I know a quick hack is to remove empty entries from the array but I want to know a better approach to avoid this problem even happening.

1 Answer 1

3

Edit - It's "blankrows" and not "blankRows"

I did a search and came across a similar question on gitmemory here, which shows that there's a blankRows property you can set to false in order to skip blank rows, which would look like this with your implementation:

/* Converts a worksheet object to an array of JSON objects*/ 
const parsedData = XLSX.utils.sheet_to_json(ws, {
    header:1,
    blankrows: false
});
Sign up to request clarification or add additional context in comments.

Comments

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.