S
S
slip312014-04-27 10:49:54
Google Sheets
slip31, 2014-04-27 10:49:54

Google Sheets - how to get a list of all sheets?

We have a table with 100 sheets (Sheet1, Sheet2, Sheet3 ...). How to get a vertical list of all sheets: Sheet1
Sheet2
Sheet3 ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Biryukov, 2020-10-14
@Slamnlc

function getSheetsListOnSeparateSheet(){ // пример вывода на отдельный лист
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let listWithSheetsNames = ss.insertSheet(0); // или куда вам нужно выводить список листов. 0 - добавляет лист на первое место
  for (let i = 1; i < ss.getNumSheets(); i++){ // если используете другой способ вывода - замените i = 0
    listWithSheetsNames.getRange(listWithSheetsNames.getLastRow() + 1, 1).setValue(ss.getSheets()[i].getSheetName());
  }
}

The loop goes through all the sheets in the workbook.

A
Alexander, 2014-04-28
Obiedkov @aobiedkov

public class MySpreadsheetIntegration {
  public static void main(String[] args)
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

    SpreadsheetService service =
        new SpreadsheetService("MySpreadsheetIntegration-v1");

    // TODO: Authorize the service object for a specific user (see other sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
        SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    if (spreadsheets.size() == 0) {
      // TODO: There were no spreadsheets, act accordingly.
    }

    // TODO: Choose a spreadsheet more intelligently based on your
    // app's needs.
    SpreadsheetEntry spreadsheet = spreadsheets.get(0);
    System.out.println(spreadsheet.getTitle().getPlainText());

    // Make a request to the API to fetch information about all
    // worksheets in the spreadsheet.
    List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();

    // Iterate through each worksheet in the spreadsheet.
    for (WorksheetEntry worksheet : worksheets) {
      // Get the worksheet's title, row count, and column count.
      String title = worksheet.getTitle().getPlainText();
      int rowCount = worksheet.getRowCount();
      int colCount = worksheet.getColCount();

      // Print the fetched information to the screen for this worksheet.
      System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
    }
  }
}

An example from the Spreadsheets API google help,
https://developers.google.com/google-apps/spreadsheets/
Specifically, at first, a list of all tables that this user has is taken:
Then select the desired table and display a list of all sheets in it as you need:
SpreadsheetEntry spreadsheet = spreadsheets.get(0);

List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();

    // Iterate through each worksheet in the spreadsheet.
    for (WorksheetEntry worksheet : worksheets) {
      // Get the worksheet's title, row count, and column count.
      String title = worksheet.getTitle().getPlainText();

      System.out.println("\t" + title);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question