How do I rename a (work)sheet in a Google Sheets spreadsheet using the API in Python?

后端 未结 1 923
别跟我提以往
别跟我提以往 2021-02-11 22:19

I have been trying/looking to solve this problem for a long while. I have read the documentation for gspread and I cannot find that there is a way to rename a worksheet. Any of

1条回答
  •  心在旅途
    2021-02-11 23:06

    This is an extraction of a library which I've coded personally:

    def _batch(self, requests):
        body = {
            'requests': requests
        }
        return self._service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body).execute()
    
    def renameSheet(self, sheetId, newName):
        return self._batch({
            "updateSheetProperties": {
                "properties": {
                    "sheetId": sheetId,
                    "title": newName,
                },
                "fields": "title",
            }
        })
    

    I think that with a little effort, you can implement it into your code and obtain what you want. In order to make the batchUpdate call, you will need the spreadsheetId as well as the initialized service as explained in the Python QUickstart - Google Sheet API

    0 讨论(0)
提交回复
热议问题