javascript 您如何将参数传递给 Google Apps 脚本调试器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24956056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:39:55  来源:igfitidea点击:

How do you pass an argument to the Google Apps Script debugger?

javascriptdebugginggoogle-apps-scriptgoogle-sheets

提问by Menasheh

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:

假设我在 google-apps 脚本中有以下损坏的示例函数。该函数旨在从带有字符串参数的谷歌表中调用:

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:

当您选择 myFunction 并按 debug 时,这个示例脚本应该在第 3 行中断,因为没有像“substrin()”这样的方法,它会在第 2 行中断,因为您不能将 undefined 放在所有大写中:

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

类型错误:无法调用未定义的方法“toUpperCase”。(第 2 行,文件“代码”)

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

问题:是否有一种官方方法可以将字符串传递给 google-apps 脚本以进行测试/调试,而无需创建附加功能

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

and debugging that?

和调试?

采纳答案by Serge insas

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;
}