Home

Awesome

excelizor

A simple tool that can be used to export .xlsx files to lua-table, json and their corresponding csharp classes and golang structs

excelizor 是一个简易的,用于将 .xlsx 文件导出为 lua-table、json 及其对应的 c# 类和 golang 结构体的工具

Travis Go Report Card GitHub release license

Installation | 安装

$ go get https://github.com/sNaticY/excelizor

or you could download release version directly at Release Page

或直接前往 Release页面 下载

Usage | 使用方法

Usage: excelizor -p <path> [-lua=<luaExportPath>] [-json=<jsonExportPath>] [-csharp=<csharpExportPath>] [-golang=<golangExportPath>] [-tag=<tag>]
  -csharp string
    	path to place exported .cs class files, export no .cs files if parameter is missing
  -golang string
    	path to place exported .go struct files, export no .go files if parameter is missing
  -json string
    	path to place exported .json files, export no .json files if parameter is missing
  -lua string
    	path to place exported .lua files, export no .lua files if parameter is missing
  -p path
    	[Required] Relative path of excel files folder
  -tag string
        only field with this tag or empty string will be exported

Excel Content Format | Excel 内容格式

example

Sheet

Only the first sheet in .xlsx file will be export

只有 .xlsx 的第一份表将会被导出,其默认名称为 "Sheet1",命名为 "Vertical" 将会开启特殊的纵向表解析功能

Default name in excel is "Sheet1", or you could give a special name "Vertical"

Head & Key | 表头与键

The first 4 rows in your excel is Head

每个表格的前四行定义为表头

  1. First row is descriptions of each field, It will not be exported at all so you can fill it with everything you want.
  2. Second row is name of each field.
  3. Third row is type of each field. [int, float, string, bool, dict, list]
  4. Forth row is tag of each field, same tag with arguments and empty tag will always be exported.

The first field must be "Id-int" as a key of every row

  1. 第一行是每一个对应字段的描述,该行的任何数据都不会被导出,因此可任意填写(一般是一个中文注释。。。)
  2. 第二行是字段名称
  3. 第三行是字段类型,如[int, float, string, bool, dict, list]等
  4. 第四行为对应字段的标签,仅当该值与导出时 -tag 参数一致或留空的字段会被导出

第一个字段目前仅支持 "Id-int" 作为每一行数据的 key

Basic Type | 基本类型

Excel

整型字符串浮点布尔注释(不导出)
IdNumberTestStringTestFloatTestBoolTestcan be empty
Intintstringfloatbool//comment
clientserver
1001345This is a string2,6truewon't be exported
1002nilnilnilnilnil

Export Json (-tag with "client"), so the field bool BoolTest = true/false will be ignored, but float FloatTest = 2.6 will be included

导出 Json 时添加 -tag=client 参数,因此该表中bool BoolTest = true/false 将会被忽略,而float FloatTest = 2.6会被导出

[ 
    { 
        "Id": 1001,
        "NumberTest": 345,
        "StringTest": "This is a string",
        "FloatTest": 2.600,
        "BoolTest": true
    },
    { 
        "Id": 1002
    }
]

Export Lua(-tag with 'server"'), so the field bool BoolTest = true/false will be included, float FloatTest = 2.6 will be ignored

导出 Lua 时添加 -tag=server 参数,因此该表中bool BoolTest = true/false 将会被导出,而float FloatTest = 2.6会被忽略

local BasicTypes = {
    [1001] = {
        Id = 1001,
        NumberTest = 345,
        StringTest = "This is a string",
        BoolTest = true,
    },
    [1002] = {
        Id = 1002,
    },
}

return BasicTypes

Export csharp class (with no tag), so all the fields with tag "client" and "server" will be ignored

导出 c# 不添加 tag 参数,因此标签为 "client" 或 "server" 的字段都会被忽略

using System.Collections.Generic;

namespace Configs
{
    public class BasicTypes 
    {
        public int Id;
        public int NumberTest;
        public string StringTest;
    }
}

Export golang struct

package exports

type BasicTypes struct {
	Id int32 `json:"Id"`
	NumberTest int32 `json:"NumberTest"`
	StringTest string `json:"StringTest"`
}

Nested Type | 嵌套类型

T is any Type such as float or int, δ is count of column which it cost in table, can be [0, +∞)], When δ == 0, the number of elements in the structure can be arbitrary and seperated with "|", otherwise, the maximum number of elements can not exceed the delta, only one element in every single cell.

T 代表某个类型如floatint,δ 代表该字段占用的后续列数且 δ >= 0,当 δ == 0 时,该 list 或 dict 中元素的个数将不受数量限制,且使用 "|" 分隔,当 δ > 0 时,该 list 或 dict 中元素的数量将不得大于 δ,每个单元格中填写一个元素

