Yes, you can do this in Python or any other language supported by the Google APIs Client Libraries. Regardless of which language, you need to use the latest Google Sheets API. Let's say you don't know where sheet4
is but want to move it to the 2nd slot as you've diagrammed.
Here are your steps, assuming SHEETS
is your service endpoint, and FILE_ID
is the ID of the Sheets file in your Google Drive:
- Find the ID of the sheet you want to move by getting your Sheets file data:
rsp = SHEETS.spreadsheets().get()
- Cycle through each sheet, i.e.,
for sheet in rsp['sheets']
and match sheet['properties']['title'] == 'sheet4'
- Now update the properties of that sheet by setting its index to 2 with something like the request below.
Here's the code for the last part:
reqs = {'requests': [
# reorder sheet4 to index 2
{'updateSheetProperties': {
'properties': {
'sheetId': sheet['properties']['sheetId'],
'index': 2
}
}}
]}
SHEETS.spreadsheets().batchUpdate(
spreadsheetId=FILE_ID, body=reqs).execute()
Sheets 2 & 3 should slide over after you've made this change. Hope this helps!
If you've already played around with the samples in the docs and want to learn more by seeing some "real-world" examples of using the Sheets API with Python, I've made a couple of videos that may be useful: