sujitv14 commited on
Commit
4cf5ddf
·
verified ·
1 Parent(s): 0c8268f

Create gsheet.py

Browse files
Files changed (1) hide show
  1. src/gsheet.py +36 -0
src/gsheet.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gspread
2
+ from oauth2client.service_account import ServiceAccountCredentials
3
+
4
+ client = gspread.authorize(credentials)
5
+
6
+ sheet = client.open("Student Marks").sheet1
7
+
8
+ sheet.append_row(["Sujit", "101", 18])
9
+
10
+ def connect_sheet():
11
+ scope = [
12
+ "https://spreadsheets.google.com/feeds",
13
+ "https://www.googleapis.com/auth/drive"
14
+ ]
15
+
16
+ creds = ServiceAccountCredentials.from_json_keyfile_name(
17
+ "credentials.json", scope
18
+ )
19
+
20
+ client = gspread.authorize(creds)
21
+
22
+ # Debug (optional)
23
+ print(client.openall())
24
+
25
+ sheet = client.open_by_key(SHEET_ID).sheet1
26
+ return sheet
27
+
28
+
29
+ def insert_data(data):
30
+ sheet = connect_sheet()
31
+ sheet.append_row(data)
32
+
33
+
34
+ def get_all_data():
35
+ sheet = connect_sheet()
36
+ return sheet.get_all_records()