简体   繁体   English

调试 Google Apps 脚本 Web 应用程序的有效方法

[英]Effective way to debug a Google Apps Script Web App

I have had some experience writing container-bound scripts, but am totally new to web apps.我有一些编写容器绑定脚本的经验,但对 web 应用程序完全陌生。

How do I debug (eg look at variable values, step through code etc) a web app?如何调试(例如查看变量值、逐步执行代码等)web 应用程序? In a container bound script it was easy, because I could set breakpoints, use the apps script debugger - how do I go about this in a web page eg when I execute a doPost ?在容器绑定脚本中这很容易,因为我可以设置断点,使用应用程序脚本调试器 - 我如何在 web 页面中对此进行 go,例如当我执行doPost时?

In his excellent book "Google Script", James Ferreira advocates setting up your own development environment with three browser windows; James Ferreira 在他的优秀著作《Google Script》中提倡用三个浏览器窗口设置自己的开发环境; one for the code, one for the live view (in Publish, Deploy as web app, you are provided with a "latest code" link that will update the live view to the latest save when it is refreshed), and one for a spreadsheet that logs errors (using try/catch wrapped around bits of code you want to keep an eye on).一个用于代码,一个用于实时视图(在发布、​​部署为 Web 应用程序中,您将获得一个“最新代码”链接,该链接将在刷新时将实时视图更新为最新保存),一个用于电子表格记录错误(使用 try/catch 包裹你想要关注的代码位)。

In Web Apps, even the most basic debugging of variables through Logger.log() does not work!在 Web Apps 中,即使是通过 Logger.log() 进行最基本的变量调试也不起作用!

A great solution to have at least simple variable logging available is Peter Herrmann's BetterLog for Apps Script . Peter Herrmann 的 BetterLog for Apps Script是至少提供简单变量日志记录的一个很好的解决方案。 It allows you to log into a spreadsheet (the same as your working spreadsheet or a separate one).它允许您登录电子表格(与您的工作电子表格相同或单独的电子表格)。

Installation is very simple - just add an external resource (see the Github readme) and a single line of code to override the standard Logger object:安装非常简单——只需添加一个外部资源(参见 Github 自述文件)和一行代码来覆盖标准 Logger 对象:

Logger = BetterLog.useSpreadsheet('your-spreadsheet-key-goes-here');

Remember , that the spreedsheet that you give here as a parameter will be used for the logging output and thus must be writable by anybody !请记住,您在此处作为参数提供的电子表格将用于日志输出,因此任何人都必须可写

BetterLog will create a new sheet called "Log" in the given spreadsheet and will write each log call into a separate row of that sheet. BetterLog 将在给定的电子表格中创建一个名为“Log”的新工作表,并将每个日志调用写入该工作表的单独一行。

So, for me, I debug the front-end using inspector, I haven't found a way to step through code yet, but you can use 'debugger' in your javascript (along with console.log) to stop the code and check variables.所以,对我来说,我使用检查器调试前端,我还没有找到单步执行代码的方法,但是您可以在 javascript 中使用“调试器”(以及 console.log)来停止代码并检查变量。

to debug the backend, what I've been doing is to write my functions like调试后端,我一直在做的是编写我的函数,比如

function test_doSomething(){
  payload = "{item1: 100, item2: 200}"  //<- copy paste from log file
  backend_doSomething(payload)
}
function backend_doSomething(payload){
  Logger.log(payload)
  params = JSON.parse(payload)
  ...
}

Then after refreshing your project on the backend, you can look at executions, grab the payload from the log file, and paste it into your test_doSomething() function.然后在后端刷新项目后,您可以查看执行情况,从日志文件中获取有效负载,并将其粘贴到您的 test_doSomething() function 中。

From there, you are re-creating the call that you want to debug and you can run that, stepping through the backend code as usual.从那里,您正在重新创建要调试的调用,您可以运行它,像往常一样单步执行后端代码。

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

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