Hide column with Sheets API call

2020-07-23 06:24发布

问题:

I would like to hide a given column in a google spreadsheet through the API v4, but I struggle to do so.

Does anyone know if it's possible and has managed to do it?
We have in the Apps Script a dedicated method to do so, and I would be surprised if this feature is not available in the REST API.

回答1:

Yes, there is. It's just not very straightforward.

Here is an example of hiding column 5:

import httplib2
from apiclient.discovery import build

credentials = get_credentials()  ## See Google's Sheets Docs
http = credentials.authorize(httplib2.Http())

service = build('sheets', 'v4', http=http)

spreadsheet_id = '#####################'
sheet_id = '#######'
requests = []

requests.append({
  'updateDimensionProperties': {
    "range": {
      "sheetId": sheet_id,
      "dimension": 'COLUMNS',
      "startIndex": 4,
      "endIndex": 5,
    },
    "properties": {
      "hiddenByUser": True,
    },
    "fields": 'hiddenByUser',
}})

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