Ok so I found this really well documented node_module
called js-xlsx
Question: How can I parse an xlsx to output json?
Here is what the excel sheet looks like:
In the end the json should look like this:
JavaScript
x
17
17
1
[
2
{
3
"id": 1,
4
"Headline": "Team: Sally Pearson",
5
"Location": "Austrailia",
6
"BodyText": "...",
7
"Media: ""
8
},
9
{
10
"id": 2,
11
"Headline": "Team: Rebeca Andrade",
12
"Location": "Brazil",
13
"BodyText": "...",
14
"Media: ""
15
}
16
]
17
index.js:
JavaScript
1
18
18
1
if(typeof require !== 'undefined') {
2
console.log('hey');
3
XLSX = require('xlsx');
4
}
5
var workbook = XLSX.readFile('./assets/visa.xlsx');
6
var sheet_name_list = workbook.SheetNames;
7
sheet_name_list.forEach(function(y) { /* iterate through sheets */
8
var worksheet = workbook.Sheets[y];
9
for (z in worksheet) {
10
/* all keys that do not begin with "!" correspond to cell addresses */
11
if(z[0] === '!') continue;
12
// console.log(y + "!" + z + "=" + JSON.stringify(worksheet[z].v));
13
14
}
15
16
});
17
XLSX.writeFile(workbook, 'out.xlsx');
18
Advertisement
Answer
Improved Version of “Josh Marinacci” answer , it will read beyond Z column (i.e. AA1).
JavaScript
1
36
36
1
var XLSX = require('xlsx');
2
var workbook = XLSX.readFile('test.xlsx');
3
var sheet_name_list = workbook.SheetNames;
4
sheet_name_list.forEach(function(y) {
5
var worksheet = workbook.Sheets[y];
6
var headers = {};
7
var data = [];
8
for(z in worksheet) {
9
if(z[0] === '!') continue;
10
//parse out the column, row, and value
11
var tt = 0;
12
for (var i = 0; i < z.length; i++) {
13
if (!isNaN(z[i])) {
14
tt = i;
15
break;
16
}
17
};
18
var col = z.substring(0,tt);
19
var row = parseInt(z.substring(tt));
20
var value = worksheet[z].v;
21
22
//store header names
23
if(row == 1 && value) {
24
headers[col] = value;
25
continue;
26
}
27
28
if(!data[row]) data[row]={};
29
data[row][headers[col]] = value;
30
}
31
//drop those first two rows which are empty
32
data.shift();
33
data.shift();
34
console.log(data);
35
});
36