-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoogle_sheets.py
84 lines (73 loc) · 3.11 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#Ricardos.geral@gmail.com
import pygsheets
import pandas as pd
# get signed credentials
try:
gc = pygsheets.authorize(service_file="service_creds.json") # the file must be updated by user
initial_rows = 1000 # default number of rows of the worksheet
initial_colmn = 15 # default number of columns
google_creds = True
print('Google signed credentials are OK')
except:
print('Problem: Google signed credentials')
google_creds = False
def spreadsheet_worksheet(ssheet_title, wsheet_title, share_email):
# get spredsheet instance
list_of_ssheets = gc.spreadsheet_titles() # gets all the available sheets titles in the account of the service_creds.jon
ssheet_exists = False
for ssheet in list_of_ssheets:
if ssheet == ssheet_title:
ssheet_exists = True
if ssheet_exists == True: # if sheet exists
sh = gc.open(ssheet_title) # open it
else: # if sheet does not exists
sh = gc.create(ssheet_title) # create it
sh.share(share_email, role='writer') # share it to the provided email
wsheet_exists = False
try:
sh.worksheets(sheet_property='title',value= wsheet_title)
wsheet_exists = True
except:
pass
if wsheet_exists == True:
wks = sh.worksheet(property='title', value= wsheet_title)
wks.clear()
else:
wks = sh.add_worksheet(title=wsheet_title, rows=str(initial_rows), cols=str(initial_colmn))
#cell instance
a1 = wks.cell('A1')
a1.text_format['bold'] = True # set headers to bold
a1.update()
# Getting a Range object
rng = wks.get_values('A1', 'I1', returnas='range')
rng.apply_format(a1) # set format of a1 to all cells in the range rng
return wks
def write_gsh(data, row, wks):
global initial_rows
fieldnames = ['date', 'time',
# 'v_up', 'v_int', 'v_down',
# 'bar_up', 'bar_int', 'bar_down',
'mmH2O_up', 'mmH2O_int', 'mmH2O_down',
'ana_turb', 'turb', 'flow', 'liters',
'water_temp' #, 'air_temp', 'air_pressure', 'air_humidity'
]
df = pd.DataFrame(columns=fieldnames)
# Create a row
df.loc[-1]=[data['date'], data['time'],
#data['v_up'], data['v_int'], data['v_down'],
#data['bar_up'], data['bar_int'], data['bar_down'],
data['mmH2O_up'], data['mmH2O_int'], data['mmH2O_down'],
data['ana_turb'], data['turb'], data['flow'], data['liters'],
data['water_temp'] #, data['air_temp'], data['air_pressure'], data['air_humidity]'
]
df.index = df.index + 1
#update the first sheet with df
if row == 1:
wks.set_dataframe(df,(row,1),copy_head=True)
else:
wks.set_dataframe(df, (row + 1, 1), copy_head=False)
if row == initial_rows-3: # when max number of rows is being reached
wks.add_rows(1000) #adds another 1000 rows
initial_rows += 1000 #new number of rows