12.12 Dynamic association operation

 

The sheets of the following Excel file are divided into three types, of which Sheet A is the basic table, part of the data is as follows:

imagepng

Sheet B1\B2…Bn are the association table, and all of these sheets have the same format, and have the same columns (Interval1, Interval2 and Interval3) as Sheet A. The figure below shows the data of Sheet B1:

imagepng

Sheet C is used to describe the Join type between A and B1\B2..Bn. There are totally 3 types of joins, of which the cross Join represents Cartesian product, and both the leftJoinBig and leftJoinSmall represent the left association, and the associated columns are Interval1 and Interval2 respectively. See the following figure for details:

imagepng

Calculation objective: associate sheet A with Sheet B1\B2..Bn according to the join type in sheet C, take the column Interva1 from sheet A, and take other columns from sheet B, and finally form a new two-dimensional table.
Let’s take the above sheet B as an example (actually each B should be different), if JoinType==crossJoin, the association result should be:

imagepng

If joinType==leftJoinBig, the association result should be:

imagepng

If joinType==leftJoinSmall, the association result should be:

imagepng

Since this calculation needs to loop through sheet C and outputs multiple two-dimensional tables, it can only be implemented by using script instead of formula.
Script:

A B
1 =file(“data.xlsx”).xlsopen()
2 =A1.xlsimport@t(;“C”)
3 =tableA=A1.xlsimport@t(;“A”)
4 for A2 =tableB=A1.xlsimport@t(;A4.Table)
5 =case(A4.JoinType,“crossJoin”:xjoin(tableA:A;tableB:B),“leftJoinBig”:xjoin@1 (tableA:A;tableB:B,A.Interval1==Interval1),“leftJoinSmall”:xjoin@1(tableA:A;tableB:B,A.Interval1==Interval1 && A.Interval2==Interval2))
6 =B5.new(A.Interval1,B.Interval2,B.Interval3,B.Type,B.Value1,B.Value2,B.Value3)
7 =file(A4.Table+A4.JoinType+“.xlsx”).xlsexport@t(B6)

The script function case can judge the Join type, xjoin calculates the Cartesian product, and @1 represents the left association.


esProc Desktop and Excel Processing
12.11 Find changes through comparison
13.1 Row-to-column conversion for fixed columns