3

I have an argument SalesID I'd like to pass through to the Google App Script. How can I pass it through with this python script?

(Script is from https://developers.google.com/apps-script/api/how-tos/execute)

from __future__ import print_function
from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

def main(SalesID):
    """Runs the sample.
    """
    SCRIPT_ID = '1WChnVrk5gycQEtumI7mPi5PexXafuhBAWN7-VnBK2aPkFpzMHtUp0cnx' #Actual Google Sheet Sample

    # Setup the Apps Script API
    SCOPES = ['https://www.googleapis.com/auth/script.projects','https://www.googleapis.com/auth/spreadsheets']
    store = oauth_file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('script', 'v1', http=creds.authorize(Http()))

    # Create an execution request object.
    request = {"function": "getFoldersUnderRoot"}

    try:
        # Make the API request.
        response = service.scripts().run(body=request,
                scriptId=SCRIPT_ID).execute()

        if 'error' in response:
            # The API executed, but the script returned an error.

            # Extract the first (and only) set of error details. The values of
            # this object are the script's 'errorMessage' and 'errorType', and
            # an list of stack trace elements.
            error = response['error']['details'][0]
            print("Script error message: {0}".format(error['errorMessage']))

            if 'scriptStackTraceElements' in error:
                # There may not be a stacktrace if the script didn't start
                # executing.
                print("Script error stacktrace:")
                for trace in error['scriptStackTraceElements']:
                    print("\t{0}: {1}".format(trace['function'],
                        trace['lineNumber']))
        else:
            # The structure of the result depends upon what the Apps Script
            # function returns. Here, the function returns an Apps Script Object
            # with String keys and values, and so the result is treated as a
            # Python dictionary (folderSet).
            folderSet = response['response'].get('result', {})
            if not folderSet:
                print('No folders returned!')
            else:
                print('Folders under your root folder:')
                for (folderId, folder) in folderSet.iteritems():
                    print("\t{0} ({1})".format(folder, folderId))

    except errors.HttpError as e:
        # The API encountered a problem before the script started executing.
        print(e.content)


if __name__ == '__main__':
    main()

Sample Google Sheet: https://docs.google.com/spreadsheets/d/1Z4PAY3CCaRorn5LRdFQKn4-EcAHxwxHJsABzEgsSQk0/edit#gid=0

Google app script's function:

function myFunction(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var timestamp = new Date();

sheet.getRange("A2").setValue(e.parameter.SalesID);
sheet.getRange("B2").setValue(timestamp);
}

Basically I'd like to be able to pass the SalesID from shell to Google app script to process. Thanks!

sojim2
  • 1,245
  • 2
  • 15
  • 38

1 Answers1

3

As written in the official documentation, You must provide it in the request body:

request = {"function": "myFunction", "parameters": [{"salesID" : 123}]}

Also use setValue(e.salesID);

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Thanks! Although your sample doesn't look like any of the documentation's sample. Their sample isn't complete to a newbie. – sojim2 Feb 22 '19 at 00:54
  • 1
    @sojim2 Considering that you read and did the `jwt` OAuth doc on your own, I'm sure you would've figured it out sooner than later. – TheMaster Feb 22 '19 at 01:01
  • After trying the request parameters `request = {"function": "myFunction", "parameters": [{"salesID": 123}]}` it gave me `Request contains an invalid argument` error. https://i.imgur.com/lFcmzLF.png I read the document over and tried different format but no luck. I tried the request given in the document `request = {"function": "getFoldersUnderRoot"}` and still same error. It leads me to think something else is wrong. I also tried only 1 scope in the sample. I've submitted the issue on github as well: https://github.com/gsuitedevs/python-samples/issues/83 – sojim2 Feb 22 '19 at 08:17
  • 1
    @sojim2 For example, how about using the authorization script at Quickstart of Drive API? In my environment, I can confirm that I used this for Apps Script API and it works. https://developers.google.com/drive/api/v3/quickstart/python – Tanaike Feb 23 '19 at 02:27
  • @Tanaike I just tried that and it works fine, perhaps I can use this credential method towards the script execute method. https://i.imgur.com/idhmJpX.png – sojim2 Feb 23 '19 at 08:27
  • 1
    @sojim2 I'm glad your issue was resolved. There are several scripts for authorizing. So when one of them cannot be used, other scripts can be used. This is one of them. By the way, can you add the completed script to the issue of GitHub you posted? I think that it might be useful for other users. – Tanaike Feb 23 '19 at 08:31
  • 1
    @Tanaike Agreed, will do so once I complete it with the execution method. – sojim2 Feb 23 '19 at 08:39
  • 1
    @sojim2 Maybeit's got something to do with oAuth2client being depreciated. – TheMaster Feb 23 '19 at 08:41
  • 1
    @TheMaster I think so. – Tanaike Feb 23 '19 at 08:48
  • 1
    @Tanaike @TheMaster It looks like others are experiencing the same thing recently https://github.com/googleapis/google-api-python-client/issues/628. They have a fix for it but it's not a long term solution since oAuth2client is deprecated. The solution is: `service = build('script', 'v1', credentials=creds)` – sojim2 Feb 23 '19 at 19:03