How Do I Rename A (work)sheet In A Google Sheets Spreadsheet Using The Api In Python?
Solution 1:
This is an extraction of a library which I've coded personally:
def_batch(self, requests):
body = {
'requests': requests
}
returnself._service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body).execute()
defrenameSheet(self, sheetId, newName):
returnself._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
Solution 2:
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:
<entry>
<id>
https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId
</id>
<updated>2007-07-30T18:51:30.666Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006"
term="http://schemas.google.com/spreadsheets/2006#worksheet"/>
<title type="text">Income</title>
<content type="text">Expenses</content>
<linktype="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full"/>
<linktype="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full"/>
<linktype="application/atom+xml"
href="https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId"/>
<linktype="application/atom+xml"
href="https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId/version"/>
<gs:rowCount>45</gs:rowCount>
<gs:colCount>15</gs:colCount>
</entry>
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.
Solution 3:
Although the title says using the API, the question details OP wanted to use gspread API (instead of using direct calls to Google Spreadsheets API).
I am surprised that there is no answer to this yet. Perhaps gspread had not a method for this when the question was posted, but now it is as simple as using update_title
method:
import gspread
# authenticate:
gc = gspread.service_account()
# choose a gspread method to open your spreadsheet and your worksheet:
wsh = gc.open_by_key(spreadsheetId).worksheet("old_WorkSheetName")
# update your worksheet name:
wsh.update_title("new_WorksheetName")
That's it.
Solution 4:
You could achieve the same with the gspread port for api v4: pygsheets (author here).
The corresponding code using pygsheets would be:
import pygsheets
gc = pygsheets.authorize()
# open spreadsheet and then worksheet
sh = gc.open('my new spreadsheet')
wks = sh.sheet1
wks.title = 'new title'
Solution 5:
If you are using Node, here's what worked for me:
import {google} from'googleapis';
const auth = new google.auth.OAuth2(...)
const sheetsService = google.sheets({version: 'v4', auth})
const requests = [
{
updateSheetProperties: {
properties: {
sheetId: 'id-of-the-sheet-that-you-want-to-rename',
title: 'new-title',
},
fields: 'title'
}
}
];
sheetsService.spreadsheets.batchUpdate({
spreadsheetId: 'some-spreasheet-id',
requestBody: {
requests,
},
});
Post a Comment for "How Do I Rename A (work)sheet In A Google Sheets Spreadsheet Using The Api In Python?"