Excel

Spread DictionaryFold DictionarySpread ListFold List
IddictTest1Item1Item2Item3DictTest2ListTest1ListTest2
intdict<float>:3                 dict<int>:0      list<int>:3         list<float>:0
20024.445.556.66Item1=10 | Item2=111231241250.2|0.4|0.6
20034.44nil6.66nilnil1.3|1.5|1.7

Export Json

[ 
    { 
        "Id": 2002, 
        "DictTest1": { 
            "Item1": 4.440,
            "Item2": 5.550,
            "Item3": 6.660 
        }, 
        "DictTest2": { 
            "Item1": 10,
            "Item2": 11 
        },
        "ListTest1": [ 
            123,
            124,
            125
        ],
        "ListTest2": [ 
            0.200,
            0.400,
            0.600
        ]
    },
    { 
        "Id": 2003, 
        "DictTest1": { 
            "Item1": 4.440,
            "Item3": 6.660 
        },
        "ListTest2": [ 
            1.300,
            1.500,
            1.700
        ]
    }
]

Export lua

local NestedTypes = {
    [2002] = {
        Id = 2002, 
        DictTest1 = {
            Item1 = 4.440,
            Item2 = 5.550,
            Item3 = 6.660, 
        }, 
        DictTest2 = {
            Item1 = 10,
            Item2 = 11, 
        },
        ListTest1 = {
            [0] = 123,
            [1] = 124,
            [2] = 125,
        },
        ListTest2 = {
            [0] = 0.200,
            [1] = 0.400,
            [2] = 0.600,
        },
    },
    [2003] = {
        Id = 2003, 
        DictTest1 = {
            Item1 = 4.440,
            Item3 = 6.660, 
        },
        ListTest2 = {
            [0] = 1.300,
            [1] = 1.500,
            [2] = 1.700,
        },
    },
}

return NestedTypes

Export csharp class

using System.Collections.Generic;

namespace Configs
{
    public class NestedTypes 
    {
        public int Id;
        public Dictionary<string, float> DictTest1;
        public Dictionary<string, int> DictTest2;
        public List<int> ListTest1;
        public List<float> ListTest2;
    }
}

Export golang struct

package exports

type NestedTypes struct {
	Id int32 `json:"Id"`
	DictTest1 map[string]float32 `json:"DictTest1"`
	DictTest2 map[string]int32 `json:"DictTest2"`
	ListTest1 []int32 `json:"ListTest1"`
	ListTest2 []float32 `json:"ListTest2"`
}

Multi-nested Type | 多重嵌套结构

MOST OF LOGIC IS EXACTLY THE SAME AS BEFORE. HARD TO EXPLAIN, LET'S SEE SOME EXAMPLES

表格填写逻辑与之前完全相同,很难描述,大家直接看示例即可 好吧其实是英文不好描述不出来,大概就是一个嵌套的结构使用"{"和"}"括起来再使用"|"分隔就好

Excel

KEYDICTINDICT1
IdDictTest3SubDict1Item1Item2SubDict2Item1Item2
intdict<dict<int>:2>:2
30013111311231213122
3002nil32213222
DICTINDICT2DICTINDICT3
DictTest4SubDict1SubDict2DictTest5
dict<dict<float>:0>:2dict<dict<string>:0>:0
it1=31.11|it2=31.12|it3=31.13it1=31.21|it2=31.22Subdict1={item1=asd|item2=sdf}|Subdict2={item1=qwe|item2=wer}
nilnil

Export Json

