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.
Method to use
Section titled “Method to use”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.
Sample code
Section titled “Sample code”/** * 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.