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

后端 未结 2 2038
死守一世寂寞
死守一世寂寞 2021-02-11 22:10

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

2条回答
  •  隐瞒了意图╮
    2021-02-11 22:46

    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

提交回复
热议问题