xlsx-gen-json

excel to json

Usage no npm install needed!

<script type="module">
  import xlsxGenJson from 'https://cdn.skypack.dev/xlsx-gen-json';
</script>

README

xlsx-2-json

1.Excel文件配置规范

1.字段名*(key)*必须为英文字母

2.字段名*(key)*在第一行

3.第二行是字段描述,不做处理

4.辅助表格字段名字用中文,就不会生成在json

5.第一列必须为必填字段,必须有值*(如:Id)*,没有值不会生成在json里

6.遍历xlsx文件里的sheet,在output目录输出以sheet name为名字的json文件

7.生成Data Type 2,首列值连续一样的行会被认为是数组,注意如图字段描述最后要加上“[]”标记生成数组形式

2.脚本调用:

例:在build.js里

const xlsx2json = require("xlsx-gen-json");
xlsx2json.toJson("./excel/gameconfig.xlsx","./src/config/",function(e,r){
    e?console.log(e):console.log(r);
});

3.Sheet and Data

Data Type 1:

Sheet

javascript:

{
    "1":{"name":"superman","toyid":1,"price":100,"tag":[101,103,108]}
}

lua:

local toys =
{
    ["1"] = 
    {
        ["name"] = "superman",
        ["toyid"] = 1,
        ["price"] = 100,
        ["tag"] = 
        {
            101,
            103,
            108
        }
    }
}
return toys

Data Type 2:

Sheet

javascript

{
    "1":[{"num":3,"id":1,"toyid":1},
        {"num":4,"id":1,"toyid":2},
        {"num":5,"id":1,"toyid":3}],
    "2":[{"num":1,"id":2,"toyid":4},
        {"num":4,"id":2,"toyid":1}]
}

lua:

local missions =
{
    ["1"] = 
    {
        {
            ["num"] = 3,
            ["id"] = 1,
            ["toyid"] = 1
        },
        {
            ["num"] = 4,
            ["id"] = 1,
            ["toyid"] = 2
        },
        {
            ["num"] = 5,
            ["id"] = 1,
            ["toyid"] = 3
        }
        },
    ["2"] = 
    {
        {
            ["num"] = 1,
            ["id"] = 2,
            ["toyid"] = 4
        },
        {
            ["num"] = 4,
            ["id"] = 2,
            ["toyid"] = 1
        }
    }
}
return missions

4.更新

1.支持生成以id为Key的Map方式以提高访问效率,参数useDictionary 2.支持生成Lua配置

const xlsx2json = require("xlsx-gen-json");
xlsx2json.toJson("./excel/gameconfig.xlsx","./src/config/",function(e,r){
    xlsx2json.jsonToLua(r,"./src/config/lua/"+s+".lua");
},true);