xlsGetSheetCount

Purpose

Returns the number of sheets in an Excel® spreadsheet.

Format

nsheets = xlsGetSheetCount(file)
Parameters:file (string) – name of .xls or .xlsx file.
Returns:nsheets (scalar) – sheet count or an error code.

Portability

Windows, Linux and macOS

Examples

Example 1: Basic usage

Determine the number of sheets in the example data file, yarn.xlsx, with the following code:

// File name with full path
fname = getGAUSShome() $+ "examples/yarn.xlsx";
nsheets = xlsGetSheetCount(fname);

Example 2: Error handling with trap

If you do not want your program to terminate in the case of an error in this function, you can set the trap state as in the example below.

// Turn on trap
trap 1;

fname = "non-existant-file.xlsx";

nsheets = xlsGetSheetCount(fname);

// Check to see if xlsGetSheetCount returned an error code
if scalmiss(nsheets);
    // Code to execute in error case here
    print "xlsGetSheetCount failed";
endif;

Remarks

If xlsGetSheetCount() fails, it will either terminate with an error message or return a scalar error code, which can be decoded with scalerr, depending on the lowest order bit of the trap flag.

trap 0 Print error message and terminate program.
trap 1 Return scalar error code which can be checked for with scalmiss().