Emerson Nery
Emerson Nery

Reputation: 11

Get Number of Sheets inside Google Sheets using Google Apps Script

I'm new to using Google Apps Script, I still need to read the documentation.

I have a list of sheets that are inside a folder, in this list (all Excel files), and these fields (GDrive_ID, File_Name, Full_Path, URL), I would like a function to read either the ID of the file or URL and count the number of sheets within each Google Workbook. I found this code on a forum and it served, only for the current worksheet, it would be possible to adapt it to search for this data inside the cell (which is actually the address for the worksheets), having with reference the ID, URL or name of the worksheet source?

I'm using this code in current file:

function getNumSheets() {
   return SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
}

Upvotes: 0

Views: 1104

Answers (1)

Cooper
Cooper

Reputation: 64140

Count Sheets and Sheets

Convert All Files to Google Sheets: https://stackoverflow.com/a/56073634/7215091

function countSheetsAndSheets() {
  const folder = DriveApp.getFolderById("folderid");
  const files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
  const shts = [];
  while (files.hasNext()) {
    let file = files.next();
    let ss = SpreadsheetApp.openById(file.getId());
    shts.push({ "name": file.getName(), "id": file.getId, "sheets": ss.getSheets().length })
  }
  //Logger.log(JSON.stringify(shts));
  return JSON.stringify(shts);
}

Upvotes: 2

Related Questions