How to call a specific sheet within a spreadsheet via the Google Sheets API v4 in Python

RemakingEden

I'm pretty new to this stuff and I've really hit a brick wall on an early project of mine.

I've done my best to look everywhere including - Search engines, StackOverflow, the API documentation and I really cannot find the answer anywhere. So I hope its appropriate to ask.

I am trying to view a range from a specific sheet (tab at the bottom) of a Google sheet. I know this sheet has a sheetId that can be found in the url. I can get this, however, I can find absolutely no way to request a specific sheet with this number. I have found ways to use this number to copy and duplicate sheets and I have even found a way to print all the available sheetId's on a spreadsheet.

def suggestion():

SAMPLE_SPREADSHEET_ID = 'mySpreadsheetID'
SAMPLE_RANGE_NAME = 'A1:D12'

"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('creds.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                            range=SAMPLE_RANGE_NAME,).execute()
values = result.get('values', [])

if not values:
    print('No data found.')
else:
    for row in values:
        # Print columns A and E, which correspond to indices 0 and 4.
        print(row)

I believe the answer is probably in here https://developers.google.com/resources/api-libraries/documentation/sheets/v4/python/latest/sheets_v4.spreadsheets.values.html but I can't seem to find it.

Right now the program is opening the default sheet (tab at the bottom) when I would like it to open one of the specific sheets and look for data there.

Thanks for any help in advance. It's appreciated massively!

mr.freeze

Google sheets uses A1 notation to specify a range. This can include the specific tab that you're trying to access values from.

You can specify a tab in your range value by adding the name of the sheet before the range. For example, if you wanted something from Sheet2:

SAMPLE_RANGE = 'Sheet2!A2:E10'

If you rename your sheet tab then you would need to update your range to match it since it is reference by name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update google spreadsheet via sheets API v4

Get list of sheets and latest sheet in google spreadsheet api v4 in Python

How To Use Google Sheets API v4 To Create New Sheet or Tab in Spreadsheet with PHP

How can I get single sheet from a spreadsheet collection from Google Sheets with Google Sheets API v4?

How to delete rows in google spreadsheet using SHEET API v4 by Python?

Create spreadsheet with Google Sheets API V4 API Key

How to protect a sheet via Google Sheets API?

GoogleSheets API v4 Python : Unable to re-order sheets in google spreadsheet

Reading whole Google Spreadsheet with Sheets API v4 Java

How do I split all merged cells for a specific sheet within a Google Spreadsheet using Python?

How to remove a blank row in google sheet using google sheets api v4 javascript?

Google sheets API v4 Create sheet and invite users

How to insert the current date time into a google sheet, respecting the sheet's timezone, using sheets api v4?

Create a new sheet within an existing spreadsheet google sheets

Reading Sheet2 via Google Sheets API & Python

How can I use the Google Sheets (v4) API getByDataFilter to return a specific row of data?

How to specify the gid (tabs) in Google Spreadsheet API v4?

How to authenticate in Google Spreadsheet Api V4 with Server Key

How to write to a specific range of a sheet in google sheets API

Google Sheets - Sending email based on specific sheet in a spreadsheet

PDI "Google Spreadsheet Input/Output" plugin compatible with Google Sheets API v4?

How to programmatically (via AppsScript) open a specific Sheet in a Google Spreadsheet via URL

How to update Spreadsheet Properties with Google Sheets API and Python?

How to add a new Sheet into existing SpreadSheet? Google Sheets API with Node.js

How to change column width with Google Sheets API v4 in Python?

Writing in Google Sheets API v4 using Python

How to append rows with google sheet api v4 in nodejs

How can I use the Google Sheets (v4) API to modify a specific row of data dependent on a specific cell?

Checking for Empty Cells in Google Sheets using c# via Google Sheets API v4