Printing to the console in Google Apps Script?

前端 未结 7 821
迷失自我
迷失自我 2021-01-31 01:13

I am very new to programming (have taken some of the JS courses on Codecademy). I am trying to create a simple script to determine, if given a spreadsheet with results from a po

7条回答
  •  孤城傲影
    2021-01-31 01:24

    Even though Logger.log() is technically the correct way to output something to the console, it has a few annoyances:

    1. The output can be an unstructured mess and hard to quickly digest.
    2. You have to first run the script, then click View / Logs, which is two extra clicks (one if you remember the Ctrl+Enter keyboard shortcut).
    3. You have to insert Logger.log(playerArray), and then after debugging you'd probably want to remove Logger.log(playerArray), hence an additional 1-2 more steps.
    4. You have to click on OK to close the overlay (yet another extra click).

    Instead, whenever I want to debug something I add breakpoints (click on line number) and press the Debug button (bug icon). Breakpoints work well when you are assigning something to a variable, but not so well when you are initiating a variable and want to peek inside of it at a later point, which is similar to what the op is trying to do. In this case, I would force a break condition by entering "x" (x marks the spot!) to throw a run-time error:

    Compare with viewing Logs:

    The Debug console contains more information and is a lot easier to read than the Logs overlay. One minor benefit with this method is that you never have to worry about polluting your code with a bunch of logging commands if keeping clean code is your thing. Even if you enter "x", you are forced to remember to remove it as part of the debugging process or else your code won't run (built-in cleanup measure, yay).

提交回复
热议问题