cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
tsl
tsl
Level III

How Can I get a list of sheets in an Excel workbook ?

I have a situation where I am opening Excel sheets using Open Database().

This has been a good way to do it since I want specific columns from specific sheets, so using an SQL SELECT statement is ideal.

I am now discovering there is no standardization on the sheet names. So what I want to do is query the Excel workbook and get a list of sheets in it. I can then use some logic in my code to decide which ones I actually want ( because while there is no standardization, there are enough "similarities" that I could figure out what I want, by checking does the sheet name contain StringA or StringB or StringC for example )

I have no idea how this can be done in JSL. Searching onling, there are plenty of code examples using VBScripting etc which will return the sheets in an Excel workbook, but that is not much help.

I tried also to find some SQL statement that would do it, but I don't know of a table name that would list the "tables"  / sheets in the workbook.

I know JMP can do it because if I use File -> DataBase -> Open Table and connect to an Excel data source it will list all the sheets available in the Schemas - Tables column. Likewise if I set a user preference to select individual Excel Worksheets and use the Open() command it will ask me to select which sheets I want to open.

So my question is does anybody know if JMP exposes that capability to figure out the Excel sheet names to the scripting language ?

many thanks

Thomas

7 REPLIES 7
mpb
mpb
Level VII

Re: How Can I get a list of sheets in an Excel workbook ?

Here's a very clunky way which may not even be feasible if the tables are very large. Just a fragment ...

1. Set preferences to open all Excel sheets on file>open.

2. countBefore = N Table();

3. Open the Excel file using open()

4. countAfter = N Table();

5. nSheets = countAfter - countBefore

5. dtname = {};

5. for(i=1, i <= nSheets, i++, dtname = Data Table(i) << get name);

Then dtname is a list of Sheet names you can play with and you can close the tables you don't want.

Message was edited by: mpb
Corrected indexing in the code fragment.

mpb
mpb
Level VII

How Can I get a list of sheets in an Excel workbook ?

Note that the code fragment above was corrected.

Re: How Can I get a list of sheets in an Excel workbook ?

The last step may get you messed up if you have data tables open before you open the excel worksheets.  Quick correction is

for(i=1, i <= nSheets, i++, dtname = Data Table(i+countBefore) << get name);

MathStatChem
Level VI

Re: How Can I get a list of sheets in an Excel workbook ?

This is an old thread, but found it useful.  Here is a script that will do this by opening the contents of the XLSX file directly.  XLSX files are simply ZIP files with a collection of XML files that define the properties, structure, and elements of the workbook.  

 

Names Default To Here( 1 );

filename = pick file("Choose Excel File", "$DOCUMENTS", {"Excel Files|xlsx"} );

zipxlsx = Open( filename, Zip );

zip_file = zipxlsx << read( "xl/workbook.xml", Format( blob ) );

// Open the file
zz = Open(
	zip_file,
	invisible,
	xml,
	XML Settings(
		Stack( 0 ),
		Row( "/workbook/sheets/sheet" ),
		Col(
			"/workbook/sheets/sheet/@name",
			Column Name( "name" ),
			Fill( "Use Once" ),
			Type( "Character" ),
			Format( {"Best"} ),
			Modeling Type( "Nominal" )
		)
	)
);

worksheets=(zz:name << get values);      

close(zz, nosave);

write(worksheets);
mmarchandFSLR
Level IV

Re: How Can I get a list of sheets in an Excel workbook ?

mmarchandFSLR_0-1734644675003.png

 

MathStatChem
Level VI

Re: How Can I get a list of sheets in an Excel workbook ?

Seriously?  How did I not know about this.  Thanks, though, very helpful!

txnelson
Super User

Re: How Can I get a list of sheets in an Excel workbook ?

How did you not know about that.....because you are human.  No one can know everything in JMP and then every 18 months or so, a plethora of new features and functions come out in a new release.  Personally, I learn so much from you and your contributions on sooooooo many things in JMP that I do not know, or no longer remember.

Jim