1

I'm triggering a function which is in a .gs file (google apps script), from my .js file using:

google.script.run.withSuccessHandler(function_In_JS_FILE).function_In_GS_FILE();

In the JS file the function to declare the global variable looks like this:

  var newVariable ;

  function function_In_JS_FILE(return_from_function_In_GS_FILE) {

     window.newVariable = return_from_function_In_GS_FILE ;
     console.log(newVariable) ;  // It show the array that comes from the.gs file. 
  }

  console.log(newVariable) ; // It shows 'Undefined'

It is supposed that using the 'window' method that variable should be global but it is not working... I will appreciate a lot your help! Thanks!

Mfnb
  • 21
  • 5
  • 1
    maybe the log is too early. – Nina Scholz May 09 '20 at 07:36
  • Can you explain more please... What does that mean? What should I do ? – Mfnb May 09 '20 at 07:39
  • 1
    sometimes `console.log` works [funny](https://stackoverflow.com/questions/23392111/console-log-async-or-sync). another reason maybe: does the function works asynchron? – Nina Scholz May 09 '20 at 07:42
  • Sorry I have no clear understanding about what asynchron is... But what I can say is that in the console, the 'undefined' (from the `console.log` that is outside the function) shows before the `console.log` which is inside the function (which shows the Array) – Mfnb May 09 '20 at 07:49
  • By the time the second `console.log()` is run, `function_In_JS_FILE` is not yet run as it is async. So, `newVariable` is `undefined` at that time. – TheMaster May 09 '20 at 07:50
  • Does this answer your question? [What is a callback function?](https://stackoverflow.com/questions/824234/what-is-a-callback-function) – TheMaster May 09 '20 at 07:55
  • Related: https://stackoverflow.com/q/14220321/ – TheMaster May 09 '20 at 07:56
  • Thanks for the referenced concept, that definitely helps... But I still doesn't know how to solve the problem... – Mfnb May 09 '20 at 20:22
  • @Mfnb What is the problem? – TheMaster May 10 '20 at 07:01
  • 1
    If you want to do something with newVariable, do it inside function_In_JS_FILE. If there is any other function depended on newVariable then call it from inside function_In_JS_FILE, so that newVariable will be having a populated value. – Anees Hameed May 10 '20 at 13:13
  • Could you try returning the parameter in the function maybe? Try the following ```var myVariable = function function_In_JS_FILE(return_from_function_In_GS_FILE) { var newVariable = return_from_function_In_GS_FILE ; return newVariable } console.log(myVariable);``` Let me know if that worked :D – Mateo Randwolf May 11 '20 at 12:25
  • Thanks, that was the solution!. I did create a function where 'newVariable' y called, inside the 'function_In_JS_FILE' . I had to make some changes in the order or execution but it worked.. – Mfnb May 11 '20 at 16:14
  • @Mfnb was my comment the right answer? If so I will formalise it into an answer to this questions so that other users with similar questions can easily see the answer. If my comment was not the answer could you please post as an answer what did work for your? Thanks ! :D – Mateo Randwolf May 12 '20 at 07:33

3 Answers3

0

Your code just declares a global variable and a function. It works right and prints undefined as expected, because the function declaration doesn't execute on itself. Your global variable will be assigned later on, as soon as your function will be invoked from the Google script. Until then you shouldn't use your variable in any way.

Thevs
  • 3,189
  • 2
  • 20
  • 32
0

My solution to this problem was to use newVariable inside the function. May there was another way but that worked for me.

Mfnb
  • 21
  • 5
-1

Just take out the "window" from window.newVariable.

Neven Subotic
  • 1,399
  • 1
  • 6
  • 18