Skip to content

Get the number of sheets

This article explains the method to use when you want to get the number of sheets in a specific spreadsheet.

Spreadsheet.getNumSheets();

Use getNumSheets(), which is provided by the Spreadsheet class. It returns an integer.

Because the script accesses the spreadsheet, you may need to authorize it the first time you run it.

/**
* ID of the target spreadsheet
* The xxx part of https://docs.google.com/spreadsheets/d/xxx/edit
*/
const SPREAD_SHEET_ID = '_______________';
/**
* Returns the number of sheets in the spreadsheet
*/
const getNumSheets = () => {
/** Target spreadsheet */
const ss = SpreadsheetApp.openById(SPREAD_SHEET_ID);
/** Number of sheets in the spreadsheet */
const numSheets = ss.getNumSheets();
console.log(numSheets);
return numSheets;
};

When you run it, the number of sheets in the target spreadsheet appears in the log.