-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleSheet.py
More file actions
46 lines (38 loc) · 1.39 KB
/
googleSheet.py
File metadata and controls
46 lines (38 loc) · 1.39 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
45
from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
# Your Google Sheet ID (from the URL)
SHEET_ID = "SHEET_ID"
RANGE = "Sheet1!A1"
def write_row_to_sheet(values):
creds = Credentials.from_service_account_file(
"service_account.json",
scopes=["https://www.googleapis.com/auth/spreadsheets"]
)
service = build("sheets", "v4", credentials=creds)
sheet = service.spreadsheets().values()
result = sheet.append(
spreadsheetId=SHEET_ID,
range="Sheet1!A:F",
valueInputOption="USER_ENTERED",
body={"values": [values]}
).execute()
# Google Sheets returns where it wrote the row: "Sheet1!A12"
updated_range = result.get("updates", {}).get("updatedRange")
# Extract row number
row = int(updated_range.split("!A")[1])
return row
def update_return_date(row_number, return_date):
creds = Credentials.from_service_account_file(
"service_account.json",
scopes=["https://www.googleapis.com/auth/spreadsheets"]
)
service = build("sheets", "v4", credentials=creds)
sheet = service.spreadsheets().values()
# "F" is the 6th column (Date of Return)
range_str = f"Sheet1!F{row_number}"
sheet.update(
spreadsheetId=SHEET_ID,
range=range_str,
valueInputOption="USER_ENTERED",
body={"values": [[return_date]]}
).execute()