Giter Club home page Giter Club logo

xls2lua's Introduction

xls2lua

将excel文件(.xls, .xlsx)转换为lua代码的小工具.

使用方法

可以独立运行,也可以作为一个库导入.

./xls2lua.py *.xlsx

主要特性:

  • 支持多级索引转换,生成层级table
  • 也支持无索引转换,按行生成数组
  • 支持差量转换,只转换有变更的文件(基于文件时间比较)
  • 支持把excel列标题直接做lua表名,也支持用额外metadata指定表名和字段类型(比如列标题可能是中文的)
  • 支持指定转换的数据类型,如string,number,bool,也可以不指定类型

环境需求

python2或者python3均可,需要xlrd模块用于读取excel.

pip install xlrd

对excel表格的要求

可按两种模式来填写excel表格

模式1:

需要在excel中专门建一个名为xls2lua的Sheet,其中每一列对应一个需要转换的Sheet:
第一行指定了该Sheet对应的table名,形如: "SomeSheet=SomeTable"
其余行表示"数据列"到lua变量的映射,形如: "SomeColumn=SomeField"

注意SomeField支持一些符号标记:

  • '*'开头的映射名表示它在table中用作索引,索引可以有多个,但不能所有列都是索引.
  • '#'结尾的映射名表示映射为数字.
  • '$'结尾的映射名表示映射为字符串,指明了这种格式时,在excel中填写时无需加引号.
  • '?'结尾的映射名表示映射为布尔变量,如果填的是字符串,会自动处理常见的值,比如(0, 1, 是,否...).
  • 结尾不是"#$?"的,会尽可能将表格中的字面值照搬到lua中去,这时如果希望被处理为字符串的,需要在表格中自行添加引号.

示例参见test1.xls.

模式2:

无需在excel文件中加入额外的名为xls2lua的meta sheet,直接在列标题中标注即可; 标注格式与方式1类似,示例参见test2.xls.

可能的问题

excel中的数字转换为代码文本时,是按6位小数精度处理的,如果有别的精度要求,可以修改代码中的定义.
注意,调整这个精度可能导致excel中填写的整数转换后变成了近似的小数.

xls2lua's People

Contributors

trumanzhao avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.