Home > Net >  How to get the "first sheet" in Python Google Sheets API
How to get the "first sheet" in Python Google Sheets API

Time:08-09

I'm using the Python googleapiclient to get the contents of a Sheet. The API currently requires specifying the sheet name. That's fine, but sometimes the name of my sheet changes, and then I get an exception googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/... because the hardcoded sheet name could no longer be found. I want to make it work for all cases.

I couldn't find anything like wildcards, so how can I just get the default/first sheet found in the spreadsheet?

Existing Code:

sheets_service = discovery.build('sheets', 'v4', credentials=credentials)
content = sheets_service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range='Sheet1').execute()

What I want:

sheets_service = discovery.build('sheets', 'v4', credentials=credentials)
content = sheets_service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range='*').execute()

CodePudding user response:

You can get the name of the first sheet with spreadsheets.get

Thereby, you can use the knowledge that the first sheet of a spreadsheet always has the sheetId 0.

  • Specify the fields you are interested in when making the request:

sheets = service.spreadsheets().get(spreadsheetId=spreadsheet_id, fields='sheets(properties(sheetId,title))'

  • This will return you an object containig the sheetIds and titles of all sheets within the spreadsheet:
{
  "sheets": [
    {
      "properties": {
        "sheetId": 0,
        "title": "Sheet1"
      }
    },
    {
      "properties": {
        "sheetId": 457388739,
        "title": "Sheet2"
      }
    }
  ]
}
  • Now you only need to filter this object to retrieve the title that belongs to the sheetId 0.

  • This title you can use then instead of Sheet1 to specify the range in spreadsheets.values.get

CodePudding user response:

In your situation, in order to retrieve the sheet name of the 1st tab, how about using "Method: spreadsheets.get" of Sheets API? When this is reflected in your script, it becomes as follows.

Modified script 1:

In this modification, "Method: spreadsheets.get" is used.

sheets_service = discovery.build('sheets', 'v4', credentials=credentials)

# Here, the sheet name of the 1st tab is retrieved.
res = sheets_service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
sheetName = res.get("sheets", [])[0].get("properties").get("title")

content = sheets_service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=sheetName).execute()

Modified script 2:

In this modification, "Method: spreadsheets.get" is not used.

sheets_service = discovery.build('sheets', 'v4', credentials=credentials)
content = sheets_service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range="A:ZZZ").execute()
  • In this modification, only a1Notation of the range without including the sheet name is used. In this case, the values of the 1st tab are retrieved. In the current stage, the maximum column is ZZZ. So, as a sample, "A:ZZZ" is used. But, if you have already known the range, you can use the specific range. When the sheet name is not included, the values are always retrieved from the 1st tab.

Reference:

  • Related