-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
318 lines (274 loc) · 8.33 KB
/
utils.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
import gspread
from apiclient.discovery import build
from gspread.models import Spreadsheet
from gspread_dataframe import set_with_dataframe
from gspread_formatting import (
set_column_width,
set_row_height,
set_frozen,
cellFormat,
format_cell_ranges,
ConditionalFormatRule,
GradientRule,
CellFormat,
get_conditional_format_rules,
GridRange,
textFormat,
Color,
GradientRule,
InterpolationPoint,
)
import json
import os
import time
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
]
WRITE_THRESHOLD = 1000
# colors
BLUE = {
"red": 0.41,
"green": 0.58,
"blue": 0.93,
}
RED = {
"red": 0.96,
"green": 0.78,
"blue": 0.77,
}
WHITE = {"green": 1.0, "red": 1.0, "blue": 1.0}
GREY = {"green": 0.41, "red": 0.41, "blue": 0.41}
GREEN = {"green": 0.73, "red": 0.34, "blue": 0.54}
COLOR_MAP = {'green': GREEN, "grey": GREY, "white": WHITE, "red": RED, "blue": BLUE}
def get_credentials(scopes=SCOPES, credential_password_env_var_name='GSHEET_PASSWORD'):
"""Read Google's JSON permission file.
https://developers.google.com/api-client-library/python/auth/service-accounts#example
:param scopes: List of scopes we need access to
"""
cred_str = os.getenv(credential_password_env_var_name)
cred_str = cred_str.replace("'", '"')
cred_json = json.loads(cred_str, strict=False)
credentials = ServiceAccountCredentials.from_json_keyfile_dict(cred_json, SCOPES)
gc = gspread.authorize(credentials)
return credentials, gc
def grant_permissions(
sheet_id,
service_account_email,
credential_password_env_var_name,
share_domains=[],
email_message="",
send_notification=False,
drive_api=None,
):
if drive_api is None:
credentials, _ = get_credentials(SCOPES, credential_password_env_var_name)
drive_api = build('drive', 'v3', credentials=credentials)
share_domains = list(set(share_domains + [service_account_email]))
for domain in share_domains:
domain_permission = {
'type': 'user',
'role': 'writer',
'emailAddress': domain,
}
# exclude email message if we aren't sending notifications
if not email_message or not send_notification:
req = drive_api.permissions().create(
fileId=sheet_id,
body=domain_permission,
fields="id",
sendNotificationEmail=send_notification,
)
else:
req = drive_api.permissions().create(
fileId=sheet_id,
body=domain_permission,
emailMessage=email_message,
fields="id",
sendNotificationEmail=send_notification,
)
req.execute()
def create_google_spreadsheet(
title,
service_account_email,
credential_password_env_var_name,
email_message=None,
send_notification=False,
share_domains=None,
):
"""Create a new spreadsheet and open gspread object for it.
.. note ::
Created spreadsheet is not instantly visible in your Drive search and you need to access it by direct link.
:param title: Spreadsheet title
:param share_domains: List of Google Apps domain whose members get full access rights to the created sheet. Very handy, otherwise the file is visible only to the service worker itself. Example:: ``["redinnovation.com"]``.
"""
credentials, gc = get_credentials(credential_password_env_var_name)
drive_api = build('drive', 'v3', credentials=credentials)
body = {
'name': title,
'mimeType': 'application/vnd.google-apps.spreadsheet',
}
req = drive_api.files().create(body=body)
new_sheet = req.execute()
# Get id of fresh sheet
sheet_id = new_sheet["id"]
grant_permissions(
sheet_id,
service_account_email,
credential_password_env_var_name,
share_domains,
email_message,
send_notification=True,
drive_api=drive_api,
)
link = f'\nhttps://docs.google.com/spreadsheets/d/{sheet_id}'
return sheet_id, gc, link
def clear_sheet1(sh):
# delete sheet1 if it exists
try:
wk = sh.worksheet('Sheet1')
sh.del_worksheet(wk)
except Exception as e:
pass
def clear_worksheet(sh, title):
try:
worksheet = sh.add_worksheet(title=title, rows='500', cols='20')
except:
wk = sh.worksheet(title)
wk.clear()
sh.del_worksheet(wk)
worksheet = sh.add_worksheet(title=title, rows='500', cols='20')
def conditional_formatter(wk, df, cols_to_colors, header_letters):
rules = get_conditional_format_rules(wk)
rules.clear()
if cols_to_colors:
for idx, col in enumerate(df.columns):
if col in cols_to_colors:
color = COLOR_MAP[cols_to_colors[col]]
else:
color = WHITE
rule = ConditionalFormatRule(
ranges=[
GridRange.from_a1_range(f'{header_letters[idx]}1:{header_letters[idx]}2000', wk)
],
gradientRule=GradientRule(
minpoint=InterpolationPoint(color=WHITE, type="MIN"),
maxpoint=InterpolationPoint(color=color, type="MAX"),
),
)
rules.append(rule)
rules.save()
def write_to_sheet(
gc,
sheet_id,
worksheet_name,
df,
column_width_dict=None,
header_row_height=None,
no_borders=False,
percent_cols=[],
wrap_cols=[],
cols_to_colors={},
):
sh = gc.open_by_key(sheet_id)
# may fail if sheet isn't created
try:
clear_worksheet(sh, worksheet_name)
except Exception as _:
pass
wk = sh.worksheet(worksheet_name)
header_letters = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
]
column_ids = header_letters.copy()
# 26**2 columns max in spreadsheet
for letter_A in header_letters:
for letter_B in header_letters:
column_ids.append(letter_A + letter_B)
# writes df to sheet
set_with_dataframe(wk, df)
header_letter = column_ids[df.shape[1] - 1]
# set all font to 11
format_obj = {
"textFormat": {"fontSize": 11},
}
# freeze the header layer
set_frozen(wk, rows=1)
# remove borders
if no_borders:
format_obj['borders'] = {
"top": {"style": "NONE", "width": 0},
"bottom": {"style": "NONE", "width": 0},
"left": {"style": "NONE", "width": 0},
"right": {"style": "NONE", "width": 0},
}
wk.format(f'A1:Z1000', format_obj)
# set cols as percent cols
for col in percent_cols:
wk.format(
f'{header_letters[col]}1:{header_letters[col]}2000',
{'numberFormat': {'pattern': '0.0%', 'type': "PERCENT"}},
)
# writes a header
wk.format(
f'A1:{header_letter}1',
{
"backgroundColor": {"red": 0.95, "green": 0.95, "blue": 0.95},
"horizontalAlignment": "CENTER",
"textFormat": {"fontSize": 12, "bold": True},
"wrapStrategy": "WRAP",
},
)
# wrap columns
for col in wrap_cols:
wk.format(
f'{header_letters[col]}1:{header_letters[col]}2000',
{
"wrapStrategy": "WRAP",
},
)
# column widths
for idx in range(df.shape[1]):
if idx % WRITE_THRESHOLD == 0:
time.sleep(2)
if column_width_dict is not None and idx in column_width_dict:
set_column_width(wk, column_ids[idx], column_width_dict[idx])
else:
set_column_width(wk, column_ids[idx], 150)
time.sleep(2)
# row heights
if header_row_height:
set_row_height(wk, '1', header_row_height)
conditional_formatter(wk, df, cols_to_colors, header_letters)
# clear sheet1 if it exists
try:
clear_sheet1(sh)
except Exception as _:
pass