2016-03-30 32 views
4

我有一個任務來生成Excel表格,我必須根據Excel表格中提供的數據類型來設計表單。例如:
enter image description hereExcel中的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執行代碼。

任何幫助,將不勝感激。

+4

不要粘貼一段代碼並要求我們調試它。你有什麼嘗試?實際與預期的行爲是什麼?錯誤訊息? http://stackoverflow.com/help/how-to-ask –

+0

@JuanMendes:好的。讓我更新一個帖子 – Sky

+0

@JuanMendes:我已更新帖子檢查 – Sky

回答

1

你可以閱讀客戶端的XLSX和其他使用JS-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時,日期類型值不能存儲爲日期對象。這些應該被存儲爲一個字符串或數字,並應轉換爲Date對象在客戶端

我已經實現這個Git項目

https://github.com/ConsciousObserver/stackoverflow/tree/master/excelTest

繼JSON生成和表單生成的輸出屏幕截圖。

Generated Forms

這裏是輸出的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" 
     ] 
    } 
    } 
} 
+0

@ 11thdimention:謝謝 – Sky

+0

不客氣! – 11thdimension

0

This project從XLSX文件生成JSON。看看這個代碼。

它用Java編寫。它使用apache.poi解析XLSX文件並使用mongodb.bson生成JSON。也許它會給你任何有用的想法。

This projectthis project用Javascript寫的。如果你在github上搜索,你可能會發現有用的代碼。

0

在我看來,你混淆了schema with that of data的概念。 Abc Xyza是財務部門是數據。給出Excel文件的部門可能的價值是財務,健康或保險是架構。

下面是一個例子:http://json-schema.org/examples.html

因此,如果問題的標題是正確的,你需要一個JSON模式,那麼我會去創造從價值提供的字符串數組功能,在的情況下, 'dropdown'或'radiobutton'類型,確定列中的值的數據類型(int爲Number,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 
}