Skip to content

Commit 73a846c

Browse files
authored
Create execute.py
1 parent dc51424 commit 73a846c

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

apps_script/execute/execute.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START apps_script_execute]
16+
def main():
17+
"""Shows basic usage of the Apps Script API.
18+
19+
Creates a Apps Script API service object and uses it to call an
20+
Apps Script function to print out a list of folders in the user's root
21+
directory.
22+
"""
23+
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
24+
25+
# Authorize and create a service object.
26+
credentials = get_credentials()
27+
http = credentials.authorize(httplib2.Http())
28+
service = discovery.build('script', 'v1', http=http)
29+
30+
# Create an execution request object.
31+
request = {"function": "getFoldersUnderRoot"}
32+
33+
try:
34+
# Make the API request.
35+
response = service.scripts().run(body=request,
36+
scriptId=SCRIPT_ID).execute()
37+
38+
if 'error' in response:
39+
# The API executed, but the script returned an error.
40+
41+
# Extract the first (and only) set of error details. The values of
42+
# this object are the script's 'errorMessage' and 'errorType', and
43+
# an list of stack trace elements.
44+
error = response['error']['details'][0]
45+
print("Script error message: {0}".format(error['errorMessage']))
46+
47+
if 'scriptStackTraceElements' in error:
48+
# There may not be a stacktrace if the script didn't start
49+
# executing.
50+
print("Script error stacktrace:")
51+
for trace in error['scriptStackTraceElements']:
52+
print("\t{0}: {1}".format(trace['function'],
53+
trace['lineNumber']))
54+
else:
55+
# The structure of the result depends upon what the Apps Script
56+
# function returns. Here, the function returns an Apps Script Object
57+
# with String keys and values, and so the result is treated as a
58+
# Python dictionary (folderSet).
59+
folderSet = response['response'].get('result', {})
60+
if not folderSet:
61+
print('No folders returned!')
62+
else:
63+
print('Folders under your root folder:')
64+
for (folderId, folder) in folderSet.iteritems():
65+
print("\t{0} ({1})".format(folder, folderId))
66+
67+
except errors.HttpError as e:
68+
# The API encountered a problem before the script started executing.
69+
print(e.content)
70+
71+
if __name__ == '__main__':
72+
main()
73+
# [END apps_script_execute]

0 commit comments

Comments
 (0)