简体   繁体   English

在 Google Apps 脚本中调试自定义函数

[英]Debugging a custom function in Google Apps Script

I am trying to create my first custom function for a Google Spreadsheet in Apps Script and I am having a hard time using the debugger.我正在尝试在 Apps Script 中为 Google 电子表格创建我的第一个自定义函数,但我在使用调试器时遇到了困难。

I am working on the custom function demo code from the Google documentation and I have set a breakpoint in the custom function drivingDistance(origin, destination) that is used in a cell of my spreadsheet.我正在处理 Google 文档中的自定义函数演示代码,并且在我的电子表格单元格中使用的自定义函数drivingDistance(origin, destination)中设置了一个断点。 The problem I have is, that that the debugger shows the parameters that are passed into the function as being undefined .我遇到的问题是,调试器显示传递给函数的参数为undefined The content of any other variables that are created during execution is displayed correctly though (as long as they do not depend on the input parameters).但是,在执行期间创建的任何其他变量的内容都会正确显示(只要它们不依赖于输入参数)。

Funny thing is that although the input parameters are displayed as undefined, the function's calculations succeed, so this seems to be a debugger issue.有趣的是,虽然输入参数显示为未定义,但函数计算成功,所以这似乎是调试器的问题。 Unfortunately this problem prevents me from successfully learning to create and debug own code (as I will have to work with complex input parameters).不幸的是,这个问题使我无法成功学习创建和调试自己的代码(因为我将不得不使用复杂的输入参数)。

I have a feeling that the problem is connected to the server-side execution of Apps Script, so I tried to log the input parameters using the Logger class and I also tried to copy these variables into new local variables.我感觉问题出在服务器端执行Apps Script,所以我尝试使用Logger类记录输入参数,我也尝试将这些变量复制到新的局部变量中。 But all I came up with was undefined .但我想出的只是undefined

Another strange hint is, that typeof of the parameters returns String .另一个奇怪的提示是,参数的typeof返回String But getting the length of them throws an error and trying to concatenate them with another string returns the string "undefined" (see my screen dump).但是获取它们的长度会引发错误并尝试将它们与另一个字符串连接会返回字符串“未定义”(请参阅​​我的屏幕转储)。

调试器截图

I am looking for insights about what is going on here.我正在寻找有关这里发生的事情的见解。

The debugger is probably not lying to you - if you launch that function in the debugger, it will have no parameters passed to it.调试器可能不会骗你——如果你在调试器中启动那个函数,它不会有任何参数传递给它。 No worries, though, you just need to make sure that you get values to use for debugging.不过不用担心,您只需要确保获得用于调试的值。 Take a look at How can I test a trigger function in GAS?看看如何在 GAS 中测试触发功能? , which demonstrates techniques that can be applied for custom functions. ,它演示了可应用于自定义函数的技术。

Instead of defining an event to pass to the function, you'll want to provide (or retrieve from your spreadsheet) values for the parameters.您需要为参数提供(或从电子表格中检索)值,而不是定义要传递给函数的事件。

function test_drivingDistance() {
  // Define a set of test values
  var testSet = [[ 'Washington, DC', 'Seattle, WA' ],
                 [ 'Ottawa, ON', 'Orlando, FL'],
                 [ 'Paris, France', 'Dakar, Senegal']];

  // Run multiple tests
  for (var test in testSet) {
    Logger.log('Test ' + test + ' = ' + drivingDistance(testSet[test][0],testSet[test][1]));
  }

  // Get parameters from sheet
  var TestFromSheet = drivingDistance(ss.getRange('A1').getValue(),ss.getRange('A2').getValue());
}

You get the idea.你明白了。 You can still set breakpoints inside your function, or use debugger to pause execution.您仍然可以在函数内设置断点,或使用debugger暂停执行。


Edit - examining arguments编辑 - 检查参数

What arguments is the custom function receiving when called from a spreadsheet?从电子表格调用时,自定义函数接收哪些参数?

You're limited in what you can do to debug this, since the debugger can't be used to examine your custom function when invoked from Sheets, and security limitations on custom functions block Logging.您可以执行的调试操作受到限制,因为当从 Sheets 调用时,调试器不能用于检查您的自定义函数,并且自定义函数的安全限制会阻止日志记录。 It might be enough to get an understanding of argument passing in general.了解一般的参数传递可能就足够了。 While javascript functions may have named parameters, all arguments are passed as an Array-like object, called arguments .虽然 javascript 函数可能有命名参数,但所有参数都作为一个类似数组的对象传递,称为arguments This custom function will return an array that reports the arguments received.这个自定义函数将返回一个报告接收到的参数的数组。 When called from a spreadsheet, each argument will appear in its own cell, starting at the cell you enter the function into:当从电子表格中调用时,每个参数都将出现在自己的单元格中,从您输入函数的单元格开始:

function testArguments(  ) {
  var argArray = [];
  for (var arg in arguments) {
    argArray.push("arguments[" + arg + "] = " + JSON.stringify(arguments[arg]))
  }

  return argArray;
}

截图

In javascript, there aren't really types like int or float - just Number.在 javascript 中,并没有真正像 int 或 float 这样的类型——只有 Number。 Those parameters will show up without quotes on them, and look like numbers.这些参数将不带引号显示,看起来像数字。 Dates arrive as Date objects, but when printed this way show up as Date-y strings.日期作为 Date 对象到达,但以这种方式打印时显示为 Date-y 字符串。 Strings have quotes.字符串有引号。

A custom function never receives a range as an argument;自定义函数从不接收范围作为参数; when you provide a range parameter in the spreadsheet, its contents are collected into a one or two-dimensional array, and the array is the argument.当您在电子表格中提供范围参数时,其内容被收集到一个一维或二维数组中,该数组即为参数。

You can use this hack to see the structure of the arguments being sent into the custom function:您可以使用此 hack 来查看发送到自定义函数中的参数的结构:

function TEST(input) {
  return (JSON.stringify(input));
}

The results will show up in your sheet like this:结果将显示在您的工作表中,如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM