-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspreadsheet.py
99 lines (75 loc) · 2.7 KB
/
spreadsheet.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
import gspread
from settings import rounds, advanced
from time import strftime
from oauth2client.service_account import ServiceAccountCredentials
# Tutorial and documentation for later
# https://www.youtube.com/watch?v=vISRn5qFrkM
# https://media.readthedocs.org/pdf/gspread/latest/gspread.pdf
# Authenticate and access Google Spreadsheets
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Open the spreadsheet
if advanced:
open = client.open("finn-property-analyser-advanced")
map1_cell = 'J1'
map2_cell = 'K1'
upd1_cell = 'J2'
upd2_cell = 'K2'
rnd1_cell = 'J3'
rnd2_cell = 'K3'
else:
open = client.open("finn-property-analyser")
map1_cell = 'F1'
map2_cell = 'G1'
upd1_cell = 'F2'
upd2_cell = 'G2'
rnd1_cell = 'F3'
rnd2_cell = 'G3'
# Get total number of sheets
def get_number_of_sheets():
num_sheets = 0
# Login to make sure the token is refreshed
client.login()
while True:
# Get current worksheet
current_sheet = open.get_worksheet(num_sheets)
# Check if sheet exists
if current_sheet == None:
return num_sheets
# Add to counter
num_sheets += 1
# Get map url
def get_map_url(sheet_number):
# Login to make sure the token is refreshed
client.login()
current_sheet = open.get_worksheet(sheet_number)
current_map = current_sheet.acell(map2_cell).value
print('Initializing property extraction for area: ' + current_sheet.title)
return current_map
# Insert extracted properties into spreadsheet
def insert_data(sheet_number, data):
try:
# Login to make sure the token is refreshed
client.login()
# Get current sheet
current_sheet = open.get_worksheet(sheet_number)
print('Opening sheet named: ' + current_sheet.title)
# Delete existing data
r = current_sheet.resize(rows=1)
print('All previous entries deleted!')
# Get the map url
current_map = current_sheet.acell(map2_cell).value
print('Current map url: ' + current_map)
# Insert all data into sheet
for x in range(len(data)):
current_sheet.insert_row(data[x], 2)
# Get current time
upd_time = strftime("%d-%m-%Y %H:%M:%S")
# Re-add map url and time data
current_sheet.update_acell(upd1_cell, 'Latest update')
current_sheet.update_acell(upd2_cell, upd_time)
current_sheet.update_acell(rnd1_cell, 'Max rounds')
current_sheet.update_acell(rnd2_cell, rounds)
except:
print('ISSUE: Inserting data into spreadsheet.')