-
Notifications
You must be signed in to change notification settings - Fork 0
/
gspread_button_update.py
67 lines (52 loc) · 2.06 KB
/
gspread_button_update.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
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
import pytz
"""Code written by Asher Mouat"""
# Necessary credentials and sheet identification for authenticating
# and reading/writing
JSON_KEYFILE = 'Replace_This_JSON.json'
SHEET_ID = 'SHEETID_YOU_MAKE'
scope = ['https://www.googleapis.com/auth/spreadsheets']
# Dictionary holding key-value pairs of serial number from button and action
# each button is assigned
buttons = {'XXXserialnumeberXXX' : "Deep Interaction", 'XXXserialnumeberXXX' : "Quick Interaction"}
def lambda_main(serial_number):
# Get time stamp for when button was pressed
# Check python's doc on pytz and datetime for more info
utc_time = datetime.utcnow()
eastern = pytz.timezone('US/Eastern')
utc_time = utc_time.replace(tzinfo=pytz.UTC)
time_stamp = utc_time.astimezone(eastern)
# Authenticationm, made very easy by gspread: https://github.com/burnash/gspread
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEYFILE, scope)
gc = gspread.authorize(credentials)
# Accessing sheet object, which I believe is a 2D list
gsheet = gc.open_by_key(SHEET_ID).sheet1
# Basic list containing str representations of data
col_time = gsheet.col_values(1)
#
# for cell in range(len(col_time)):
# print(col_time[cell])
# Find next blank cell, remember coordinate of cell
cell_coord = None
for cell in range(len(col_time)):
if (col_time[cell] == ''):
cell_coord = 'A' + str(cell+1)
print(cell_coord)
break
# Update relevant cells with necessary info
gsheet.update_acell(cell_coord, time_stamp)
action_cell_coord = 'B' + cell_coord[1:]
print(action_cell_coord)
gsheet.update_acell(action_cell_coord, buttons[serial_number])
"""lambda_handler will activate when button is pressed. Button sends JSON payload that looks like
{
"serialNumber" : "ABCDEFG12345",
"batteryVoltage" : "2000mV",
"clickType" : "SINGLE"
}
Payload is indexed the same way as a python dictionary
"""
def lambda_handler(context, event):
lambda_main(context['serialNumber'])