DEV Community

Cover image for Easily parse an excel spreadsheet into JSON

Easily parse an excel spreadsheet into JSON

Nena on October 28, 2020

Did you know that you can easily parse an excel spreadsheet using Node.js? Keep reading to speed up your information retrieval processes. ...
Collapse
 
waldtq profile image
wald-tq

you can simplify the code like this and get rid of the loop and ifs:

let currentRow = {id: row.values[0], name: row.values[1], img: row.values[2]}

Collapse
 
nena profile image
Nena

You're right, thanks! I can't believe I didn't think of that before. :D

Collapse
 
caiangums profile image
Ilê Caian • Edited

Awesome post @nena !

Just some notes:
1 - If you use the syntax highlight at your code block it makes it more readable! For JavaScript, you just need to place javascript after your opening 'triple `'

2 - You should avoid using == and prefer ===. I wrote an article here about it 😄 : dev.to/caiangums/the-js-equality-c...

3 - You could test just for rowVal, because the empty string counts as false on boolean checks


if (!rowVal) {
// code
}

Another point is that if you know that rowVal will always be a String, you could check for empty string instead:


if (rowVal.length > 0) {
// code
}

4 - For you JS code, instead of using multiple else if's you could use switch-case:


// switch-case
const COLUMN = {
ID: 1,
NAME: 2,
IMG: 3,
// and so on...
};
switch(colNum) {
case COLUMN.ID:
currentRow.id = rowVal;
break;
case COLUMN.NAME:
currentRow.name = rowVal;
break;
case COLUMN.IMG:
currentRow.img = rowVal;
break;
default:
break;
}

5 - For this specific case, you could use Array.reduce() instead of Array.forEach(). The Array.reduce() is always a good option to be used when you want to convert a List into another type of object (String, Number, Object...)

Edit: Apparently, the "code block" at responses doesn't work properly... 😅

Collapse
 
nena profile image
Nena

Wow, thanks for your great feedback!

And a special thanks for telling me how to highlight the code block. This bugged me as well but since it was my first post I didn't know how to highlight it yet. So thanks for enlightening me, I just edited it! :)

Regarding 4: I used to love switch-case but when I started coding in JavaScript I often heard that it's not as performant as if-else unless you have a looooot of elses. Do you know more about that?

As for the rest: Thanks for pointing it out, it's mostly stuff I definitely know but most of the times just forget about or are too lazy to be consistent with. 😅

Collapse
 
juniorterin profile image
Ademir Terin Junior

I made a tool thats dynamicly convert Google's Spreadsheet JSON endpoint to a worthily readable (not judging the original complexity but common guys, know you looking for a free BaaS solution for your garage project, u have no time and money to spend spreadsheet-json-beaut.glitch.me/

Collapse
 
andrewbaisden profile image
Andrew Baisden

Nice post don't forget the code block syntax highlighting it makes it more readable 😉

Collapse
 
nena profile image
Nena

Yes, this bugged me as well. :D It was my first post, so I didn't know how to do it yet. But I just learned how and edited it! :)

Collapse
 
bholmesdev profile image
Ben Holmes

Good stuff! Thanks for presenting the need for it at the top of the post as well. My gears are turning on replacing crazy CMS setups with some straight Google sheets 😛

Collapse
 
nena profile image
Nena

Haha, glad I was able to inspire you! :D

Collapse
 
amplanetwork profile image
Ampla Network

Excellent !!

Collapse
 
nena profile image
Nena

Thanks for your feedback!