11

Say I have the following broken example function in a google-apps script. The function is intended to be called from a google sheet with a string argument:

function myFunction(input) {
  var caps = input.toUpperCase()
  var output = caps.substrin(1, 4)
  return output
}

While this example script should break on line 3 when you select myFunction and press debug, as there is no such method as "substrin()," it will break on line 2, because you can't put undefined in all caps:

TypeError: Cannot call method "toUpperCase" of undefined. (line 2, file "Code")

Question: Is there an official way to pass a string to a google-apps script for testing/debugging without making an additional function

function myOtherFunction() {
 myFunction("testString")
}

and debugging that?

1 Answer 1

10

The function as you wrote it does need a parameter and there is no way to avoid that except by including a default value in the function itself. See example below

function myFunction(input) {
  input= input||'test';
  var caps = input.toUpperCase();
  var output = caps.substrin(1, 4);
  return output;
}
Sign up to request clarification or add additional context in comments.

4 Comments

So this will choose 'test' if input starts as undefined, but otherwise use the input? How does it decide which to use? And there's no Google UI for this?
Yes. Add a Logger.log(output) before return to be able to visualize the output
input = input || 'test' is shorthand for if ( !input ) input = 'test'; the logical or ("||") converts input to true or false, and if false, chooses the second operand. Note, however, that 0 will evaluate to false, so if 0 is passed, input will be 'test'. You can avoid this by using: input = (input !== undefined) ? input : 'test'; On the other hand, any string will test as true: ("","0","false","undefined") will all evaluate to true.
it's pathetic that in the debugger you can't pass parameters to the function, you just have to modify the code, add default values ​​to check the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.