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

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

问题描述

我试图在Apps Script中为Google Spreadsheet创建我的第一个自定义函数,而且我很难使用调试器。



我正在研究从Google文档中,它演示了可以应用于自定义函数的技术。



不是定义要传递给函数的事件,而是想要提供(或从您的电子表格中检索)参数的值。

  function test_drivingDistance(){
// Define一套测试值
var testSet = [['华盛顿特区','西雅图,华盛顿'],
['渥太华,ON','奥兰多,佛罗里达州],
[ '巴黎,法国','达喀尔,塞内加尔']];

//运行多个测试
(test test in testSet){
Logger.log('Test'+ test +'='+ drivingDistance(testSet [test] [ 0],测试集[测试] [1]));
}

//从表单获取参数
var TestFromSheet = drivingDistance(ss.getRange('A1')。getValue(),ss.getRange('A2')。的getValue());
}

你明白了。你仍然可以在你的函数中设置断点,或者使用调试器来暂停执行。




编辑 - 检查参数



从电子表格调用自定义函数接收什么参数?

调试器的功能有限,因为调试器不能用于从表格调用时检查自定义函数,而自定义函数的安全限制会阻止日志记录。一般来说,理解通过的论证可能就足够了。虽然JavaScript函数可能具有命名参数,但所有参数都以类似数组的对象的形式传递,称为 arguments 。该自定义函数将返回一个报告接收到的参数的数组。从电子表格中调用时,每个参数都会出现在它自己的单元格中,从您输入函数的单元格开始:

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

返回argArray;
}



在javascript中,并没有像int或float这样的真正类型 - 只是Number。这些参数将显示在它们上面而没有引号,并且看起来像数字。日期以Date对象形式出现,但以这种方式打印时显示为Date-y字符串。字符串有引号。



自定义函数从不接收范围作为参数;当您在电子表格中提供范围参数时,其内容将被收集到一维或二维数组中,并且数组就是参数。


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.

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).

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.

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.

解决方案

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

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


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

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天全站免登陆