Did you know that you can easily parse an excel spreadsheet using Node.js? Keep reading to speed up your information retrieval processes.
...
For further actions, you may consider blocking this person and/or reporting abuse
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]}
You're right, thanks! I can't believe I didn't think of that before. :D
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 asfalse
on boolean checksif (!rowVal) {
// code
}
Another point is that if you know that
rowVal
will always be aString
, 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 useswitch-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 ofArray.forEach()
. TheArray.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... 😅
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. 😅
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/
Nice post don't forget the code block syntax highlighting it makes it more readable 😉
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! :)
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 😛
Haha, glad I was able to inspire you! :D
Excellent !!
Thanks for your feedback!