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

后端 未结 2 2041
死守一世寂寞
死守一世寂寞 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-11 22:43

    Your answer can be solved via a HTTP request from Python.

    Link is here

    You need to send some sort of metadata for the worksheet via HTTP.

    For example, get the ID of the worksheet using Python, and send the following info:

    
      
        https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId
      
      2007-07-30T18:51:30.666Z
      
      Income
      Expenses
      
      
      
      
      45
      15
    
    

    The website has a Java and .NET solution as well. (This is for the legacy version 3)

    For the newer version, you can use a batch update via a POST http request from Python as well.

    link is here

    The data for the request is

    {
      "requests": [{
          "updateSpreadsheetProperties": {
              "properties": {"title": "My New Title"},
              "fields": "title"
            }
        }]
    }
    

    to be sent via POST to https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate

    In both requests, replace the spreadsheetId in the URL with the ID of the Google Sheet you are editing.

    Notice the change from v3 to v4 in the URLs.

    If you are using a version 3 application and want to migrate, the link to that is here

    EDIT

    A commentor noted that the second request does not change name of a worksheet. The link I added shows the way to change intricate properties of a worksheet, I will be updating my answer soon.

提交回复
热议问题