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

Viewed 1042

I'm trying to change column width with Google Sheets API. I know that spreadsheetId I can get with ss_drive.get('id') but I don't know how to get sheetId:

ss_drive = drive_api.files().create(body=file_metadata).execute()
ssId = ss_drive.get('id')

sheets_api = build('sheets', 'v4', credentials=credentials)

requests = []
requests.append({
    "updateDimensionProperties": {
    "range": {
      "sheetId": sheetId,
      "dimension": "COLUMNS",
      "startIndex": 0,
      "endIndex": 1
    },
    "properties": {
      "pixelSize": 160
    },
    "fields": "pixelSize"
    }
})

response = sheets_api.spreadsheets().batchUpdate(
    spreadsheetId=ssId, body={'requests': requests}).execute()

I changed "sheetId": sheetId to "sheetId": 0. And the column changed its width.

1 Answers

You can have multiple sheets stored in your account, so you need to know the sheet ID beforehand. You can log in to https://sheets.google.com, navigate to the sheet of your choice and the URL will look something like:

https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms

The token after the d/ is the spreadsheet's ID which you can use in your queries according to the developer example.

Related