return JSON excel 中每行数据的对象
return JSON object for each row of data in excel
我正在为 SheetJS 使用 webpack。我是 webpack 的新手,也是 SheetJS 的新手。我不想 returning one JSON 对象 Excel 数据,而是 return 一个 JSON 对象Excel.
中的每行数据
示例输入 excel 布局:
Col1 Col2 Col3
A2 B2 C2
A3 B3 C3
本例中的理想输出是 2 JSON 个对象:
JSON1:
{
"A2": [
{
"Col1": "A2"
"Col2": "B2"
"Col3": "C2"
}
]
}
JSON 2:
{
"A3": [
{
"Col1": "A3"
"Col2": "B3"
"Col3": "C3"
}
]
}
尝试的解决方案:
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);
}
当前结果:
{
"Col3": [
"Col1",
"Col2",
"Col3"
],
"C2": [
"A2",
"B2",
"C2"
],
"C3": [
"A3",
"B3",
"C3"
]
}
任何方向正确的事情都会很棒。如果对您有帮助,这里是 github repo .. 谢谢!
你的意图是正确的代码是错误的。您正在定义数据并将其推送到数组中,而不是创建 JSON 对象。试试这个。
var to_json_linebyline = function to_json_linebyline(wb){
var sheet = wb.Sheets['Sheet1'];
var results = [];
var range = XLSX.utils.decode_range(sheet['!ref']);
for(let rowNum = (range.s.r+1); rowNum <= range.e.r; rowNum++){
let thisRow = {},
thisNode = '';
for(let colNum=range.s.c; colNum<=range.e.c; colNum++){
var thisHeader = sheet[XLSX.utils.encode_cell({r: 0, c: colNum})].w
var thisCell = sheet[XLSX.utils.encode_cell({r: rowNum, c: colNum})].w
if(colNum === 0){
thisNode = thisCell;
}
thisRow[thisHeader] = thisCell;
}
thisResult = {};
thisResult[thisNode] = [thisRow]
results.push(thisResult)
}
return JSON.stringify(results);
}
我正在为 SheetJS 使用 webpack。我是 webpack 的新手,也是 SheetJS 的新手。我不想 returning one JSON 对象 Excel 数据,而是 return 一个 JSON 对象Excel.
中的每行数据示例输入 excel 布局:
Col1 Col2 Col3
A2 B2 C2
A3 B3 C3
本例中的理想输出是 2 JSON 个对象:
JSON1:
{
"A2": [
{
"Col1": "A2"
"Col2": "B2"
"Col3": "C2"
}
]
}
JSON 2:
{
"A3": [
{
"Col1": "A3"
"Col2": "B3"
"Col3": "C3"
}
]
}
尝试的解决方案:
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);
}
当前结果:
{
"Col3": [
"Col1",
"Col2",
"Col3"
],
"C2": [
"A2",
"B2",
"C2"
],
"C3": [
"A3",
"B3",
"C3"
]
}
任何方向正确的事情都会很棒。如果对您有帮助,这里是 github repo .. 谢谢!
你的意图是正确的代码是错误的。您正在定义数据并将其推送到数组中,而不是创建 JSON 对象。试试这个。
var to_json_linebyline = function to_json_linebyline(wb){
var sheet = wb.Sheets['Sheet1'];
var results = [];
var range = XLSX.utils.decode_range(sheet['!ref']);
for(let rowNum = (range.s.r+1); rowNum <= range.e.r; rowNum++){
let thisRow = {},
thisNode = '';
for(let colNum=range.s.c; colNum<=range.e.c; colNum++){
var thisHeader = sheet[XLSX.utils.encode_cell({r: 0, c: colNum})].w
var thisCell = sheet[XLSX.utils.encode_cell({r: rowNum, c: colNum})].w
if(colNum === 0){
thisNode = thisCell;
}
thisRow[thisHeader] = thisCell;
}
thisResult = {};
thisResult[thisNode] = [thisRow]
results.push(thisResult)
}
return JSON.stringify(results);
}