2

Here is my Python side.

...
SCRIPT_ID = '<script id>'
creds = GetGoogleCredentials()
gscripts = build( 'script', 'v1', credentials = creds )

# Create an execution request object.
request = {
   "function": "test1", 
   "devMode": True, 
   "parameters": [{
      "1": "hi", 
      "3": "bye",
      "a5": "yes"
   }]
}

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

print( response )

And here is the Apps Script side:

function test1( event ) {
   Logger.log( event );
   Logger.log( event[1] );
   Logger.log( event["3"] );
   Logger.log( event["a5"] );
}

And the output in the logger:

[20-01-16 16:34:21:530 PST] {1=hi, 3=bye, a5=yes}
[20-01-16 16:34:21:531 PST] undefined
[20-01-16 16:34:21:531 PST] undefined
[20-01-16 16:34:21:532 PST] yes

I have been trying to figure out what I'm doing wrong and why the object is returning undefined when I try to index it, even when the logger is able to read the values. It seems something to do with using numbers as object keys that isn't being handled right. I was sure at first I was doing something stupid, but this is the test case I narrowed it down to.

halfer
  • 19,824
  • 17
  • 99
  • 186
mukunda
  • 2,908
  • 15
  • 21

1 Answers1

3
  • You have already been able to run the function of Google Apps Script using Apps Script API with Python.
  • When you run the function of test1 at GAS side with the arguments of {"1": "hi", "3": "bye","a5": "yes"}, both event[1] and event["3"] return undefined.
    • You want to retrieve the values hi and bye with event[1] and event["3"], respectively.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Issue and workaround:

When the following function is directly run at the script editor of Google Apps Script, all values of hi, bye and yes are shown in the log. From this, it is found that Google Apps Script can use the key which is the number.

function myFunction() {
  var event = {"1": "hi", "3": "bye","a5": "yes"};
  Logger.log( event[1] );  // <--- hi
  Logger.log( event["3"] );  // <--- bye
  Logger.log( event["a5"] );  // <--- yes
}

When {"1": "hi", "3": "bye","a5": "yes"} is sent with Apps Script API, the key which is the number doesn't return the value. I could also confirm this situation in my environment. In this case, each key can be retrieved. But when the key is the number, the value for the key cannot be retrieved. On the other hand, at the official document, I could confirm that struct_value (JSON object) can be used for parameters[].

As another test, I ran the function test1 with the following curl command.

curl -X POST \
  -H "Authorization: Bearer ###" \
  -H "Content-Type: application/json" \
  -d "{function: 'test1', parameters: [{'1': 'hi', '3': 'bye', 'a5': 'yes'}], devMode: true}" \
  "https://script.googleapis.com/v1/scripts/###:run"

I could confirm that the same issue occurred. By this, it is considered that the module of google-api-python-client is not the reason of this issue.

From above situation, I thought that when the object is sent to the GAS side, the object might not be able to be correctly received. So as a workaround, I put event = JSON.parse(JSON.stringify(event)) at the top of function. By this, I could confirm that all values are retrieved.

Modified script:

function test1( event ) {
   event = JSON.parse(JSON.stringify(event));  // Added

   Logger.log( event );  // <--- {1=hi, 3=bye, a5=yes}
   Logger.log( event[1] );  // <--- hi
   Logger.log( event["3"] );  // <--- bye
   Logger.log( event["a5"] );  // <--- yes
}

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you for the research and somewhat ruling out the python client. The `event = JSON.parse(JSON.stringify(event));` is one of the tests I tried with success as well, though I just ended up changing my input to not use numbered fields. Might even be bordering on a security issue if it's doing undefined behavior - I'm just trying to wrap my head around how it's happening (i.e., JSON module can read it, but I can't). – mukunda Jan 17 '20 at 02:51
  • @mukunda Thank you for replying. I apologize for my unclear answer for resolving your issue. In this situation, also I would like to recommend not to use the number as the key of object. – Tanaike Jan 17 '20 at 05:20