Skip to content

Commit 0e22d3b

Browse files
committed
Add Basic SpreadSheet Functions
1 parent 7f9a178 commit 0e22d3b

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
main.py
2-
__pycache__
2+
__pycache__
3+
token.json

API/GoogleSheet.py

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
1+
from __future__ import print_function
2+
from googleapiclient.discovery import build
3+
from google.oauth2 import service_account
4+
from PySql import PySql
15

2-
class GoogleSheetAPI:
3-
4-
def __init__(self):
5-
pass;
6+
7+
class GoogleSheetAPI(PySql):
8+
def __init__(self, SCOPES, TOKEN_PATH):
9+
"""
10+
Initializes API Object\n\n
11+
TOKEN_PATH = `type: string`, the path to your tokens/keys
12+
"""
13+
14+
self.SERVICE_ACCOUNT_FILE = TOKEN_PATH
15+
16+
credentials = None
17+
credentials = service_account.Credentials.from_service_account_file(
18+
self.SERVICE_ACCOUNT_FILE, scopes=SCOPES)
19+
20+
self.service = build('sheets', 'v4', credentials=credentials)
21+
pass
22+
23+
def getSpreadsheetData(self, RANGE_INPUT, SPREADSHEET_ID):
24+
"""
25+
Returns a dictionary of the spreadsheet.\n\n
26+
RANGE_INPUT = `type: string`, [more info](https://docs.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/refer-to-cells-and-ranges-by-using-a1-notation) \n\n
27+
SPREADSHEET_ID = `type: string`
28+
"""
29+
30+
sheet = self.service.spreadsheets()
31+
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
32+
range=RANGE_INPUT).execute()
33+
return result
34+
35+
def updateSpreadsheetData(self, RANGE_INPUT, SPREADSHEET_ID, data):
36+
"""
37+
Updates the spreadsheet based on data input.\n\n
38+
RANGE_INPUT = `type: string`, [more info](https://docs.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/refer-to-cells-and-ranges-by-using-a1-notation) \n\n
39+
SPREADSHEET_ID = `type: string`\n\n
40+
data = `type: string`
41+
"""
42+
43+
sheet = self.service.spreadsheets()
44+
result = sheet.values().update(
45+
spreadsheetId=SPREADSHEET_ID,
46+
range=RANGE_INPUT,
47+
valueInputOption="USER_ENTERED",
48+
body={
49+
"values": data
50+
}).execute()
51+
52+
return result
53+
54+
def addSheet(self, SPREADSHEET_ID, props):
55+
"""
56+
Adds a Sheet\n\n
57+
props = `type: object`, refer to [link](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#sheetproperties)
58+
"""
59+
60+
self.__updateSheet(SPREADSHEET_ID, props)
61+
62+
def deleteSheet(self, SPREADSHEET_ID, SHEET_ID):
63+
"""
64+
Deletes a Sheet\n
65+
SPREADSHEET_ID = `type: string`\n\n
66+
SHEET_ID = `type: string`
67+
"""
68+
req = {
69+
"deleteSheet": {
70+
"sheetId": SHEET_ID
71+
}
72+
}
73+
74+
self.__updateSheet(SPREADSHEET_ID, {"requests": [req]})
75+
76+
def __updateSheet(self, SPREADSHEET_ID, reqs):
77+
"""
78+
Creates a sheet, returns the resulting sheet\n
79+
SPREADSHEET_ID = `type: string`\n\n
80+
reqs = refer to https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets\n\n
81+
"""
82+
83+
sheet = self.service.spreadsheets()
84+
res = sheet.batchUpdate(
85+
spreadsheetId=SPREADSHEET_ID, body=reqs).execute()

0 commit comments

Comments
 (0)