-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.py
220 lines (191 loc) · 9 KB
/
defs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import datetime
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
def copiarformatacaodepresenca(service, spreadsheet_id, source_sheet_id, target_sheet_id):
source_sheet = service.spreadsheets().get(
spreadsheetId=spreadsheet_id,
ranges=[],
fields="sheets.conditionalFormats"
).execute()['sheets'][source_sheet_id].get('conditionalFormats', [])
for rule in source_sheet:
for range_ in rule['ranges']:
range_['sheetId'] = target_sheet_id
requests = [{
'addConditionalFormatRule': {
'rule': rule,
'index': 0
}
} for rule in source_sheet]
if requests:
body = {'requests': requests}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
def copiarformatacao(service, spreadsheet_id, source_sheet_id, target_sheet_id):
source_sheet = service.spreadsheets().get(
spreadsheetId=spreadsheet_id,
ranges=[],
fields="sheets.data.rowData.values.userEnteredFormat,sheets.data.rowData.values.effectiveFormat"
).execute()['sheets'][source_sheet_id].get('data', [])[0].get('rowData', [])
requests = []
for row_index, row in enumerate(source_sheet):
for col_index, cell in enumerate(row.get('values', [])):
if 'userEnteredFormat' in cell:
requests.append({
'repeatCell': {
'range': {
'sheetId': target_sheet_id,
'startRowIndex': row_index,
'endRowIndex': row_index + 1,
'startColumnIndex': col_index,
'endColumnIndex': col_index + 1
},
'cell': {
'userEnteredFormat': cell['userEnteredFormat']
},
'fields': 'userEnteredFormat'
}
})
if requests:
body = {'requests': requests}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
def copiarlarguradecoluna(service, spreadsheet_id, source_sheet_id, target_sheet_id):
source_sheet = service.spreadsheets().get(
spreadsheetId=spreadsheet_id,
ranges=[],
fields="sheets.data.columnMetadata"
).execute()['sheets'][source_sheet_id].get('data', [])[0].get('columnMetadata', [])
requests = []
for col_index, col in enumerate(source_sheet):
if 'pixelSize' in col:
requests.append({
'updateDimensionProperties': {
'range': {
'sheetId': target_sheet_id,
'dimension': 'COLUMNS',
'startIndex': col_index,
'endIndex': col_index + 1
},
'properties': {
'pixelSize': col['pixelSize']
},
'fields': 'pixelSize'
}
})
if requests:
body = {'requests': requests}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
def is_file_empty(file_path):
try:
with open(file_path, 'r') as file:
# Lê o primeiro caractere e verifica se o arquivo está vazio
first_char = file.read(1)
if not first_char:
return True # Arquivo está vazio
return False # Arquivo não está vazio
except FileNotFoundError:
print("O arquivo não foi encontrado.")
return False
def clear_file(file_path):
with open(file_path, 'w') as file:
pass
#função que atualiza a planilha do sheets. Executada todo dia meia noite somente se o arquivo 'codes.txt' não estiver vazio e limpa ele após executar
def diariamente():
if not is_file_empty('codes.txt'):
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('sheets', 'v4', credentials=creds)
spreadsheet_id = '17HqMNAT7WVbt9FsrtNz-7SfFIejBsLL9yZq7ysHFUhQ'
range_name = 'Sheet1!E2'
sheet = service.spreadsheets()
# Verificar a data atual
result = sheet.values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
values = result.get('values', [])
current_date = datetime.datetime.now().strftime("%d/%B/%Y")
# Se a data da planilha for diferente da data atual, criar uma nova aba
if not values or values[0][0] != current_date:
new_sheet_title = f"Sheet_{current_date.replace('/', '_')}"
sheet_metadata = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
sheets = sheet_metadata.get('sheets', '')
# Criar nova aba
new_sheet = {
'properties': {
'title': new_sheet_title
}
}
response = service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheet_id,
body={
'requests': [
{'addSheet': new_sheet}
]
}
).execute()
new_sheet_id = response['replies'][0]['addSheet']['properties']['sheetId']
original_sheet_id = sheet_metadata['sheets'][0]['properties']['sheetId']
# Copiar dados da aba original para a nova aba
source_range = 'Sheet1!A:E'
source_values = sheet.values().get(spreadsheetId=spreadsheet_id, range=source_range).execute().get('values', [])
if source_values:
service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id,
range=f"{new_sheet_title}!A1",
valueInputOption="RAW",
body={"values": source_values}
).execute()
# Marcar todos como ausentes na nova aba
row_count = len(source_values)
absence_values = [["Ausente"] for _ in range(1, row_count)]
service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id,
range=f"{new_sheet_title}!C2:C{row_count}",
valueInputOption="RAW",
body={"values": absence_values}
).execute()
# Atualizar a data na nova aba
service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id,
range=f"{new_sheet_title}!E2",
valueInputOption="RAW",
body={"values": [[current_date]]}
).execute()
# Copiar formatação condicional
copiarformatacaodepresenca(service, spreadsheet_id, original_sheet_id, new_sheet_id)
# Copiar formatação de células
copiarformatacao(service, spreadsheet_id, original_sheet_id, new_sheet_id)
# Copiar tamanhos das colunas
copiarlarguradecoluna(service, spreadsheet_id, original_sheet_id, new_sheet_id)
range_name = f"{new_sheet_title}!B:B"
else:
range_name = 'Sheet1!B:B'
result = sheet.values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
values = result.get('values', [])
data = current_date
with open('codes.txt', 'r') as file:
for line in file:
code = int(line.strip())
for i, row in enumerate(values):
if row and row[0] != 'NUsp' and int(row[0]) == code:
cell_range = f'{new_sheet_title}!C{i + 1}'
receber = [["Presente"]]
update_result = sheet.values().update(
spreadsheetId=spreadsheet_id,
range=cell_range,
valueInputOption="RAW",
body={"values": receber}).execute()
nome_range = f'{new_sheet_title}!A{i + 1}'
nome_result = sheet.values().get(spreadsheetId=spreadsheet_id, range=nome_range).execute()
clear_file('codes.txt')
else:
return