我有一個任務來生成Excel表格,我必須根據Excel表格中提供的數據類型來設計表單。例如:
Excel中的JSON模式在Javascript中
我試圖做一個JSON
模式從上面的Excel數據,這樣我可以將其插入MongoDB的動態生成表格。
以下是代碼我想實現:
var workbook = XLSX.readFile(req.file.path);
//console.log(workbook);
var result = {};
workbook.SheetNames.forEach(function (sheetName) {
var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if (roa.length > 0) {
result = roa;
}
});
//return result;
//console.log(result);
var jsonData = {};
var dropdown = {};
var attrTypes = result[0];
//console.log(attrTypes);
for (var i = 1; i < result.length; i++) {
var obj = result[i];
//console.log(obj);
for (var key in obj) {
var attrName = key;
var attrValue = obj[key];
if (attrTypes[attrName]) {
var type = attrTypes[attrName].toLowerCase().replace(/ /g, ''); // Means type is given
//console.log(type);
if (type === "selectbox") {
console.log(attrValue);
//var dropdown = attrValue;
//console.log(dropdown);
}
} else {
//console.log(type); // Means type is not given
jsonData = attrName + ":" + attrValue;
//console.log(jsonData);
}
}
}
預計JSON
輸出:
[
{
Number : 1,
FirstName : "Abc",
LastName : "Xyza",
Dept: ['Finance','Health','Insurance'],
Country : ['US','Australia','Canada'],
Year : ['2014','2015','2016'],,
DateofBirth" : new Date(1937,05,02),
Gender : ['M','F']
},
{
Number : 2,
FirstName : "Abcd",
LastName : "Xyzb",
Dept: ['Finance','Health','Insurance'],
Country : ['US','Australia','Canada'],
Year : ['2014','2015','2016'],,
DateofBirth" : new Date(1948,10,27),
Gender : ['M','F']
}
.
.
and so on
]
以上就是我想在MEANSTACK執行代碼。
任何幫助,將不勝感激。
不要粘貼一段代碼並要求我們調試它。你有什麼嘗試?實際與預期的行爲是什麼?錯誤訊息? http://stackoverflow.com/help/how-to-ask –
@JuanMendes:好的。讓我更新一個帖子 – Sky
@JuanMendes:我已更新帖子檢查 – Sky