From 0b948da61f7bb4fe7c1e3afe25e2f878b860d051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Su=C3=A1rez?= Date: Mon, 25 Jan 2021 11:07:10 +0100 Subject: [PATCH 1/2] added script --- rondait/jornada_drive.py | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 rondait/jornada_drive.py diff --git a/rondait/jornada_drive.py b/rondait/jornada_drive.py new file mode 100755 index 0000000..5ee6f0a --- /dev/null +++ b/rondait/jornada_drive.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import pickle +import time +import os.path +from googleapiclient.discovery import build +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request +from tqdm import tqdm + +# Permisos +SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; + +# Copia una nova 'sheet' a cada un dels spreadsheets de la llista +class ModificacioJornades(): + + def _batch(self, spreadsheetId, requests): + body = { + 'requests': requests + } + return self.service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute() + + def renameSheet(self, spreadsheetId, sheetId, newName): + return self._batch(spreadsheetId, { + "updateSheetProperties": { + "properties": { + "sheetId": sheetId, + "title": newName, + }, + "fields": "title", + } + }) + + def __init__(self): + + credentials = None + + if os.path.exists('token.pickle'): + with open('token.pickle', 'rb') as token: + credentials = pickle.load(token) + + if not credentials or not credentials.valid: + if credentials and credentials.expired and credentials.refresh_token: + credentials.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', SCOPES) + credentials = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('token.pickle', 'wb') as token: + pickle.dump(credentials, token) + + self.service = discovery.build('sheets', 'v4', credentials=credentials) + + # Ids de la original que volem copiar + spreadsheet_id = '' + sheet_id = '' + + # Llistat de id's de les spreadsheets destí + dest_spreadsheet_list_ids = [] + + for dest_spreadsheet in tqdm(dest_spreadsheet_list_ids): + try: + copy_sheet_to_another_spreadsheet_request_body = { + 'destination_spreadsheet_id': dest_spreadsheet + } + request = self.service.spreadsheets().sheets().copyTo( + spreadsheetId=spreadsheet_id, + sheetId=sheet_id, + body=copy_sheet_to_another_spreadsheet_request_body) + response = request.execute() + new_sheet_id = response['sheetId'] + self.renameSheet(dest_spreadsheet, new_sheet_id, 'Nom pestany') + time.sleep(5) # Per no excedir la quota de crides per minut + + except Exception as e: + print("Error amb dest_spreadsheet {}".format(dest_spreadsheet), e) + +if __name__ == '__main__': + ModificacioJornades() From af40a43ee1bb2b0760ae4a7512f43b1e2d454efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Su=C3=A1rez?= Date: Mon, 25 Jan 2021 11:12:43 +0100 Subject: [PATCH 2/2] import mistake --- rondait/jornada_drive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rondait/jornada_drive.py b/rondait/jornada_drive.py index 5ee6f0a..faaec56 100755 --- a/rondait/jornada_drive.py +++ b/rondait/jornada_drive.py @@ -3,7 +3,7 @@ import pickle import time import os.path -from googleapiclient.discovery import build +from googleapiclient import discovery from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from tqdm import tqdm