在 Google Apps 脚本中调试自定义函数 [英] Debugging a custom function in Google Apps Script

本文介绍了在 Google Apps 脚本中调试自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Apps Script 中为 Google 电子表格创建我的第一个自定义函数,但我在使用调试器时遇到了困难.

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.

我正在处理来自Google 文档和我在我的电子表格单元格中使用的自定义函数 drivingDistance(origin, destination) 中设置了一个断点.我遇到的问题是,调试器显示传入函数的参数为​​ undefined.但是,在执行期间创建的任何其他变量的内容都可以正确显示(只要它们不依赖于输入参数).

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. The problem I have is, that that the debugger shows the parameters that are passed into the function as being 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).

我感觉问题出在服务器端执行Apps Script,所以我尝试使用Logger类记录输入参数,我也尝试将这些变量复制到新的局部变量中.但我想到的只是undefined.

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. But all I came up with was undefined.

另一个奇怪的提示是,参数的typeof返回String.但是获取它们的长度会引发错误并尝试将它们与另一个字符串连接会返回字符串未定义"(请参阅​​我的屏幕转储).

Another strange hint is, that typeof of the parameters returns 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.

推荐答案

调试器可能不会骗你——如果你在调试器中启动那个函数,它不会有任何参数传递给它.不过不用担心,您只需要确保获得用于调试的值.看看 如何测试触发器GAS 中的函数?,展示了可应用于自定义函数的技术.

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?, 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());
}

你懂的.您仍然可以在函数内部设置断点,或使用 debugger 暂停执行.

You get the idea. You can still set breakpoints inside your function, or use debugger to pause execution.

自定义函数在从电子表格调用时接收哪些参数?

What arguments is the custom function receiving when called from a spreadsheet?

您可以执行的调试操作受到限制,因为当从 Sheets 调用时,调试器不能用于检查您的自定义函数,并且自定义函数的安全限制会阻止日志记录.了解一般的参数传递可能就足够了.虽然 javascript 函数可能有命名参数,但所有参数都作为类似数组的对象传递,称为 arguments.这个自定义函数将返回一个报告接收到的参数的数组.从电子表格中调用时,每个参数都将出现在自己的单元格中,从您输入函数的单元格开始:

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

在 javascript 中,并没有真正的类型,如 int 或 float - 只有数字.这些参数将不带引号显示,看起来像数字.日期作为 Date 对象到达,但以这种方式打印时显示为 Date-y 字符串.字符串有引号.

In javascript, there aren't really types like int or float - just 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. 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.

这篇关于在 Google Apps 脚本中调试自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