-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_sheets.py
402 lines (349 loc) · 17.7 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import settings
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
# We need write access to the spreadsheet: https://developers.google.com/sheets/api/guides/authorizing
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# some predefined constants that describe data structure
# need to move it out to settings
STUDENT_NAME_COLUMN = 1
LAB_COLUMN_OFFSET = 1
def colnum_string(n, zero_based=False):
string = ""
if zero_based:
n += 1
while n > 0:
n, remainder = divmod(n - 1, 26)
string = chr(65 + remainder) + string
return string
def get_spreadsheet_instance():
"""
Performs authentication and creates a service.spreadsheets() instance
:returns: service.spreadsheets() instance
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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(
settings.google_credentials_file, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds, cache_discovery=False)
# Call the Sheets API
spreadsheet = service.spreadsheets()
return spreadsheet
def get_sheet_names(spreadsheet):
"""
Get all sheet names that are present on the spreadsheet
:param spreadsheet: a service.spreadsheets() instance
:returns: list with sheet names
"""
sheets = []
result = spreadsheet.get(spreadsheetId=settings.google_spreadsheet_id).execute()
for s in result['sheets']:
sheets.append(s.get('properties', {}).get('title'))
return sheets
def get_multiple_sheets_data(spreadsheet, sheets, dimension='COLUMNS'):
"""
Get data from multiple sheets at once with a batchGet request
:param spreadsheet: a service.spreadsheets() instance
:param sheets: a list of sheet names for which the data is to be retrieved
:param dimension: passed to spreadsheet.values().batchGet as a value of majorDimension param. Possible values are 'COLUMNS' or 'ROWS'
:returns: dict with sheet name as key and data as value
"""
data = {}
request = spreadsheet.values().batchGet(spreadsheetId=settings.google_spreadsheet_id, ranges=sheets, majorDimension=dimension)
response = request.execute()
for i in range(0, len(response.get('valueRanges'))):
data[sheets[i]] = response.get('valueRanges')[i].get('values')
return data
def _find_github_column(data, student, dimension='COLUMNS'):
"""
Find GitHub column id (zero-based)
:param data: dict with sheet name as key and data as value
:param student: dict with a 'group' key
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: zero-based column number of GitHub column
:raises ValueError: if GitHub column is not found
"""
github_column = None
for i in range(0, len(data[student['group']])):
if 'GitHub' in data[student['group']][i]:
github_column = i
break
if github_column is None:
raise ValueError("Internal error! GitHub account column not found on sheet {}. Please, verify spreadsheet integrity!".format(student['group']))
return github_column
def find_student(data, student, dimension='COLUMNS', searchby='name'):
"""
Find student in data from multiple sheets. A student is described by his/her
group and either name or github account. If both student's name and
github account are present, the value of searchby param is used to determine
which one of them will be used. If student's name is missing, but github
account is present, value of searchby is ignored.
:param data: dict with sheet name as key and data as value
:param student: dict with a 'group' key and one of ('name', 'github') keys
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:param searchby: choose either 'name' or 'github' to be used for the
search if both are used to describe the student
:returns: row number if the student is found (zero based)
:raises ValueError: if either group or student are not found in data
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
if student['group'] not in data:
raise ValueError("Group '{}' not found in spreadsheet! Available groups are: {}. Check your spelling or contact course staff if you don't see your group listed.".format(student['group'], list(data.keys())))
if 'name' in student and searchby == 'name':
try:
position = data[student['group']][STUDENT_NAME_COLUMN].index(student['name'])
except ValueError:
raise ValueError("Student '{}' not found in group {}! Check spelling or contact course staff if you are not on the group list.".format(student['name'], student['group']))
elif 'github' in student:
github_column = _find_github_column(data, student, dimension)
try:
github_names = [s.lower() for s in data[student['group']][github_column]]
position = github_names.index(student['github'].lower())
# position = data[student['group']][github_column].index(student['github'])
except ValueError:
raise ValueError("Student with GitHub account {} not found in group {}! Check spelling or contact course staff if you are not on the group list.".format(student['github'], student['group']))
else:
raise ValueError("Internal error! Both name and github account info are missing from student description: {}".format(student))
return position
def find_student_by_github(data, github, dimension='COLUMNS'):
"""
Search for a student in all groups by his/her github
:param data: dict with sheet name as key and data as value
:param github: github account name to be searched for
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: student info as a dict with 'group', 'name',
'github' and 'position' keys
:raises ValueError: if student with such github account is not found in data
"""
position = None
student = {'group': None, 'github': github}
for group in data:
student['group'] = group
try:
position = find_student(data, student, searchby='github')
except ValueError as e:
position = None
pass
if position:
student['name'] = data[student['group']][STUDENT_NAME_COLUMN][position]
student['position'] = position
return student
raise ValueError("Student with GitHub account {} not found in any of the groups!".format(github))
def get_student_task_id(data, student, dimension='COLUMNS'):
"""
Find stident's task id from google spreadsheet
:param data: dict with sheet name as key and data as value
:param student: dict with at least 'name' and 'group' keys
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: task id if student is found
:raises ValueError: if either group or student are not found in data
"""
TASK_ID_COLUMN = 0
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
student_position = find_student(data, student, dimension=dimension)
task_id = data[student['group']][TASK_ID_COLUMN][student_position]
return task_id
def get_student_github(data, student, dimension='COLUMNS'):
"""
Get student's GitHub account from google spreadsheet
:param data: dict with sheet name as key and data as value
:param student: dict with at least 'name' and 'group' keys
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: GitHub account name if student is found. None if student is found, but has no account
:raises ValueError: if either group, student or GitHub column are not found in data
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
student_position = find_student(data, student, dimension=dimension)
github_column = _find_github_column(data, student, dimension)
student_github = None
try:
student_github = data[student['group']][github_column][student_position]
if len(student_github.strip()) == 0:
student_github = None
except IndexError:
student_github = None
return student_github
def get_student_lab_status(data, student, lab_id, dimension='COLUMNS'):
"""
Get student's lab status from google spreadsheet
:param data: dict with sheet name as key and data as value
:param student: dict with at least 'name' and 'group' keys
:param lab_id: integer lab identifier (starting from 1 onwards)
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: status of lab if student is found. None if student is found, but has no status for that lab
:raises ValueError: if either group or student are not found in data
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
student_position = find_student(data, student, dimension=dimension)
student_lab_status = None
try:
lab_column = LAB_COLUMN_OFFSET + lab_id
student_lab_status = data[student['group']][lab_column][student_position]
except IndexError:
student_lab_status = None
if student_lab_status == "":
student_lab_status = None
return student_lab_status
def get_lab_deadline(data, group, lab_id, dimension='COLUMNS'):
"""
Get deadline for a lab
:param data: dict with sheet name as key and data as value
:param group: group the deadline to retrieve for
:param lab_id: integer lab identifier (starting from 1 onwards)
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:returns: deadline for a given lab
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
try:
lab_column = LAB_COLUMN_OFFSET + lab_id
lab_deadline = data[group][lab_column][0]
except IndexError:
lab_deadline = None
return lab_deadline
def set_student_github(data, student, dimension='COLUMNS', data_update=[]):
"""
Set student's github account to the value specified in student param
:param data: dict with sheet name as key and data as value
:param student: dict with at least 'name', 'group' and 'github' keys
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:param data_update: a list of pending data updates prepared for
spreadsheets.values.batchUpdate request
:returns: a list of pending data update requests with appended
request to update github account for the user in question
:raises ValueError: if group or student not found, if github account
for that student is already known and is different, if github account
if already used by another student
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
# check data for validity:
# # 1. does this student already have a github account?
# student_github = get_student_github(data, student, dimension=dimension)
# if student_github is not None and student_github != student['github']:
# raise ValueError("GitHub account for student '{}' from group '{}' is '{}'. Can't set it to '{}'. Contact course staff if you want to update it.".format(student['name'], student['group'], student_github, student['github']))
# # 2. does any other student already use that github account?
other_student = None
is_new_student = True
try:
other_student = find_student_by_github(data, student['github'], dimension=dimension)
except ValueError as e:
pass
else:
# 1. does any other student already use that github account?
if other_student['group'] != student['group'] or other_student['name'] != student['name']:
raise ValueError("Can't set GitHub account for student '{}' from group '{}' to '{}'. This GitHub account is already used by student '{}' from group '{}'. Are you trying to cheat here?".format(student['name'], student['group'], other_student['github'], other_student['name'], other_student['group']))
# 2. does this student already have a github account?
elif other_student['github'] != student['github']:
# here we assume that other_student and student are the same person
raise ValueError("GitHub account for student '{}' from group '{}' is '{}'. Can't set it to '{}'. Contact course staff if you want to update it.".format(student['name'], student['group'], other_student['github'], student['github']))
# 3. If not 1 & 2, then it must be a duplicate, so ignore it
else:
# return data_update
is_new_student = False
# TODO: join validity checks 1 & 2 above to be just a single check
# ^ seems to be done!
if is_new_student:
student_position = find_student(data, student, dimension=dimension, searchby='name')
github_column = _find_github_column(data, student, dimension=dimension)
values_count = len(data[student['group']][github_column])
if values_count < student_position + 1:
data[student['group']][github_column] = [
data[student['group']][github_column][i] if i < values_count else "" for i in range(0, student_position+1)
]
data[student['group']][github_column][student_position] = student['github']
data_update.append({
'range': "{}!{}{}".format(student['group'], colnum_string(github_column, True), student_position+1),
# 'majorDimension': dimension,
'values': [[student['github']]]
})
print("Pending write operation: {} @ {}".format(data_update[-1]['values'], data_update[-1]['range']))
return data_update
def set_student_lab_status(data, student, lab_id, value, dimension='COLUMNS', data_update=[]):
"""
Set student's result for lab 'lab_id' to 'value'
:param data: dict with sheet name as key and data as value
:param student: dict with at least 'group' and one of 'name' or 'github' keys
:param lab_id: integer lab identifier (starting from 1 onwards)
:param value: string value to be set as student's lab_id result
:param dimension: how the data is stored, see spreadsheet.values().batchGet
:param data_update: a list of pending data updates prepared for
spreadsheets.values.batchUpdate request
:returns: a list of pending data update requests with appended
request to update github account for the user in question
:raises ValueError:
"""
if dimension != 'COLUMNS':
raise ValueError("Not implemented! Only 'COLUMNS' dimension value is supported at the moment.")
student_position = find_student(data, student, dimension=dimension)
lab_column = LAB_COLUMN_OFFSET + lab_id
values_count = len(data[student['group']][lab_column])
if values_count < student_position + 1:
data[student['group']][lab_column] = [
data[student['group']][lab_column][i] if i < values_count else "" for i in range(0, student_position+1)
]
# print(student)
# print(lab_column, student_position, values_count, len(data[student['group']][lab_column]))
# print(data[student['group']][lab_column])
data[student['group']][lab_column][student_position] = value
data_update.append({
'range': "{}!{}{}".format(student['group'], colnum_string(lab_column, True), student_position+1),
# 'majorDimension': dimension,
'values': [[value]]
})
print("Pending write operation: {} @ {}".format(data_update[-1]['values'], data_update[-1]['range']))
return data_update
def batch_update(spreadsheet, data_update):
"""
Performs a batchUpdate query on a spreadsheet
:param spreadsheet: a service.spreadsheets() instance
"""
body = {
'valueInputOption': "RAW",
'data': data_update
}
result = spreadsheet.values().batchUpdate(
spreadsheetId=settings.google_spreadsheet_id, body=body).execute()
print('{0} cells updated.'.format(result.get('totalUpdatedCells')))
return result.get('totalUpdatedCells')
# raise ValueError("Not implemented!")
# def stuff():
# values = result.get('values', [])
# if not values:
# print('No data found.')
# else:
# print('Name, Major:')
# for row in values:
# # Print columns A and E, which correspond to indices 0 and 4.
# print('%s, %s' % (row[0], row[4]))
#
def main():
spreadsheet = get_spreadsheet_instance()
sheets = get_sheet_names(spreadsheet)
print(sheets)
#escape sheet names:
sheets = ["'{}'".format(s) for s in sheets]
data = get_multiple_sheets_data(spreadsheet, sheets)
if __name__ == '__main__':
main()