-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
33 lines (24 loc) · 956 Bytes
/
main.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
import gspread
from google.oauth2.service_account import Credentials
scopes = ["https://www.googleapis.com/auth/spreadsheets"]
creds = Credentials.from_service_account_file("credentials.json", scopes=scopes)
client = gspread.authorize(creds)
sheet_id = "1dBFzUMXbEMDSPuoyDOi6l2gGnDG-fnK1dNt4h8ei_H0"
workbook = client.open_by_key(sheet_id)
values = [
["Name", "Price", "Quantity"],
["Basketball", 29.99, 1],
["Jeans", 39.99, 4],
["Soap", 7.99, 3],
]
worksheet_list = map(lambda x: x.title, workbook.worksheets())
new_worksheet_name = "Values"
if new_worksheet_name in worksheet_list:
sheet = workbook.worksheet(new_worksheet_name)
else:
sheet = workbook.add_worksheet(new_worksheet_name, rows=10, cols=10)
sheet.clear()
sheet.update(f"A1:C{len(values)}", values)
sheet.update_cell(len(values) + 1, 2, "=sum(B2:B4)")
sheet.update_cell(len(values) + 1, 3, "=sum(C2:C4)")
sheet.format("A1:C1", {"textFormat": {"bold": True}})