-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_sheet_functions.py
77 lines (63 loc) · 2.84 KB
/
google_sheet_functions.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
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = [ "https://www.googleapis.com/auth/spreadsheets" ]
def get_credentials(function):
def wrapper(*args):
credentials = None
if os.path.exists("token.json"):
credentials = Credentials.from_authorized_user_file("token.json", SCOPES)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
credentials = flow.run_local_server(port=0)
with open("token.json", 'w') as token:
token.write(credentials.to_json())
function(*args, credentials)
return wrapper
@get_credentials
def create_sheet(spreadsheet_id, title, credentials):
try:
service = build("sheets", "v4", credentials=credentials)
request_body = { "requests": [{ "addSheet": { "properties": { "title": title }}}]}
result = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=request_body).execute()
return result
except HttpError as error:
print(f"An error occurred: {error}")
raise error
@get_credentials
def read_values(spreadsheet_id, range_name, credentials):
try:
service = build("sheets", "v4", credentials=credentials)
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
rows = result.get("values", [])
# for row in rows: print(row)
return result
except HttpError as error:
print(f"An error occurred: {error}")
return error
@get_credentials
def append_values(spreadsheet_id, range_name, values, credentials):
try:
service = build("sheets", "v4", credentials=credentials)
body = { "values": values }
result = service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id, range=range_name,
valueInputOption="USER_ENTERED", body=body).execute()
# print(f"{(result.get('updates').get('updatedCells'))} cells appended.")
return result
except HttpError as error:
print(f"An error occurred: {error}")
return error
if __name__ == "__main__":
TEST_SPREADSHEET_ID = "1cJNbeULQvetY2LEde1RDGsu_31JY_Av_AQMNBkaAvWQ"
TEST_RANGE_NAME = "2022/10/11"
create_sheet(TEST_SPREADSHEET_ID, "test")
# read_values(TEST_SPREADSHEET_ID, TEST_RANGE_NAME)
# append_values(TEST_SPREADSHEET_ID, TEST_RANGE_NAME, [[ 1, 2, 3, 4, 5 ]])