使用Sheets API调用隐藏列

时间:2016-11-29 19:32:18

标签: google-sheets google-sheets-api

我想通过API v4隐藏Google电子表格中的给定列,但我很难这样做。

有没有人知道它是否可能并且设法做到了? 我们在Apps脚本中有dedicated method这样做,如果REST API中没有此功能,我会感到惊讶。

1 个答案:

答案 0 :(得分:5)

是的,有。这不是很直接。

以下是隐藏第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()