[ 
    { 
        "Id": 3001, 
        "DictTest3": {  
            "SubDict1": { 
                "Item1": 3111,
                "Item2": 3112 
            }, 
            "SubDict2": { 
                "Item1": 3121,
                "Item2": 3122 
            } 
        }, 
        "DictTest4": {  
            "SubDict1": { 
                "It1": 31.110,
                "It2": 31.120,
                "It3": 31.130 
            }, 
            "SubDict2": { 
                "It1": 31.210,
                "It2": 31.220 
            } 
        }, 
        "DictTest5": {  
            "Subdict1": { 
                "Item1": "asd",
                "Item2": "sdf" 
            }, 
            "Subdict2": { 
                "Item1": "qwe",
                "Item2": "wer" 
            } 
        }
...

Export lua

local MultiNestedTypes = {
    [3001] = {
        Id = 3001, 
        DictTest3 = { 
            SubDict1 = {
                Item1 = 3111,
                Item2 = 3112, 
            }, 
            SubDict2 = {
                Item1 = 3121,
                Item2 = 3122, 
            }, 
        }, 
        DictTest4 = { 
            SubDict1 = {
                It1 = 31.110,
                It2 = 31.120,
                It3 = 31.130, 
            }, 
            SubDict2 = {
                It1 = 31.210,
                It2 = 31.220, 
            }, 
        }, 
        DictTest5 = { 
            Subdict1 = {
                Item1 = "asd",
                Item2 = "sdf", 
            }, 
            Subdict2 = {
                Item1 = "qwe",
                Item2 = "wer", 
            }, 
        },
...

Export csharp class

using System.Collections.Generic;

namespace Configs
{
    public class MultiNestedTypes 
    {
        public int Id;
        public Dictionary<string, Dictionary<string, int>> DictTest3;
        public Dictionary<string, Dictionary<string, float>> DictTest4;
        public Dictionary<string, Dictionary<string, string>> DictTest5;
...

Export golang struct

package exports

type MultiNestedTypes struct {
	Id int32 `json:"Id"`
	DictTest3 map[string]map[string]int32 `json:"DictTest3"`
	DictTest4 map[string]map[string]float32 `json:"DictTest4"`
	DictTest5 map[string]map[string]string `json:"DictTest5"`
...

FOR MORE EXAMPLES, PLEASE CHECK excels/nested_types.xlsx AND exports/nested_types.*

欲查看更多示例,请直接打开 excels/nested_types.xlsx 和 exports/nested_types.***

Other Features | 其他特性

Customizable templates | 可自定义模板

All of exporting features are based on go-template, so you can edit templates/*.tmpl to do anything you want

导出功能基于 go-template,因此直接修改 templates/*.tmpl 即可任意修饰导出文件

Auto convert file name | 自动转换文件名

We recommand your excel name is full_lowercase_letters.xlsx. When exporting csharp class file, it will auto convert your file name to CamelFileName.cs to adapt csharp code style. Exporting other files is not affected.

当文件名为“全小写+下划线分割”式时,导出的 .cs 文件的文件名将会自动转换为驼峰式,其他文件名则保持不变

All the class or struct name, even lua table name will also be CamelFileName

导出的 c# 类和 lua-table 表名以及 golang 结构体名都会自动转换为驼峰式

Auto convert field name | 自动转换字段名称

In csharp class and golang struct, public field should start with a capital letter. If you start with lowercase letter, we will automatically convert to capital letters for you

在 c# 和 golang 中 public 字段名称首字母通常为大写,故字段首字母小写时将自动转换为大写

Vertical sheet | 纵向表

Sometimes you have only few rows in a sheet but many fields, it will be much easier to transpose your sheet. So we support vertical sheet.

某些情况下一个表格只有很少的几列却有很多字段,通常进行转置操作后会更容易管理,因此纵向表的功能是被支持的。

整型字符串浮点布尔布尔布尔...
IdNumberTestStringTestFloatTestBoolTest1BoolTest2BoolTest34,5,6,7,8,9,10
intintstringfloatboolboolbool,,,
1001345This is a string2.6truefalsetrue,,,

Edit your sheet name (default is "Sheet1") to "Vertical" and then you can fill your cell like this

将表名 (默认为"Sheet1") 修改为 "Vertical" 后按照以下方式填写表格后,

Idint1001
整型NumberTestint345
字符串StringTeststringThis is a string
浮点FloatTestfloat2.6
布尔BoolTest1booltrue
布尔BoolTest2boolfalse
布尔BoolTest3booltrue
布尔BoolTest4boolfalse
布尔BoolTest5booltrue
布尔BoolTest6boolfalse
布尔BoolTest7booltrue
布尔BoolTest8boolfalse
布尔BoolTest9booltrue
布尔BoolTest10boolfalse

Then everyhing still works well.

直接导出即可。

Type check | 类型检查

That is a basic feature. We will check if the value is valid for the type, so if you fill 2..2in a float cell, we will tell you.

这只是基础功能,任何字段的值导出前都会进行检查以确认是否可以正确解析。如果在一个 float 字段中填写了 "2..2",将会直接报错

Comment row | 注释行

If it is not enough for you to only use comment column ( field type start with "//" ) , You can insert a row which id start with "//". Everything in this row will be ignored.

如果注释列的功能(以"//"起始的字段将被忽略)还不够的话,可以插入以"//"为id起始的一行,该行在导出时会被忽略。

整型字符串浮点布尔注释(不导出)
IdNumberTestStringTestFloatTestBoolTestcan be empty
Intintstringfloatbool//comment
1001345This is a string2,6truewon't be exported
//1002ok, I can filleverythingbecausethis rowwill be ignored

Contributing

If you are interested in contributing to the excelizor project, please make a PR.

如果你对本项目感兴趣可以随时 pull request

License

This project is licensed under the MIT License.

License can be found here.