单步执行 Google Apps 脚本中的代码(相当于 VBA-GAS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17266155/
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
Stepping through code in Google Apps Script (equivalent VBA-GAS )
提问by Frank Montemorano
When writing my VBA macros I often used "F8" so as to debug the macro line by line. Is there a similar feature in Google Apps Script?
在编写 VBA 宏时,我经常使用“F8”来逐行调试宏。Google Apps Script 中是否有类似的功能?
回答by Mogsdad
Similar, but not the same.
相似,但不一样。
Google Apps Script is developed in a dedicated IDE1called the Script Editor, which provides support for single-step execution and breakpoints, among other things.
Google Apps Script 是在称为Script Editor的专用 IDE 1 中开发的,它提供对单步执行和断点等的支持。
For a quick introduction to debugging using the IDE, see this video. The Troubleshootingsection of the online documentation includes a quick overview of the basics.
有关使用 IDE 进行调试的快速介绍,请参阅此视频。在线文档的故障排除部分包括对基础知识的快速概述。
Debugging Functions with parameters
带参数的调试函数
In the IDE you can select any function in your script for execution, and then select the "run" or "Debug" icons to start. Unfortunately, there is no way to pass parameters to a function this way, so here are a few ways you can deal with that.
在 IDE 中,您可以选择脚本中的任何函数来执行,然后选择“运行”或“调试”图标来启动。不幸的是,没有办法通过这种方式将参数传递给函数,所以这里有几种方法可以解决这个问题。
Set defaults. There are numerous ways to define defaultsin javascript, here is a picture of the debugger operating on a function using the simplest of them. The function
toText()
from this answeraccepts a number as a parameter, so for this example we are forcing the default value to21
. The picture shows the debugger paused at line 40; if we continue stepping through the function we expect to end up with a results == 'Twenty-one'
.Write a test function. This is a better approach than setting defaults, because it allows you to write multiple test cases, and it avoids polluting your target function with debug code. As an example, the target function
flipFlopAndFly()
and its test functiontest_flipFlopAndFly()
were provided in this answer. The test function accesses the spreadsheet to provide appropriate data for testing the target, so we can easily modify the data for different tests. Note also, this function includes error checking, so it would not be appropriate to test by forcing default values.
设置默认值。在 javascript 中定义默认值的方法有很多种,这里是调试器使用其中最简单的方法对函数进行操作的图片。该函数
toText()
从这个答案接受一个数字作为参数,所以在这个例子中,我们强制的默认值21
。图为调试器在第 40 行暂停;如果我们继续逐步执行我们期望最终得到结果的函数s == 'Twenty-one'
。写一个测试函数。这是比设置默认值更好的方法,因为它允许您编写多个测试用例,并且可以避免调试代码污染您的目标函数。例如,此答案中提供了目标函数
flipFlopAndFly()
及其测试函数。测试功能访问电子表格为测试目标提供适当的数据,因此我们可以轻松修改不同测试的数据。另请注意,此功能包括错误检查,因此通过强制默认值进行测试是不合适的。test_flipFlopAndFly()
There are many variations on these basic patterns, so feel free to adapt them to your own situation. It will help your debugging capability to think while you are writingabout how you would step through your code:
这些基本模式有许多变体,因此请根据自己的情况随意调整它们。这将有助于您在编写代码时思考如何单步调试代码的调试能力:
- Is each important object and value stored in a
var
, so you can see it? - Is your function result (return value) in a
var
?
- 每个重要的对象和值是否都存储在一个 中
var
,以便您可以看到它? - 您的函数结果(返回值)是否在 a 中
var
?
Custom Functions
自定义函数
When developing or debugging custom functions that will be called from a spreadsheet, remember that you cannot "jump" into the IDE. If you need to step through the script, you will need to work entirely within the IDE to observe execution of your code. You can use the techniques described above to debug custom functions.
在开发或调试将从电子表格中调用的自定义函数时,请记住您不能“跳转”到 IDE。如果您需要单步执行脚本,则需要完全在 IDE 中工作以观察代码的执行情况。您可以使用上述技术来调试自定义函数。