Excel到Javascript中的JSON模式

Excel到Javascript中的JSON模式,javascript,json,mongodb,mean-stack,mean,Javascript,Json,Mongodb,Mean Stack,Mean,我有一个生成表单excelsheet的任务,其中我必须根据excelsheet中提供的数据类型设计表单。例子: 我试图从上面的excel数据中创建一个JSON模式,以便将其插入mongodb以动态生成表单 以下是我试图实现的代码: var workbook = XLSX.readFile(req.file.path); //console.log(workbook); var result = {};

我有一个生成表单excelsheet的任务,其中我必须根据excelsheet中提供的数据类型设计表单。例子:

我试图从上面的excel数据中创建一个
JSON
模式,以便将其插入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);
                    }
                }
            }
以上是我试图在MEANSTACK中实现的代码

任何帮助都将不胜感激。

从XLSX文件生成JSON。看看那个代码

它是用Java编写的。它使用
apache.poi
解析XLSX文件,并使用
mongodb.bson
生成JSON。也许它会给你一些有用的想法


并且是用Javascript编写的。如果您在github上搜索,您可能会找到有用的代码。

在我看来,您似乎混淆了的概念。Abc Xyza的财务部门是数据部门。给定Excel文件的部门的可能值为财务、健康或保险是模式

以下是一个例子:

因此,如果问题的标题是正确的,并且您需要一个JSON模式,那么我将使用从提供的值创建字符串数组的函数,在“下拉”或“单选按钮”类型的情况下,确定列中值的数据类型(int表示Number,string表示FirstName,等等),确定最小值和最大值,甚至是允许的字符串模式

我在这里设想的输出是这样的:

{
    "id" : "http://your.site/form-schema",
    "title" : "Form schema",
    "description" : "JSON schema for autogenerating forms",
    "type" : "object",
    "properties" : {
        "Number" : {
            "type" : "integer"
        },
        "FirstName" : {
            "type" : "string"
        },
        "LastName" : {
            "type" : "string"
        },
        "Dept" : {
            "type" : "string",
            "oneOf" : [
                        { "format" : "Finance"},
                        { "format" : "Health" },
                        { "format" : "Insurance" }
            ]
        },
        "Country" : {
            "type" : "string",
            "oneOf" : [
                        {"format" : "US" },
                        { "format" : "Australia" },
                        { "format" : "Canada" }
            ]
        },
        "Year" : {
            "type" : "integer",
            "oneOf" : [
                        { "format" : "2014" },
                        { "format" : "2015" },
                        { "format" : "2016" }
            ]
        },
        "DateofBirth" : {
            "type" : "string",
            "pattern" : "yyyyMMdd"
        },
        "Gender" : {
            "enum" : ["M", "F"]
        }
    },
    "required" : ["Number", "FirstName", "LastName"],
    "additionalProperties" : false
}

您可以使用库读取客户端的XLSX和其他excel格式

您不需要存储下拉列表,也不需要在输入乘法中填充任何其他值。它们应该分开存放。因此数组对象应该如下所示

[   
    {  
      "Number":1,
      "FirstName":"Abc",
      "LastName":"Xyza",
      "Dept":"Finance",
      "Country":"US",
      "Year":2014,
      "DateOfBirth":19370502,
      "Gender":"M"
    },
    {  
      "Number":2,
      "FirstName":"Abcd",
      "LastName":"Xyzb",
      "Dept":"Health",
      "Country":"Australia",
      "Year":2014,
      "DateOfBirth":19481027,
      "Gender":"F"
    }
]
下拉列表和单选值应单独存储,如下所示:

{  
   "Dept":{  
      "type":"dropdown",
      "values":[  
         "Finance",
         "Health",
         "Insurance"
      ]
   },
   "Country":{  
      "type":"dropdown",
      "values":[  
         "US",
         "Australia",
         "Canada"
      ]
   },
   "Year":{  
      "type":"dropdown",
      "values":[  
         2014,
         2015,
         2016
      ]
   },
   "Gender":{  
      "type":"radio button",
      "values":[  
         "M",
         "F"
      ]
   }
}
这两者可以作为一个模式对象组合在一起

