cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
GroupSquareWolf
Level III

Scripting: save multiple data table to multiple sheets in Excel

I like to save multiple datatables, each to an individual sheet, in a single Excel file?

What is the script to achieved that?

 

I tried "append", but it did not work

dt<< Save as("$DESKTOP/Untitled73.xlsx", append);

 

Thanks!

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Scripting: save multiple data table to multiple sheets in Excel

The answer to your question comes from the Scripting Indes

txnelson_0-1667058459806.png

Here is one of the examples from that entry

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Big Class.jmp" );
Open( "$SAMPLE_DATA/Abrasion.jmp" );
Create Excel Workbook(
	"$TEMP/MyWorkbook.xlsx",
	{"Big Class", "Abrasion"},
	{"Big", "Abrasive"}
);
Jim

View solution in original post

txnelson
Super User

Re: Scripting: save multiple data table to multiple sheets in Excel

This issue you are having is with 2 of the elements that you are attempting to pass to the Create Excel Workbook

The  third element in Create Excell Workbook function, need to be a list.  In your case,  the specification is not a list.

  Element 2 documentation indicates that what is required is "List of open tables".  While your specification of

 While the returned element from the Summarize() function, GroupBy, is a list, GroupBy[1] is a string variable.  You need to do something like

tempList = {};
insert into( tempList, GroupBy[1]);
Create Excel Workbook ("$DESKTOP/Untitled1.xlsx", {dt2}, tempList);

 

 

Jim

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Scripting: save multiple data table to multiple sheets in Excel

txnelson
Super User

Re: Scripting: save multiple data table to multiple sheets in Excel

The answer to your question comes from the Scripting Indes

txnelson_0-1667058459806.png

Here is one of the examples from that entry

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Big Class.jmp" );
Open( "$SAMPLE_DATA/Abrasion.jmp" );
Create Excel Workbook(
	"$TEMP/MyWorkbook.xlsx",
	{"Big Class", "Abrasion"},
	{"Big", "Abrasive"}
);
Jim
GroupSquareWolf
Level III

Re: Scripting: save multiple data table to multiple sheets in Excel

Thank you for pointing out the Srcipting index, it is really helpful.

 

A further question: does the "list of the worksheet names" have to be strings? Can they be string variables?

GroupSquareWolf_2-1667191186373.png

 

 

Here is an example I am struggling with:

GroupSquareWolf_1-1667190873534.png

 

The script below creates three datatables based on :Column 2 values, then save three separate sheets in one Excel files. I would like to name each worksheet based on :Column 2 values, but when I put GroupBy variable as the worksheet names (the red script below), it does not assign :Column 2 value to each worksheet. 

 

dt = current datatable();

Summarize (dt, GroupBy = By (:Column 2) );

For ( i=1, i<= N items (GroupBy), i++,

dt<< select where (:Column 2 == GroupBy [i]);

dt2= dt << subset (

	Selected Rows( 1 ),
	Selected columns only( 0 )
);

show (dt2);
show (GroupBy[i]);
Create Excel Workbook ("$DESKTOP/Untitled1.xlsx", {dt2}, GroupBy[i]);
);

 

Thank you for your help!

txnelson
Super User

Re: Scripting: save multiple data table to multiple sheets in Excel

This issue you are having is with 2 of the elements that you are attempting to pass to the Create Excel Workbook

The  third element in Create Excell Workbook function, need to be a list.  In your case,  the specification is not a list.

  Element 2 documentation indicates that what is required is "List of open tables".  While your specification of

 While the returned element from the Summarize() function, GroupBy, is a list, GroupBy[1] is a string variable.  You need to do something like

tempList = {};
insert into( tempList, GroupBy[1]);
Create Excel Workbook ("$DESKTOP/Untitled1.xlsx", {dt2}, tempList);

 

 

Jim