excel转化为lua配置(xls2lua)

去年瞎折腾的项目中使用到的配置都是excel写的,所以写了一个小的工具来将excel转为为lua。

工具代码(xls2lua.pl):

#!/usr/bin/perl


use Spreadsheet::Read;

use Getopt::Long;


my $xls_file;

my $opt;

my $id_col = 1;

GetOptions("x|xls=s" => \$xls_file,

           "o|opt=s" => \$opt,

           "c|col:i" => \$id_col)

       or die ("Error param");


if ($opt ne "a" and $opt ne "h")

{

    die ("Invalid opt\n");

}


my $book = ReadData("$xls_file");


my $sheet_num = $book->[0]{sheets};

my @titles;

for (my $sheet = 1; $sheet <= $sheet_num; $sheet = $sheet + 1)

{

    my %content = %{$book->[$sheet]};

    my $row = $content{maxrow};

    my $col = $content{maxcol};

    my @titles = {};

    for (my $col_index = 1; $col_index <= $col; $col_index = $col_index + 1)

    {

        my $cell = cr2cell($col_index, 1);

        my $value = $content{$cell};

        push(@titles, $value);

    }

    print "$content{label} = $content{label} or {\n";

    for(my $row_index = 2; $row_index <= $row; $row_index = $row_index + 1)

    {

        my $key_cell = cr2cell($id_col, $row_index);

        my $key = $content{$key_cell};


        if ($opt eq "a")

        {

            print "    {\[\"$titles[$id_col]\"\] = \"$key\", ";

        }

        elsif ($opt eq "h")

        {

            print "    [\"$key\"] = {";

        }

        for (my $col_index = $id_col + 1; $col_index <= $col; $col_index = $col_index + 1)

        {

            my $cell = cr2cell($col_index, $row_index);

            my $value = $content{$cell};

            $value =~ s/^\s+//;

            $value =~ s/\s+$//;

            print "\[\"$titles[$col_index]\"\] = \"$value\"";

            if ($col_index != $col)

            {

                print ", ";

            }

        }

        if ($row_index == $row)

        {

            print "}\n";

        }

        else

        {

            print"},\n";

        }

    }

    print "}\n";

}


脚本依赖:

windows:

activePerl


linux / mac os:

default has perl


librarys:

linux/mac:

sudo cpan install Spreadsheet::Read

sudo cpan install Spreadsheet::XLSX

sudo cpan install Getopt::Long

windows:

可以直接用PPM下载上面三个模块


usage:

./xls2lua.pl --xls excel_file_path --opt [h|a] [--col index]

--xls(-x): excel 文件全路径

--opt(-o):

h  means convert excel to hash table

a  means convert excel to array


--col(-c):开始列索引,即从excel的第几页开始导出到lua,默认为1


注意:

该脚本只是将转化后的结果输出到屏幕,如果要保存成文件,用户可以自己修改脚本,或者使用如下方式:

linux/mac: ./xls2lua.pl -x excel_file -o a | tee file.lua

windows: xls2lua.pl -x excel_file -o -a > file.lua


git地址: https://github.com/liwanghong/xls2lua

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值