My current task involves creating a form in an Excel sheet where the design is based on the provided data types. For example:
https://i.sstatic.net/9v4Cw.png
To achieve this, I am working on converting the Excel data into a JSON schema that can be inserted into MongoDB to dynamically generate the form.
This is the code snippet I am currently implementing:
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);
}
}
}
Desired JSON
output:
[
{
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
]
The above code is part of my implementation in MEANSTACK.
I would greatly appreciate any assistance with this project.