簡體   English   中英

有沒有辦法用 gspread 更新谷歌表格過濾視圖范圍?

[英]Is there a way to update google sheets filter view ranges with gspread?

我有一個谷歌表,其中包含多個過濾器視圖,其范圍跨越整個電子表格(范圍:A1:D)由於當前有 9 行數據,過濾器視圖的范圍設置為 A1:D9。 我經常使用帶有 gspread 庫的 python 腳本添加到這個電子表格中,因此,我必須手動更新過濾器視圖的范圍以包含電子表格的新大小。

有沒有辦法通過代碼來做到這一點? 我發現了很多關於它的帖子都暗指這樣的功能,但沒有可供我學習的代碼片段。 我嘗試自己調查這個問題,涉及通過在我的 IDE 上調試運行我的腳本,以嘗試查找包含任何類型的存儲過濾器視圖的變量的電子表格對象的任何屬性,但我找不到任何東西。

我所要求的可能嗎?

我最終能夠通過查看 gspread 庫之外的內容找到答案。 Google Sheets API v4 終於有了答案:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', scope)

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

update_filter_view_request = {
        'updateFilterView': {
            'filter': {
                'filterViewId': filter_view_id,
                'range': {"sheetId": '''tab id goes here''', 
                          "startRowIndex": 0, # 0 is A
                          "endRowIndex": 3, # 3 is D
                          "startColumnIndex": 0,
                          "endColumnIndex": '''updated range value'''}
            },
            'fields': {
                'paths': ['*']
            }
        }
    }

service.spreadsheets().batchUpdate(spreadsheetId='''spreadsheet ID goes here''', body={'requests': [update_filter_view_request]}).execute()

我最終將上述代碼的一部分放入了一個方法信息中

resp = service.spreadsheets().get(spreadsheetId=sheet_id).execute()

插入循環並獲取我有興趣更新的所有選項卡和過濾器視圖的 ID。 希望這對某人有用,因為它困擾了我多年!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM