Currently, I am delving into webpack for SheetJS. My experience with webpack is still growing, and I'm a complete novice when it comes to SheetJS. Rather than just getting back one JSON object containing all the Excel data, my goal is to retrieve one JSON object for every row of data in the Excel sheet.
Here's an example of the input excel layout:
Col1 Col2 Col3
A2 B2 C2
A3 B3 C3
In this scenario, the desired output would consist of 2 JSON objects:
JSON 1:
{
"A2": [
{
"Col1": "A2"
"Col2": "B2"
"Col3": "C2"
}
]
}
JSON 2:
{
"A3": [
{
"Col1": "A3"
"Col2": "B3"
"Col3": "C3"
}
]
}
This is what I've tried so far:
var to_json_linebyline = function to_json_linebyline(wb){
var sheet = wb.Sheets['Sheet1'];
var result = {};
var row, rowNum, colNum;
var range = XLSX.utils.decode_range(sheet['!ref']);
for(rowNum = range.s.r; rowNum <= range.e.r-2; rowNum++){
row = [];
for(colNum=range.s.c; colNum<=range.e.c; colNum++){
var nextCell = sheet[
XLSX.utils.encode_cell({r: rowNum, c: colNum})
];
if( typeof nextCell === 'undefined' ){
row.push(void 0);
} else row.push(nextCell.w);
}
result[nextCell.v] = row;
}
return JSON.stringify(result, 2, 2);
}
The current outcome looks like this:
{
"Col3": [
"Col1",
"Col2",
"Col3"
],
"C2": [
"A2",
"B2",
"C2"
],
"C3": [
"A3",
"B3",
"C3"
]
}
I would greatly appreciate any guidance or assistance pointing me in the right direction. For further reference, you can access the GitHub repository. Thank you!