Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Afegit script per a copiar un sheet a un llistat de spreadsheets #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions rondait/jornada_drive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pickle
import time
import os.path
from googleapiclient import discovery
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()