Check out Kite (free AI Coding Assistant) → Link



Buy Me a Coffee? Your support is much appreciated!

PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn

Source Code:

from Google import Create_Service # source code is in the description

CLIENT_SECRET_FILE = '<client secret file directory>'
API_SERVICE_NAME = 'sheets'
API_VERSION = 'v4'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

service = Create_Service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)
google_sheets_id = '<Google Sheets Id>'

response = service.spreadsheets().get(spreadsheetId=google_sheets_id).execute()
sheet_names = [sheet['properties']['title'] for sheet in response['sheets']]
sheet_names = sorted(sheet_names, reverse=False)

requests = []

for sheet in response['sheets']:
    sheet['properties']['index'] = sheet_names.index(sheet['properties']['title'])
    
    requests.append({
        'updateSheetProperties': {
            'properties': sheet['properties'], 
            'fields': '*'
        }
    })

request_body = {
    'requests': requests
}

service.spreadsheets().batchUpdate(
    spreadsheetId=google_sheets_id,
    body=request_body
).execute()