-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet_utils.py
More file actions
44 lines (34 loc) · 1.05 KB
/
sheet_utils.py
File metadata and controls
44 lines (34 loc) · 1.05 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
import os
from googleapiclient.discovery import build
from utils import load_and_refresh_credentials
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
SPREADSHEET_ID = os.getenv("SPREADSHEET_ID")
SHEET_NAME = "Sheet1"
creds = load_and_refresh_credentials(scopes=SCOPES)
def append_to_sheet(values: list[str]):
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
body = {
'values': [values]
}
result = sheet.values().append(
spreadsheetId=SPREADSHEET_ID,
range=f"{SHEET_NAME}!A2:G",
valueInputOption="RAW",
insertDataOption="INSERT_ROWS",
body=body
).execute()
return result
def update_to_sheet(range_to_update, values: list[str]):
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
body = {
'values': [values]
}
result = sheet.values().update(
spreadsheetId=SPREADSHEET_ID,
range=range_to_update,
valueInputOption="RAW",
body=body
).execute()
return result