/
sheet.py
276 lines (263 loc) · 7.73 KB
/
sheet.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
import json
import gspread #sudo pip install gspread
from oauth2client.client import SignedJwtAssertionCredentials #sudo pip install python-gflags oauth2client
from multiprocessing import Pool
import re
import time
import os
import codecs, json
import stat
# Remove file
def remove_file(filename):
if os.path.exists(filename):
os.remove(filename)
else:
print filename, 'does not exists.'
# Create a file
def create_file(filename):
return open(filename, 'w')
# Write file into local directory
def write_append(filename, word):
with open(filename, "a") as myfile:
myfile.write(word + '\n')
# myfile.close()
# Read file into local directory and return the content
def read_file(filename):
data = open(filename, "r")
return data.readlines()
# Parse data and split ':' to return list
def return_list(data):
lst = []
for i in data:
i = i.strip()
if i:
if re.search(':', i):
i = i.split(':')
i0 = i[0]
i1 = i[-1]
i1 = i1.strip()
i = (i0, i1)
lst.append(i)
return lst
# Column i.e A1, B1, C1, A2..A3
def updateSheet_by_column(sheet, worksheetName, column, text):
try:
worksheet = sheet.worksheet(worksheetName)
worksheet.update_acell(column, text)
except:
pass
# Row x to Col y
def updateSheet_x_y(sheet, worksheetName, row, col, text):
try:
worksheet = sheet.worksheet(worksheetName)
worksheet.update_cell(row, col, text)
except:
pass
def updateSheet_x_y2(parameter):
sheet, worksheetName, row, col, text = parameter
try:
worksheet = sheet.worksheet(worksheetName)
worksheet.update_cell(row, col, text)
except:
pass
# Delete worksheet
# Not verified, need to configure permission access
def del_worksheet(sheet, worksheetName):
sheet.del_worksheet(worksheetName)
# Add new worksheet
# tested
def add_worksheet(sheet, worksheetName, rows, cols):
sheet.add_worksheet(worksheetName, rows, cols)
# Select worksheet by name
# test if found or not
# tested
def sel_wrksht_by_name(sheet, worksheetName):
try: # make a try-catch to filter worksheet if not found
worksheet = sheet.worksheet(worksheetName)
return worksheet
except:
print 'Work sheet', worksheetName, 'not found.'
# Get list of worksheet name
# Tested
def get_all_worksheet_list(sheet):
lst = sheet.worksheets() # resturn all class values of worksheet
myL = []
for i in lst:
i=str(i) # convert to string
i=i.split("'") # separate the data into single quote "'"
wrksht_name = i[1] # Get the data at index 1
if wrksht_name: # If not empty
myL.append(wrksht_name) # Append worksheet name into list myL
return myL
# Get cell value by Column ie. A1, B1, C1, A2, A3....
def get_cel_value_by_col(sheet, worksheetName,column):
worksheet = sheet.worksheet(worksheetName)
worksheet = worksheet.acell(column).value
return worksheet # Return the data value
# Get cell value by Coordinates
# Tested
def get_cel_value_by_coord(sheet, worksheetName,column, row):
worksheet = sheet.worksheet(worksheetName)
worksheet = worksheet.cell(row, column).value
return worksheet # Return the data value
#Get all values under column specified in variable number i.e headers
# Tested
def get_column_data(sheet, worksheetName, number):
worksheet = sheet.worksheet(worksheetName)
worksheet = worksheet.row_values(number)
if not worksheet[0]: # test if index zero is null
worksheet = set(worksheet)
return worksheet
# Get Permission from the G-Spreadsheet
# Tested
def access_sheet(key):
json_key = json.load(open('API Project-4af8ab99186b.json'))
sheet = ['https://spreadsheets.google.com/feeds']
sheet = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], sheet)
sheet = gspread.authorize(sheet)
sheet = sheet.open_by_key(key)
return sheet
# looking for an empty row to update the new data
def find_empty_row(sheet, worksheetName):
cnt = 1
while True:
headers = get_column_data(sheet, worksheetName, cnt)
if len(headers) == 1:
return cnt
break
cnt += 1
# def update_sheet(sheet, worksheetName, listWorksheet_val):
# def update_sheet(sheet, listWorksheet_val):
def update_sheet(parameter):
# sheet, worksheetName, listWorksheet_val = parameter
# delay, sheet, listWorksheet_val = parameter
# time.sleep(delay)
liquidate = []
# listWorksheet_val = parameter
sheet, listWorksheet_val = parameter
# key = "1M3qsO6IkPoVFBHYkqVgDlC18KQmeP9RaU3TbGuFEqk0"
# sheet = access_sheet(key)
mySheet = listWorksheet_val['worksheet'] # Get the worksheet 'cPersonalDetails'
# headers = get_column_data(sheet, worksheetName, 1)
headers = get_column_data(sheet, mySheet, 1)
x = find_empty_row(sheet, mySheet) # Find rows that are vacant/empty cells
liquidate = read_file('record')
try:
liquidate = return_list(liquidate)
except:
pass
tup = mySheet + ':' + str(x)
tup_ = (mySheet, str(x))
cnt = 0
for i in liquidate:
if i == tup_:
cnt += 1
tup = mySheet + ':' + str(x)
write_append('record', tup)
if cnt != 0:
x += cnt
y = 1 # initial column 1
for i in headers: # iterate headers
text = listWorksheet_val[i] # write the value
# print type(text)
if re.search('str', str(type(text))) or re.search('unicode', str(type(text))): # if text type is string
updateSheet_x_y(sheet, mySheet, x, y, text) #start to update at worksheet cPersonalDetails
else:
cnt = 0
for z in text:
newX = x + cnt
cnt += 1
updateSheet_x_y(sheet, mySheet, newX, y, z) #start to update at worksheet cPersonalDetails
y += 1
def div_data(parameter):
data = []
data2 = []
data3 = []
cnt = 0
for i in parameter:
if cnt % 2 == 0:
data2.append(i)
elif cnt % 3 == 0:
data3.append(i)
else:
data.append(i)
cnt += 1
return [ data, data2, data3 ]
def to(parameter):
P = Pool(processes=8)
sheet, data = parameter
jobs = [(sheet, worksheet) for worksheet in data]
P.map(update_sheet, jobs)
# Get current time at the server
# Return the current time: hr-min-sec, hr
def mytime():
from datetime import datetime
getCurTime = datetime.now().strftime('%H:%M:%S')
getCurTime = getCurTime.strip()
mytime = getCurTime.split(':')
return getCurTime, mytime[0] + ':' + mytime[1]
# return getCurTime, mytime[0]
# Given the hr:min:sec then return hr:min
def getTime_w_o_sec(time):
time = str(time)
time = time.strip()
time = time.split(':')
time_sec = time[1]
time = time[0]
return time + ':' + time_sec
# return time
# Given the hr:min then return hr
def getHr(time):
time = str(time)
time = time.strip()
time = time.split(':')
# time_sec = time[1]
time = time[0]
return time
# Get current Day at the server
def getDay():
import datetime
today = datetime.date.today()
# today = today.strip()
return today
# Get the current day and the expiration date
def calc_duration(number):
import datetime
today = getDay()
duration = datetime.timedelta(days=number)
END = today + duration
return END
# Update Json file but not sure if this can be use now
def write_json(filename, data):
import codecs
with codecs.open(filename, 'a', 'utf8') as f:
f.write(json.dumps(data, f, sort_keys = True, ensure_ascii=False))
f.close()
# Read Json file and return as list
def read_json(filename):
with open(filename) as json_file:
json_data = json.load(json_file)
return list(json_data)
# Initial file and make it writable
def init_json(filename):
open(filename, 'w').close()
st = os.stat(filename)
os.chmod(filename, st.st_mode | 0o111)
# Reading JSON file and append/update attribute accordingly
def update_json(filename, attribute):
# data={}
try:
with open(filename, "r") as jsonFile:
data = json.load(jsonFile)
except:
data = {}
len_ = str(len(data.keys()))
data[len_] = attribute
print data
with open(filename, "w") as jsonFile:
jsonFile.write(json.dumps(data))
# Reading JSON file and return keys and values accordingly
def get_json_key_val(filename):
with open(filename, "r") as jsonFile:
data = json.load(jsonFile)
return data.keys(), data.values()