-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_upload.py
More file actions
74 lines (61 loc) · 2.41 KB
/
google_upload.py
File metadata and controls
74 lines (61 loc) · 2.41 KB
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
#-*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
from oauth2client.service_account import ServiceAccountCredentials
import gspread
import json
if sys.version_info.major<3:
print("Python version is too low, use over python 3")
exit()
from builtins import input
if not(os.path.isfile('cfg.json')):
print("No configure file, please configure first.")
print("\"python configure.py\"")
exit();
with open('cfg.json', 'r') as cfgfile:
cfgFilename = json.load(cfgfile)["SheetFile Name"]
def GetCredentials():
credentialsjson = 'credentialKey.json'
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentialsjson, scope)
gc = gspread.authorize(credentials)
return gc
def OpenSheet(sheetname):
return GetCredentials().open(cfgFilename).worksheet(sheetname)
def getAllSheetNames():
sheetnames = list()
worksheets = GetCredentials().open(cfgFilename).worksheets()
for indsheet in worksheets:
sheetnames.append(indsheet.title)
return sheetnames
def make_sheet_with_firstrow(sheetname,firstrow):
firstrow.insert(0, "TimeStamp")
newsheet = GetCredentials().open(cfgFilename).add_worksheet(sheetname,1,len(firstrow))
firstrowcells = newsheet.range(1,1,1,len(firstrow))
for i in range(0,len(firstrow)):
firstrowcells[i].value = firstrow[i]
newsheet.update_cells(firstrowcells)
def Upload_dict(timestampstr,timestamp,location,inputdict):
sheetlist = getAllSheetNames()
for typekey in list(inputdict[list(inputdict.keys())[0]].keys()): #SUM
sheetname = location + '_' + typekey
if not sheetname in sheetlist:
make_sheet_with_firstrow(sheetname,list(inputdict.keys()))
ObjectSheet = OpenSheet(sheetname)
row_dict = dict()
for typekey2 in list(inputdict.keys()): #micrometer
row_dict[ObjectSheet.find(typekey2).col] = inputdict[typekey2][typekey]
#
row_array = [None]*max(list(row_dict.keys()))
#
for col_index in list(row_dict.keys()):
row_array[col_index-1] = row_dict[col_index]
#
row_array[ObjectSheet.find(timestampstr).col-1] = timestamp
ObjectSheet.append_row(row_array)
if __name__ == "__main__":
print("Test Google_Upload_Script")
#print(getAllSheetNames())
#print(OpenSheet('hello').range(1,1,1,5))
print("end")