背景信息:
我正在尝试从庞大而复杂的xlsx文件创建JSON文件。但是,我将在此处发布一个小示例,以便我们将其简化。我提出了以下SheetJS code:
代码:
<!doctype html>
<html>
<head>
<title>Excel to JSON Demo</title>
<script src="xlsx.full.min.js"></script>
</head>
<body>
<script>
/* set up XMLHttpRequest */
var url = "array test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i)
arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {type:"binary"});
/* DO SOMETHING WITH workbook HERE */
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}
oReq.send();
</script>
</body>
</html>
问题:
我已使用此代码转换以下简单的xlsx file。现在的问题是我没有得到预期的JSON文件。
我得到的:
(6) […]
0: {…}
Blocklist: "id"
__EMPTY: "name"
__EMPTY_1: "type"
__EMPTY_2: "blocks"
__rowNum__: 1
version: 1
<prototype>: Object { … }
1: {…}
Blocklist: "dsts"
__EMPTY: "grupo"
__EMPTY_1: 2
__EMPTY_2: "sort ID"
__EMPTY_3: "exercises"
__rowNum__: 2
<prototype>: Object { … }
2: {…}
__EMPTY_2: 1
__EMPTY_3: "dsts1"
__EMPTY_4: "dsts2"
__EMPTY_5: "dsts3"
__rowNum__: 3
<prototype>: Object { … }
3: {…}
__EMPTY_2: 2
__EMPTY_3: "dsts4"
__EMPTY_4: "dsts5"
__EMPTY_5: "dsts6"
__rowNum__: 4
<prototype>: Object { … }
4: {…}
Blocklist: "afus"
__EMPTY: "Fußtechnik"
__EMPTY_1: 1
__EMPTY_2: 1
__EMPTY_3: "afus1"
__EMPTY_4: "afus2"
__EMPTY_5: "afus3"
__rowNum__: 5
<prototype>: Object { … }
5: {…}
__EMPTY_2: 2
__EMPTY_3: "afus4"
__EMPTY_4: "afus5"
__EMPTY_5: "afus6"
__rowNum__: 6
<prototype>: Object { … }
length: 6
<prototype>: Array []
我需要什么
{
"version": "1.0",
"blocklist": [
{
"id" : "dsts",
"name": "grupo",
"type" : 2,
"blocks": [
{
"sortId": 1,
"exercises": [
"dsts1",
"dsts2",
"dsts3"
]
},
{
"sortId": 2,
"exercises": [
"dsts4",
"dsts5",
"dsts6"
]
}
]
},
{
"id" : "afus",
"name": "Fußtechnik",
"type" : 1,
"blocks": [
{
"sortId": 1,
"exercises": [
"afus1",
"afus2",
"afus3"
]
},
{
"sortId": 2,
"exercises": [
"afus4",
"afus5",
"afus6"
]
}
]
}
]
}
问题:
很明显,我在这里有2个选项:或者我使Excel文件适合我的JS代码,或者我使代码适合Excel文件。我愿意做这两个事情,但是我认为修改代码会更聪明,因为Excel文件将来应该不断更新和扩展。那么,为了使它输出所需的JSON格式,我应该更改/添加什么代码?
其他信息: