3.4 Multiple source data areas

 

Having solved the multiple-target problem, it is easy to think of the multiple-source problem. In practice, the calculation may need to use multiple source data areas in Excel. However, the clipboard() function only holds the most-recently copied data area. How to solve this problem?

We can copy the clipboard content directly to a cell in the cellset.

The following is an example of “querying the orders that meet the specified conditions”.

There are two sheets in Excel, order details and employee list, where the order details table is as follows:

A B C D E
1 OrderID Client SellerId Amount OrderDate
2 1 WVF Vip 1 440 2014-11-03
3 2 UFS Com 1 1863 1/1/2015
4 3 SWFR 2 1813 11/1/2014
5 4 JFS Pep 2 671 1/1/2015
6 5 DSG 1 3730 1/1/2015
7 6 JFE 1 1445 1/1/2015
8 7 OLF 3 625 1/1/2015
9 8 PAER 3 2490 1/1/2015

The employee list is as follows:

A B C D E F G
1 Eid State Dept Name Gender Salary Birthday
2 2 New York Marketing Ashley F 11001 7/19/1980
3 3 New Mexico Sales Rachel F 9000 12/17/1970
4 4 Texas HR Emily F 7000 3/7/1985
5 5 Texas R&D Ashley F 16000 5/13/1975
6 6 California Sales Matthew M 11000 7/7/1984
7 7 Illinois Sales Alexis F 9000 8/16/1972
8 8 California Marketing Megan F 11000 4/19/1979
9 1 Texas HR Victoria F 3000 12/7/1983

Calculation objective: query the order data in the last days, or the order data belonging to the department list depts. The required columns include the OrderID, OrderDate and Amount in the order details, and the Name and Dept in the employee list. In these conditions, days is an external parameter, and you can enter a different value for each execution, for example, entering 30 means querying the order data in the last 30 days; depts is also an external parameter, such as [“Markeding”,“Finance”]. This calculation objective involves dynamic query and multi-key-value query. Such queries are hard to be achieved in Excel, but is easy to get it done with SPL.

First, select the data area of "Order Details” sheet in Excel, and copy and paste it to the cell A1 in SPL cellset together with the column names; Likewise, copy the data area of “Employee list” sheet to cell A2 in SPL cellset. See the figure below:

imagepng

Note: the paste action should be performed when the cell A1 is in the editing state, otherwise the content of the clipboard will be filled into a large range of cells, as shown below:

imagepng

This figure shows a wrong result. Since it occupies a too large area, the code layout will be affected.

If the operation is correct, only a small part of data is displayed on the appearance of cell A1/A2, and the complete data will be presented only when clicking A1/A2.This is the unique feature of cell-style code, which is suitable for copying a large range of data without affecting reading and code layout.

After copying the two source data areas, edit the script, during which we can run the script many times and perform the copy and paste actions manually. The final code is as follows:

A B
1 OrderID Client… =A1.import@t()
2 EId State… =A2.import@t()
3 =B1.switch(SellerId,B2:EId)
4 =A3.select(OrderDate>=after(date(now()),days*-1)|| depts.pos(SellerId.Dept))
5 =A4.new(OrderID,OrderDate,Amount,SellerId.Name:Name,SellerId.Dept:Dept)

After the calculation is finished, we can use the “copy data” button to copy the calculation result in A5 back to Excel. It should be noted that the data in the cell A1/A2 should be cleared when saving the code, otherwise all source data will be saved.