-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_helper.py
71 lines (52 loc) · 1.96 KB
/
sheet_helper.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
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ['https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets']
def find_url_col(header_col):
i = 1
for col in header_col:
if col == 'url':
break
i += 1
return i
def url_col_to_char(url_col):
return chr(64+url_col)
def add_event_row(sheet, sheet_id, sheet_name, row_idx, url_col, date, title, url):
try:
range_name = "{}!A{}:{}{}".format(sheet_name, row_idx, url_col_to_char(url_col), row_idx)
row = [date, title]
for i in range(3, url_col):
row.append('')
row.append(url)
body = {
'values': [row]
}
result = sheet.values().update(
spreadsheetId=sheet_id, range=range_name,
valueInputOption='USER_ENTERED', body=body).execute()
print(f"{result.get('updatedCells')} cells updated.")
return result
except HttpError as error:
print(f"An error occurred: {error}")
return error
def init_sheet():
token_file = os.path.join(os.getcwd(), './token.json')
creds = service_account.Credentials.from_service_account_file(token_file, scopes=SCOPES)
try:
service = build('sheets', 'v4', credentials=creds)
return service.spreadsheets()
except HttpError as err:
print(err)
def add_event(sheet, sheet_id, sheet_name, date, title, url):
try:
result = sheet.values().get(spreadsheetId=sheet_id,
range='{}!A1:M'.format(sheet_name)).execute()
values = result.get('values', [])
url_col = find_url_col(values[0])
row_idx = len(values) + 1
add_event_row(sheet, sheet_id, sheet_name, row_idx, url_col, date, title, url)
except HttpError as err:
print(err)