-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
199 lines (117 loc) · 4.44 KB
/
examples.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from pydoc import pager
import requests
import os
from ms_graph import generate_access_token, GRAPH_API_ENDPOINT
from microsoftgraph.client import Client
# Get the following details by create an app in your Microsoft Azure Portal
# 1. Go to App registrations page
# 2. Register your app
# Your Application (client) ID
CLIENT_ID = 'YOUR_CLIENT_ID'
# Your Application (client) secret value
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
# Your OneDrive Excel Sheet Id
WORKBOOK_ID = 'YOUR_SHEET_ID';
# The scopes you wish your auth token to have
SCOPES = ['files.readwrite.all']
# LIST OF DIFFERENT SCOPES
# files.read
# files.read.all
# files.readwrite
# files.readwrite.all
# offline_access
# AUTHENTICATION SECTION
CLIENT = '9cef1616-a075-4596-8b2b-90cf9fa06579'
SCOPES = ['files.readwrite.all']
# accessToken = find_access_token(CLIENT, SCOPES)
# headers = {
# 'Authorization' : 'Bearer ' + accessToken['access_token']
# }
# POST TO SHEET TABLE EXAMPLE
tableName = 'Table1';
# url = 'https://graph.microsoft.com/v1.0/me/drive/items/'+ sheetId + '/workbook/tables/'+ tableName +'/rows/add'
# data = {
# "values": [
# ["John", "Doe", "+1 305 1234567"],
# ]
# }
# x = requests.post(url, json = data, headers = headers)
# print(x.json())
# UPLOAD FILE PYTHON REQUEST EXAMPLE
# filePath = '/Applications/MAMP/htdocs/video-guides/python-scrapy-beginner-series/scrapy-indeed/basic-scrapy-project/text.txt'
# fileName = os.path.basename(filePath)
# with open(filePath, 'rb') as upload:
# media_content = upload.read()
# response = requests.put(
# GRAPH_API_ENDPOINT + '/me/drive/items/root:/{fileName}:/content',
# headers = headers,
# data = media_content
# )
# print(response)
# UPDATE SPREADSHEET RANGE EXAMPLE
# client = Client(CLIENT, KEY, account_type='common') # by default common, thus account_type is optional parameter.
# accessToken = generate_access_token(CLIENT, SCOPES)
# token = accessToken
# client.set_token(token)
# https://learn.microsoft.com/en-us/graph/api/range-update?view=graph-rest-1.0&tabs=javascript
# # response = client.users.get_me()
# # response = client.files.drive_root_items()
# # print(response.data)
# # workbook_id = '77C597EFB8B5B71B'
# #get the workbook_id from the sheet url: resid=XXXXXXXXXX
# workbook_id = '77C597EFB8B5B71B!840'
# response = client.workbooks.list_worksheets(workbook_id)
# print(response.data)
# workbook_id = '77C597EFB8B5B71B!840'
# worksheet_id = '{A41E3F32-A5CD-448C-A2C9-F5445D791EA6}'
# response1 = client.workbooks.create_session(workbook_id)
# workbook_session_id = response1.data["id"]
# client.set_workbook_session_id(workbook_session_id)
# response = client.workbooks.get_used_range(workbook_id, worksheet_id)
# print(response.data)
# range_address = ":"
# data = {
# "values": [
# ["John", "Doe", "+1 305 1234567", "Miami, FL"],
# ["Bill", "Gates", "+1 305 1234567", "St. Redmond, WA"],
# ]
# }
# response2 = client.workbooks.update_range(workbook_id, worksheet_id, range_address, json=data)
# response3 = client.workbooks.close_session(workbook_id)
# MANUALLY INSERTING A ROW
# UPDATE RANGE EXAMPLE 2
# https://learn.microsoft.com/en-us/graph/api/range-update?view=graph-rest-1.0&tabs=javascript
# await client.api('/me/drive/items/{id}/workbook/worksheets/{sheet-id}/range(address='A1:B2')')
# url = 'https://graph.microsoft.com/v1.0/me/drive/items/'+ sheetId + '/workbook/tables/'+ tableName +'/rows/add'
# workbookId = '77C597EFB8B5B71B!840'
# worksheetId = '{A41E3F32-A5CD-448C-A2C9-F5445D791EA6}'
# url = "https://graph.microsoft.com/v1.0/me/drive/items/"+ workbookId + "/workbook/worksheets/"+ worksheetId + "/range(address='A1:B1')"
# data = {
# "values": [
# ["John", "Doe"],
# ],
# "formulas": [
# [None, None]
# ],
# "numberFormat": [
# [None, None]
# ],
# }
# print(url)
# x = requests.patch(url, json = data, headers = headers)
# print(x.json())
accessToken = generate_access_token(CLIENT, SCOPES)
headers = {
'Authorization' : 'Bearer ' + accessToken['access_token']
}
# Add new table
tableData = {
"address": "Sheet1!A1:D5",
"hasHeaders": True
}
url = GRAPH_API_ENDPOINT + '/me/drive/items/'+ sheetId + '/workbook/tables/'+ tableName
headers = {
'Authorization' : 'Bearer ' + accessToken
}
returnedRequest = requests.post(url, json=tableData, headers = headers)
print(returnedRequest.json())