Greetings,
I’ve been beating my head for long enough with code that should be quite simple.
I would like to take tables opened from multiple excel file and sheets and join them in a wide table with columns that are named to correspond to their excel sheet.
In my case, columns should be as shown below.
Col 1 of TestWB1Sheet1 |
Col 2 of TestWB1Sheet1 |
Col 3 of TestWB1Sheet1 |
Col 1 of TestWB1Sheet2 |
... |
Col 3 of TestWB2Sheet4 |
The one catch is that I don't want to upload data from the last sheet in each excel file (sheet5 in my example).
I’ve tried using the code from this discussion but can’t seem to get the naming correct. https://community.jmp.com/t5/Discussions/Joining-Multiple-Data-Table-using-a-loop/m-p/9451
I believe the answer may be in this discussion but it is hard for me to digest the solution because of the extraneous code.
https://community.jmp.com/t5/Discussions/Joining-multiple-DataTables-and-retaining-column-name/m-p/1...
Here is my simplified code that doesn’t name columns correctly:
Names Default to Here(1);
//Close All is a convienience script to use while writing the script
//Close All(Data Tables, NoSave);
//Create list of files to open
file = Pick File("Pick file/files", "$Desktop", {"All Files|*"}, 1,0, "", multiple);
//create blank list of data tables
dtList = {};
//Open list of files to dtList
for (i=1, i<=nitems(file), i++,
ws = Get Excel Worksheets(file[i]);
sws = ws[1::(N Items(ws) - 1)];
show(i);
for (s=1, s<=nitems(sws), s++,
show(s);
dt = Open(file[i],
Worksheets( sws[s] ),
use for all sheets( 1 ),
Concatenate Worksheets( 0 ),
Create Concatenation Column( 0 ),
Worksheet Settings(
1,
Has Column Headers( 1 ),
Number of Rows in Headers( 1 ),
Headers Start on Row( 1 ),
Data Starts on Row( 2 ),
Data Starts on Column( 1 ),
Data Ends on Row( 0 ),
Data Ends on Column( 0 ),
Replicated Spanned Rows( 1 ),
Replicated Spanned Headers( 0 ),
Suppress Hidden Rows( 1 ),
Suppress Hidden Columns( 1 ),
Suppress Empty Columns( 1 ),
Treat as Hierarchy( 0 ),
Multiple Series Stack( 0 ),
Import Cell Colors( 0 ),
Limit Column Detect( 0 ),
Column Separator String( "-" )
)
);
dt << Set Name(char(i)||char(s));
insert into (dtList, dt);
)
);
//Designate dt_old to hold the joined data
dt_old = data table(dtList[1]);
//Join all tables in dtList to dt_old by row number
for ( i = 2, i <= N Items( dtList ), i++,
dt_new = data table(dtList[i]);
join_name = "Limits" || char(i);
show(i);
show(dt_new);
dt_old << Join( With (dt_new),
By Row Number,
Output Table( join_name )
);
show(dt_old);
close(dt_old, nosave);
dt_old = data table(join_name);
);
Thanks in advance for the help.