-
Notifications
You must be signed in to change notification settings - Fork 3
/
google_sheets.py
351 lines (288 loc) · 12.6 KB
/
google_sheets.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import webbrowser
from googleapiclient import discovery
from googleapiclient.errors import HttpError
from oauth2client import file, client
from oauth2client.tools import run_flow
def cell(row, column) -> str:
return chr(ord("A") + column) + str(row)
def constant_cell(row, column) -> str:
return "${}${}".format(chr(ord("A") + column), row)
class GoogleSheets:
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']
def __init__(self, spreadsheet_id, client_id='assets/client_id.json', storage='assets/storage.json',
flags=None) -> None:
super().__init__()
self.flags = flags
self.spreadsheet_id = spreadsheet_id
self.storage = storage
self.client_id = client_id
self.credentials = self.get_credentials()
self.service = self.get_service()
self.green = self.get_color(183 / 255, 225 / 255, 205 / 255)
self.red = self.get_color(244 / 255, 199 / 255, 195 / 255)
self.orange = self.get_color(252 / 255, 232 / 255, 178 / 255)
self.clean_up_sheet()
def get_credentials(self):
store = file.Storage(self.storage)
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets(self.client_id, self.SCOPES)
creds = run_flow(flow, store, flags=self.flags)
return creds
def get_service(self):
return discovery.build('sheets', 'v4', credentials=self.credentials)
def update_value(self, values: list, range_: str = None,
value_input_option='USER_ENTERED'):
value_range_body = {
"values":
values
}
if range_ is None:
range_ = self.get_string_range(values)
request = self.service.spreadsheets().values().update(spreadsheetId=self.spreadsheet_id, range=range_,
valueInputOption=value_input_option,
body=value_range_body)
return request.execute()
def send_to_google_sheets(self, formatter):
response = self.update_value(formatter.values)
# TODO use the formulas above to make it even more dynamic for dates
self.clean_up_sheet()
# print(formatter.check_date_row, formatter.check_date_column)
self.format_sheet(formatter.outreach_cell, formatter.participation_cell, formatter.check_column_index,
formatter.outreach_column, formatter.participation_column,
formatter.check_date_row, formatter.check_date_column, formatter.offset)
def format_sheet(self, outreach_cell, participation_cell, check_column_index,
outreach_column,
participation_column, check_date_row, check_date_column, calculated_offset=3):
reqs = self.formatting(outreach_cell, participation_cell, check_column_index, outreach_column,
participation_column,
check_date_row, check_date_column,
filter_stop_column=check_column_index + calculated_offset,
offset=calculated_offset)
return self.run_batch_update(reqs)
def clean_up_sheet(self):
reqs = self.clean_up_formatting()
try:
res = self.run_batch_update(reqs)
return True
except HttpError:
return False
def formatting(self, outreach_cell, participation_cell, check_column, outreach_column, participation_column,
check_date_row,
check_date_column,
filter_stop_column=0, offset=3):
equation = '=AND(${1}1<{0},NOT(${1}1=""))'
outreach_column_name = chr(ord('A') + outreach_column)
participation_column_name = chr(ord('A') + participation_column)
check_column_c = check_column + offset
outreach_column_c, participation_column_c = outreach_column + offset, participation_column + offset
outreach_column_name_c = chr(ord('A') + outreach_column_c)
participation_column_name_c = chr(ord('A') + participation_column_c)
# print(outreach_cell, participation_cell, outreach_column_name_c, check_column, outreach_column,
# participation_column)
reqs = {'requests': [
self.__add_freeze_row(),
self.__add_freeze_col(),
self.__add_bold_row(),
self.__add_text_condition("TEXT_CONTAINS", "GOOD", self.green, check_column),
self.__add_text_condition("TEXT_CONTAINS", "PARTICIPATION", self.orange, check_column),
self.__add_text_condition("TEXT_CONTAINS", "OUTREACH", self.orange, check_column),
self.__add_text_condition("TEXT_CONTAINS", "BOTH", self.red, check_column),
self.__add_filter(filter_stop_column),
self.__add_custom_condition(equation.format(outreach_cell, outreach_column_name), self.red,
outreach_column),
self.__add_custom_condition(equation.format(participation_cell, participation_column_name), self.orange,
participation_column),
self.__add_text_condition("TEXT_CONTAINS", "GOOD", self.green, check_column_c),
self.__add_text_condition("TEXT_CONTAINS", "PARTICIPATION", self.orange, check_column_c),
self.__add_text_condition("TEXT_CONTAINS", "OUTREACH", self.orange, check_column_c),
self.__add_text_condition("TEXT_CONTAINS", "BOTH", self.red, check_column_c),
self.__add_custom_condition(equation.format(outreach_cell, outreach_column_name_c), self.red,
outreach_column_c),
self.__add_custom_condition(equation.format(participation_cell, participation_column_name_c), self.orange,
participation_column_c),
self.__add_date_format(check_date_row, check_date_column)
]}
return reqs
@staticmethod
def get_string_range(data_list: list, start=None):
rows = len(data_list)
cols = len(data_list[0])
if start is None:
start = cell(1, 0)
end = cell(int(start[1]) + rows - 1, ord(start[0]) - ord('A') + cols - 1)
return "{}:{}".format(start, end)
def retrieve_sheet_data(self, ranges, value_render_option="UNFORMATTED_VALUE",
date_time_render_option="FORMATTED_STRING"):
request = self.service.spreadsheets().values().batchGet(spreadsheetId=self.spreadsheet_id, ranges=ranges,
valueRenderOption=value_render_option,
dateTimeRenderOption=date_time_render_option)
return request.execute()
def run_batch_update(self, reqs):
return self.service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheet_id, body=reqs).execute()
def clean_up_formatting(self):
request = self.service.spreadsheets().get(spreadsheetId=self.spreadsheet_id, includeGridData=False)
response = request.execute()
number_of_conditionals = 0
if "conditionalFormats" in response["sheets"][0]:
number_of_conditionals = len(response["sheets"][0]['conditionalFormats'])
return {
"requests": [
*self.__delete_previous_conditions(number_of_conditionals)
]
}
@staticmethod
def __delete_previous_conditions(number_of_conditionals) -> list:
return [
{
"deleteConditionalFormatRule": {
"index": 0,
}
}
for _ in range(number_of_conditionals)
]
@staticmethod
def __add_filter(filter_column):
return {"setBasicFilter": {
"filter": {
# "title": title,
# "filterViewId": 10,
"range": {
# "startColumnIndex": filter_column,
"endColumnIndex": filter_column + 1
# TODO make it so that a column range can be set, right now if there is a filter it will be replaced
},
"sortSpecs": [
# {
# "dimensionIndex": filter_column,
# "sortOrder": "ASCENDING"
# }
],
"criteria": {
0: {"hiddenValues": [
""
]}
}
}
}}
@staticmethod
def __add_date_format(check_date_row, check_date_column):
return {
"repeatCell": {
"range": {
"startRowIndex": check_date_row,
"endRowIndex": check_date_row + 1,
"startColumnIndex": check_date_column,
"endColumnIndex": check_date_column + 1
},
"cell": {
"userEnteredFormat": {
"numberFormat": {
"type": "DATE",
}
}
},
"fields": "userEnteredFormat.numberFormat"
}
}
@staticmethod
def __add_bold_row(start_row=0, end_row=1):
return {'repeatCell': {
'range': {
'startRowIndex': start_row,
'endRowIndex': end_row
},
'cell':
{'userEnteredFormat':
{'textFormat':
{'bold': True
}
}
},
'fields': 'userEnteredFormat.textFormat.bold',
}}
@staticmethod
def __add_freeze_row(number_of_rows=1):
return {'updateSheetProperties': {
'properties':
{'gridProperties':
{'frozenRowCount': number_of_rows},
},
'fields': 'gridProperties.frozenRowCount'
}}
@staticmethod
def __add_freeze_col(number_of_columns=1):
return {'updateSheetProperties': {
'properties':
{'gridProperties':
{'frozenColumnCount': number_of_columns},
},
'fields': 'gridProperties.frozenColumnCount'
}}
@staticmethod
def __add_text_condition(conditional, value, color: dict, check_column, index=0):
return {"addConditionalFormatRule": {
"rule": {
"ranges": [
{
"startColumnIndex": check_column,
"endColumnIndex": check_column + 1,
}
],
"booleanRule": {
"condition": {
"type": conditional,
"values": [
{
"userEnteredValue": value
}
]
},
"format": color
}
},
"index": index
}}
@staticmethod
def get_color(red, green, blue):
return {
"backgroundColor": {
"blue": blue,
"green": green,
"red": red
}
}
@staticmethod
def __add_custom_condition(equation, color, column, index=0):
return {"addConditionalFormatRule": {
"rule": {
"ranges": [
{
"startColumnIndex": column,
"endColumnIndex": column + 1,
}
],
"booleanRule": {
"condition": {
"type": "CUSTOM_FORMULA",
"values": [
{
"userEnteredValue": equation
}
]
},
"format": color
}
},
"index": index
}}
def open_spreadsheet(self):
url = 'https://docs.google.com/spreadsheets/d/{}'.format(self.spreadsheet_id)
# MacOS
# chrome_path = 'open -a /Applications/Google\ Chrome.app %s'
# Windows
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# Linux
# chrome_path = '/usr/bin/google-chrome %s'
webbrowser.get(chrome_path).open(url)