Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - How to skip empty rows in excel while reading with xlsx

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.

like image 958
DevLoverUmar Avatar asked Nov 24 '25 10:11

DevLoverUmar


1 Answers

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
});
like image 173
lh-22 Avatar answered Nov 27 '25 00:11

lh-22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!