Atharva Gupta
Atharva Gupta

Reputation: 35

How do I access different tabs in a Google Sheets via the Google Sheets API?

I am trying to read a sheet from Google Drive using the Google Drive/Sheets API. I was wondering how to navigate between different tabs.

The tab is the circled part, (ignore the other stuff)
(source: cachefly.net)

(Ignore the other stuff, this is a random picture demonstrating what a tab is.)

Essentially, what I want to do is be able to access the other tabs and read the text that is there as well. Each tab is in the same format. I've been able to read from the first tab and would like to do the same for the others as well.

Here is my code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint


# Use credentials to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'foo', scope)
client = gspread.authorize(credentials)

# Opening the first sheet
sheet = client.open("Resume Data").sheet1

# Prints the code in a nicer format
pp = pprint.PrettyPrinter()


# Extract and print all of the values
list_of_resumes = sheet.get_all_records()

# Printing
pp.pprint(list_of_resumes)

Upvotes: 0

Views: 5032

Answers (1)

Tanaike
Tanaike

Reputation: 201388

  • You want to get values from sheets except for the sheet of the 1st index.
  • You want to achieve this using gspread.
  • You have already been able to get and put values for your Spreadsheet.

If my understanding is correct, how about this modification?

From:

sheet = client.open("Resume Data").sheet1

To:

spreadsheetName = "Resume Data"
sheetName = "###"  # <--- please set the sheet name here.
spreadsheet = client.open(spreadsheetName)
sheet = spreadsheet.worksheet(sheetName)

Note:

  • If you want to select the sheet using the index, please use sheet = spreadsheet.get_worksheet(1). This means to select the sheet of 2nd index.

Reference:

Upvotes: 2

Related Questions