-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite.py
57 lines (38 loc) · 1.38 KB
/
write.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
import gspread
import pandas as pd
from datetime import datetime
from settings import Settings
def auth():
gc = gspread.service_account(filename=Settings.AUTH_PATH)
sh = gc.open_by_key(Settings.SHEET_ID)
return sh
def status():
sh = auth()
wks = sh.worksheet("Sheet1")
if wks.acell("A1").value != "Currently updating":
sh.worksheet("Sheet1").update("A1", "Currently updating")
else:
sh.worksheet("Sheet1").update("A1", "Last update: " + datetime.strftime(datetime.utcnow(), "%x %X"))
def write(inst, data_path):
sh = auth()
# Iterate through Sheet1!A2:A, if inst is not in there, add.
wks = sh.worksheet("Sheet1")
inst_names = wks.range('A2:A999')
if inst not in [cell.value for cell in inst_names]:
# find first non-blank cell
for i, cell in enumerate(inst_names, 2):
print(cell.value, i)
if cell.value == "":
wks.update('A'+str(i), inst)
break
data = pd.read_csv(data_path)
header = data.columns.fillna('-').tolist()
data = data.fillna('-').values.tolist()
try:
wks = sh.worksheet(inst)
except:
wks = sh.add_worksheet(title=inst, rows="100", cols="20")
sh.values_clear(inst + "!A1:Z9999")
# Update a range of cells using the top left corner address
wks.update('A1', [header])
wks.update('A2', data)