如何把有合并格的分组 Excel 转换成 Json

现有Excel文件json.xlsx,如下所示:

EMPId

Name

Date

Periodname

TimUnitDuration

charge entry






Type

Price

21

abc

2015/12/31

15 data

15

fixed

100

45 data

45

fixed

100

42

def

2015/12/31

15 data

15

fixed

100

30 data

30

fixed

100

45 data

45

fixed

100

需要将这样带有合并格的分组Excel中的数据转为json格式,结果如下:

[

    {

        "EMPId":21,

        "Name":"abc",

        "Date":"2015-12-31",

        "Period":[

            {

                "Periodname":"15 data",

                "TimUnitDuration":15,

                "ChargeEntry":[

                    {

                        "Type":"fixed",

                        "Price":100

                    }

                ]

            },

            {

                "Periodname":"45 data",

                "TimUnitDuration":45,

                "ChargeEntry":[

                    {

                        "Type":"fixed",

                        "Price":100

                    }

                ]

            }

        ]

    },

    {

        "EMPId":42,

        "Name":"def",

        "Date":"2015-12-31",

        "Period":[

            {

                "Periodname":"15 data",

                "TimUnitDuration":15,

                "ChargeEntry":[

                    {

                        "Type":"fixed",

                        "Price":100

                    }

                ]

            },

            {

                "Periodname":"30 data",

                "TimUnitDuration":30,

                "ChargeEntry":[

                    {

                        "Type":"fixed",

                        "Price":100

                    }

                ]

            },

            {

                "Periodname":"45 data",

                "TimUnitDuration":45,

                "ChargeEntry":[

                    {

                        "Type":"fixed",

                        "Price":100

                    }

                ]

            }

        ]

    }

]

借助集算器可以很方便地完成件事。
集算器安装包可去润乾网站集算器职场版,运行需要一个授,免版本就用。

1.        在集算器中编写脚本p1.dfx:


A

1

=file("json.xlsx").xlsimport@w(;,3)

2

=A1.group@i(~(1))

3

=A2.(~.(~.(if(~==null,A2.~(1)(#),~))))

4

=create(EMPId,Name,Date,Periodname,TimUnitDuration,CT,CP)

5

=A3.(~.(A4.record(~)))

6

=A4.group(#1,#2,#3;~.group(Periodname,TimUnitDuration;~.new(CT:Type,CP:Price):ChargeEntry):Period)

7

=json(A6)

A1   从第三行开始,读取 json.xlsx 数据,选项@w表示将数据成序列的序列。

A2   若每个序列内第一个值不为空,则新分一组

A3   将合并格的空值补足

A4   创建空序表

A5   将数据插入序表

A6   按 Excel 逻辑对数据分组

A7   序表转 json

2.     执行脚本,A7 中的数据是需要的结果。