/xls2lua

A script to convert xls or xlsx to lua files.

Primary LanguagePythonMIT LicenseMIT

xls2lua

Convert xls to lua script for game resource

(将xls数据文件转化为lua脚本,作为游戏资源使用)

use python xlrd

(使用python xlrd模块)

Blog: http://www.luzexi.com

Email: jesse_luzexi@163.com

What's this.(是什么)

This is a script to convert xls to lua.

If you use lua language , the data write in lua is the best thing for you to code.

So this script will help you convert xls to lua , so you can do your job more easily.

(如果你在使用Lua语言,将数据写进Lua文件是最方便的做法。这个脚本将帮助你将数据xls文件转化为lua文件,这样你就可以更好的工作了。)

Declare (声明)

This script is inherit from https://github.com/zfengzhen/xls2lua .

I improve it to fit my data rule like add array type in script and remove the different talbe name in xls and so on.

Any way , you can choose one that more fit your project.

(这个脚本是从 https://github.com/zfengzhen/xls2lua 继承过来的,我改进了很多东西,也去除了很多东西,我改成了适合我自己的脚本。不管怎样,你可以选择一个适合你的脚本去运行。)

Example(例子xls表格)

example_building.xls

id name use_money use_food is_init defense args1 args2 args3 args4
i s i f b i ai af as ab
1 house 1000 2.33 TRUE 100 1;2;3 1.23;2;3.23 sdf;23e;s true;false;true
2 house2 123 336.2 TRUE 1;2;3 1;2.3445;3 你好;你在哪 true;false
3 456 222.33665 FALSE 130 3;2;5;; 3;2;2.5;; 我在这里啊;你在那;呢 false;true
4 farm 100 220 FALSE 200 2;3; 200.3;3;234.23; df;ssd;dd;dd
5 house5 22.1 2343;6;6;;;7 3;6.3;6;;;7 ss;d;d;d true;true
6 horse3 200 FALSE 333 2e;w;e;we false;false;false;false

Excute Example (举例执行命令)

python ./xls2lua.py example_building.xls ./data/

NOTICE:(注意点)

The sheet name must start with "output_" , the lua file name will be the name behind "output_".
The first row must be title.
The second row must be type
The **type must be i , f , s , b , ai , af , as , ab.
i mean int , f mean float , s mean string , b mean bool , ai mean array int , af mean array float , as mean array string , ab mean array bool.
The first column must be int , so the type in first column must be i.
The string type with char " or ' will be replace by " or '
The empty col will be a default value like 0 or "" or false or {}
(sheet名以"output_"开头的才会被识别转换,否则将被忽略)
(第1行必须是关键字名)
(第2行必须为类型)
(类型有:i,f,s,b,ai,af,as,ab这几种)
(i表示int,f表示float,s表示string,b表示bool,ai表示int数组,af表示float数组,as表示string数组,ab表示bool数组)
(第1列必须为int类型的唯一关键字)
(string类型中"和'会自动用"和'替代) (空列将会被默认值代替,例如:0,"",false,{})

Lua script (生成后的Lua文件示例)

-- this file is generated by program!
-- don't change it manaully.
-- source file: example_building.xls
-- created at: Thu Mar 26 02:53:52 2015

local data = {}

data[1] = { id = 1,  name = "house",  use_money = 1000,  use_food = 2.33,  is_init = true,  defense = 100,  aadd = {1,2,3},  aadddss = {1.23,2,3.23},  ddff = {"sdf","23e","s"},  ffdd = {true,false,true}}
data[2] = { id = 2,  name = "house2",  use_money = 123,  use_food = 336.2,  is_init = true,  defense = 0,  aadd = {1,2,3},  aadddss = {1,2.3445,3},  ddff = {"你好","你在哪"},  ffdd = {true,false}}
data[3] = { id = 3,  name = "",  use_money = 456,  use_food = 222.33665,  is_init = false,  defense = 130,  aadd = {3,2,5},  aadddss = {3,2,2.5},  ddff = {"我在这里啊","你在那",""},  ffdd = {false,true}}
data[4] = { id = 4,  name = "farm",  use_money = 100,  use_food = 220.0,  is_init = false,  defense = 200,  aadd = {2,3},  aadddss = {200.3,3,234.23},  ddff = {"df","ssd","dd","dd"},  ffdd = {}}
data[5] = { id = 5,  name = "house5",  use_money = 0,  use_food = 22.1,  is_init = false,  defense = 234,  aadd = {3,6,6,7},  aadddss = {3,6.3,6,7},  ddff = {"ss","d","d","d"},  ffdd = {true,true}}
data[6] = { id = 6,  name = "horse3",  use_money = 200,  use_food = 0,  is_init = false,  defense = 333,  aadd = {},  aadddss = {},  ddff = {"2e","w","e","we"},  ffdd = {false,false,false,false}}

return data

How to use lua with data. (如何使用生成的lua数据)

local building = require "building"

print(building[1].name)

The console will print "house"