This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheets.py
44 lines (38 loc) · 1.67 KB
/
sheets.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
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
class SheetWriter:
def __init__(self, spreadsheet_id, sheet):
self.spreadsheet_id = spreadsheet_id
self.sheet = sheet
def send(self, values, value_input_option="USER_ENTERED"):
"""
Writes to the spreadsheet
"""
# Call the Sheets API and write
body = {
'values': values
}
service = self.__connect()
sheet = service.spreadsheets()
result = sheet.values().append(spreadsheetId=self.spreadsheet_id,
valueInputOption=value_input_option, body=body, range=self.sheet,).execute()
print('{0} cells appended.'.format(result \
.get('updates') \
.get('updatedCells')))
@staticmethod
def __connect():
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flags = tools.argparser.parse_args('--auth_host_name localhost --logging_level INFO --noauth_local_webserver'.split())
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store, flags)
service = build('sheets', 'v4', http=creds.authorize(Http()))
return service