This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
SheetsService.py
166 lines (112 loc) · 4.96 KB
/
SheetsService.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import sys
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
# CLIENT_SECRET_FILE = 'client_secret.json'
CLIENT_SECRET_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'client_secret.json')
APPLICATION_NAME = 'Sheeter Fusion 360 Add-in'
DIRECTORY_NAME = 'FusionSheeter'
CREDENTIALS_JSON = 'fusion-sheeter.json'
def modules_path():
if not hasattr(modules_path, 'path'):
modules_path.path = os.path.join(os.path.dirname(__file__), 'lib')
return modules_path.path
def modify_path():
sys.path.insert(0, modules_path())
def revert_path():
if modules_path() in sys.path:
sys.path.remove(modules_path())
modify_path()
try:
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
finally:
revert_path()
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, DIRECTORY_NAME, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, CREDENTIALS_JSON)
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def get_sheets_service():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)
return service
# Get Values from a sheet
def sheets_get_range(spreadsheet_id, range_name):
""" Returns a Value Range for the input ranges.
See Sheets API Documnetation for range syntax
Sample: 'Sheet1' returns all values on the sheet
"""
service = get_sheets_service()
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
if not result:
# Todo change to raise?
raise Exception('Failed to get sheet data')
return result
# Get Value Ranges from a sheet with batch
def sheets_get_ranges(spreadsheet_id, sheet_ranges):
""" Returns a Value Ranges for the input ranges.
See Sheets API Documnetation for rangee syntax
Sample: ['Sheet1', 'Sheet2] returns all values on these sheets
"""
service = get_sheets_service()
response = service.spreadsheets().values().batchGet(spreadsheetId=spreadsheet_id, ranges=sheet_ranges).execute()
if not response:
raise Exception('Failed to get sheet data')
value_ranges = response.get("valueRanges", [])
return value_ranges
# Update values in a Sheet
def sheets_update_values(sheet_id, sheet_range, range_body):
service = get_sheets_service()
request = service.spreadsheets().values().update(spreadsheetId=sheet_id, range=sheet_range, body=range_body,
valueInputOption='USER_ENTERED')
response = request.execute()
return response
# Update values in a Sheet
def sheets_append_values(sheet_id, sheet_range, range_body):
service = get_sheets_service()
request = service.spreadsheets().values().append(spreadsheetId=sheet_id, range=sheet_range, body=range_body,
valueInputOption='USER_ENTERED')
response = request.execute()
return response
# Update a spreadsheet
def sheets_update_spreadsheet(spreadsheet_id, update_request_list):
service = get_sheets_service()
batch_update_spreadsheet_request_body = {
# A list of updates to apply to the spreadsheet.
# Requests will be applied in the order they are specified.
# If any request is not valid, no requests will be applied.
'requests': update_request_list
}
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id,
body=batch_update_spreadsheet_request_body)
response = request.execute()
return response
def sheets_copy_spreadsheet(spreadsheet_id):
service = get_sheets_service()
result = service.spreadsheets().get(spreadsheetId=spreadsheet_id, includeGridData=True).execute()
new_result = service.spreadsheets().create(body=result).execute()
return new_result