How to debug or view logs in Google App Script fired from sheet
Asked Answered
M

3

8

When developing Google Apps Scripts (specifically for Sheets in my case), you can use the Debug button to step through code and inspect variables, or you can use the Logger to output debug info.

What I can't figure out, however, is if there is any way to do these things when functions are triggered through the sheet itself. For example, from a custom menu item which fires a function, or an onEdit() trigger.

It makes it very difficult to debug issues because, as far as I can tell, GAS simply fails silently. Nothing useful appears in the browser JS console either.

Molybdenous answered 2/11, 2022 at 10:58 Comment(3)
If you run the script from a menu item or even custom function, you can view the success or errors of the script from the exection log. Go to script editor and click on execution log and you should see the name of the function that you ran there and click on it and see if there are any errors. Using this technique for debugging requires you put a lot of Logger.log() in your source code.Hepsibah
You can run a function from an intermediate function and still use the debugger. Even an onEdit as long as you provide the event object. This could simulate both the menu run function and the onEdit(). Personally, I tend to avoid that.Witkin
@Witkin I'm not clear what you mean by that. An intermediate function meaning a function that exists simply to call the event handler? How would it create an appropriate event object to pass to the event script?Pulmonic
O
3

The answer from @Lorena Gomez is correct but misses explaining a key detail -- where the heck is the "Apps Script execution log"? If, like me, you repeatedly looked under the place labeled "Execution Log", you're looking in the wrong place.

Move your mouse to the left edge of the browser window. There is a column of unlabeled icons. Hover over the hamburger-with-triangle one. When you hover, it expands, and you can see it says "Executions". Click it. You'll see a list of function runs. Click one to expand it.

Overrate answered 23/7, 2023 at 6:3 Comment(0)
J
1

I'm writing this answer as a community wiki, since the issue was resolved from the comments section, in order to provide a proper response to the question.

As mentioned in the comments, you can use the Apps Script execution log in the script editor. You can use either the Logger or console logging classes.

The difference between Apps Script Logger and console is that Logger.log requires using %s placeholders for each parameter, while console works in the usual JavaScript way:

Logger.log('Values', 1, 2, 3);   // will only log "Values"
console.log('Values', 1, 2, 3);  // will log "Values 1 2 3"

In addition, to test a trigger function and check if there are any errors, you can do the following:

  1. Since you're using onEdit trigger, edit a cell in the sheet.
  2. Go to the Apps Script project.
  3. If there are errors, you can view the logs in the "Executions" section and you'll find a list of executions which you can filter by Failed and Time out. enter image description here

Reference:

Apps Script mechanisms for logging

Jalbert answered 2/11, 2022 at 10:58 Comment(1)
This doesn't really answer the question. The OP was fully aware of the Logger. The question was how to see Logger output while testing an event triggered function. From what I can see, the logger produces no output from such a function.Pulmonic
K
1

I have failed so far to get any Logger output from a function fired by a custom menu item. So I've done it in a simpler way - create another function that will pre-select the sheet and the cell for your triggered fuction. That will also allow you do step-by-step debugging, set breakpoints, and inspect variables in the debugger. Here's a code snippet for copypasting:

function generateVacationsDebug() {
  SpreadsheetApp.getActiveSpreadsheet().setActiveSheet(
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Vacations")
  );
  SpreadsheetApp.getActiveSheet().setActiveRange(
    SpreadsheetApp.getActiveSheet().getRange("C305")
  );

  generateVacations();
};    
Kellby answered 4/7, 2023 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.