//included single objects from both for brevity
jsonSchema = {
    array: [
        {  
            "Number":2,
            "FirstName":"Abcd",
            "LastName":"Xyzb",
            "Dept":"Health",
            "Country":"Australia",
            "Year":2014,
            "DateOfBirth":19481027,
            "Gender":"F"
        }
    ],
    inputs: {
        "Gender":{  
            "type":"radio button",
            "values":[  
                "M",
                "F"
            ]
        }
    }
};
注意:序列化为JSON时,日期类型值不能存储为日期对象。这些数据应存储为字符串或数字,并应在客户端转换为日期对象

我在这个GIT项目中实现了JSON生成和表单生成

以下是输出屏幕截图

下面是JSON的输出

{
  "array": [
    {
      "Number": 1,
      "FirstName": "Abc",
      "LastName": "Xyza",
      "Dept": "Finance",
      "Country": "US",
      "Year": 2014,
      "DateOfBirth": 19370502,
      "Gender": "M"
    },
    {
      "Number": 2,
      "FirstName": "Abcd",
      "LastName": "Xyzb",
      "Dept": "Health",
      "Country": "Australia",
      "Year": 2014,
      "DateOfBirth": 19481027,
      "Gender": "F"
    },
    {
      "Number": 3,
      "FirstName": "Abce",
      "LastName": "Xyzc",
      "Dept": "Health",
      "Country": "US",
      "Year": 2015,
      "DateOfBirth": 19441029,
      "Gender": "F"
    },
    {
      "Number": 4,
      "FirstName": "Abcf",
      "LastName": "Xyzd",
      "Dept": "Insurance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19481030,
      "Gender": "M"
    },
    {
      "Number": 5,
      "FirstName": "Abcg",
      "LastName": "Xyze",
      "Dept": "Finance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19480604,
      "Gender": "M"
    }
  ],
  "inputs": {
    "Dept": {
      "type": "dropdown",
      "values": [
        "Finance",
        "Health",
        "Insurance"
      ]
    },
    "Country": {
      "type": "dropdown",
      "values": [
        "US",
        "Australia",
        "Canada"
      ]
    },
    "Year": {
      "type": "dropdown",
      "values": [
        2014,
        2015,
        2016
      ]
    },
    "Gender": {
      "type": "radio button",
      "values": [
        "M",
        "F"
      ]
    }
  }
}

不要粘贴一个代码块并要求我们调试它。你试过什么?实际和预期的行为是什么?错误消息@胡安曼德斯:好的。让我更新一个post@JuanMendes:我已更新后plz检查它预期的
Dept
值是数组吗?它不应该是下拉列表的值吗?我建议先将工作表转换为常规的字符串2d数组,然后再创建对象。有什么错误/问题?你对问题投入的越多,回答的速度就越快。@11thdimension:谢谢
{
  "array": [
    {
      "Number": 1,
      "FirstName": "Abc",
      "LastName": "Xyza",
      "Dept": "Finance",
      "Country": "US",
      "Year": 2014,
      "DateOfBirth": 19370502,
      "Gender": "M"
    },
    {
      "Number": 2,
      "FirstName": "Abcd",
      "LastName": "Xyzb",
      "Dept": "Health",
      "Country": "Australia",
      "Year": 2014,
      "DateOfBirth": 19481027,
      "Gender": "F"
    },
    {
      "Number": 3,
      "FirstName": "Abce",
      "LastName": "Xyzc",
      "Dept": "Health",
      "Country": "US",
      "Year": 2015,
      "DateOfBirth": 19441029,
      "Gender": "F"
    },
    {
      "Number": 4,
      "FirstName": "Abcf",
      "LastName": "Xyzd",
      "Dept": "Insurance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19481030,
      "Gender": "M"
    },
    {
      "Number": 5,
      "FirstName": "Abcg",
      "LastName": "Xyze",
      "Dept": "Finance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19480604,
      "Gender": "M"
    }
  ],
  "inputs": {
    "Dept": {
      "type": "dropdown",
      "values": [
        "Finance",
        "Health",
        "Insurance"
      ]
    },
    "Country": {
      "type": "dropdown",
      "values": [
        "US",
        "Australia",
        "Canada"
      ]
    },
    "Year": {
      "type": "dropdown",
      "values": [
        2014,
        2015,
        2016
      ]
    },
    "Gender": {
      "type": "radio button",
      "values": [
        "M",
        "F"
      ]
    }
  }
}