Software Developer & Team Lead at TQ-Systems GmbH
I am hiring: https://www.tq-group.com/de/unternehmen/karriere/stellenangebote/detail/softwareentwickler-iot-web-und-cloud-services-50010403-652/
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 `'
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... 😅
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/
The streaming approach is the part that matters most for real sheets. Loading a whole workbook into memory works for a small file, but row-by-row parsing keeps large exports usable. The header row is worth treating carefully too: normalizing it once, and deciding how to handle blank or duplicate headers, saves a lot of cleanup later when the JSON is consumed.
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 😛
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
javascriptafter 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 asfalseon boolean checksif (!rowVal) {
// code
}
Another point is that if you know that
rowValwill 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/
The streaming approach is the part that matters most for real sheets. Loading a whole workbook into memory works for a small file, but row-by-row parsing keeps large exports usable. The header row is worth treating carefully too: normalizing it once, and deciding how to handle blank or duplicate headers, saves a lot of cleanup later when the JSON is consumed.
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! :)
Excellent !!
Thanks for your feedback!
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