skip to Main Content

I’m writing a fairly large amount of code in Apps Scripts currently, and debugging minor things has become troublesome.

For example, instead of putting "SpreadsheetApp" I put "SpreadsHeetApp", and although I was made aware of this error by Apps Scripts, it did not tell me which line of code this was on!

If you run a single function Apps Scripts provides an Execution log, but this does not seem to be available when you are running it from your sheet.

Am I missing something? Is there a better way to log and read errors in Apps Scripts?

2

Answers


  1. I make it a habit of always wrapping my functions in a try catch block. That way I create a redimentary traceback. You can even create your own error messages as shown in the alternate debug2.

    Code.gs

    function debug() {
      try {
        debug1();
      }
      catch(err) {
        console.log("Error in debug:"+err);
      }
    }
    
    function debug1() {
      try {
        debug2();
      }
      catch(err) {
        throw "nError in debug1: "+err;
      }
    }
    
    function debug2() {
      try {
        let a = 1;
        let b = 2;
        let c = 3;
        let x = a/b;
        let y = c/d;
      }
      catch(err) {
        throw "nError in debug2: "+err;
      }
    }
    

    Executiong log

    7:04:13 AM  Notice  Execution started
    7:04:14 AM  Info    Error in debug:
                        Error in debug1: 
                        Error in debug2: ReferenceError: d is not defined
    7:04:13 AM  Notice  Execution completed
    

    Alternate debug2

    function debug2() {
      try {
        let a = 1;
        let b = 2;
        let c = 3;
        let d = 0;
        if( d === 0 ) throw "d is zero";
        let x = a/b;
        let y = c/d;
      }
      catch(err) {
        throw "nError in debug2: "+err;
      }
    }
    

    Execution log

    7:10:45 AM  Notice  Execution started
    7:10:46 AM  Info    Error in debug:
                        Error in debug1: 
                        Error in debug2: d is zero
    7:10:46 AM  Notice  Execution completed
    

    Reference

    Login or Signup to reply.
  2. You can find the execution logs by using one of two navigation routes

    1. Open the Google Apps Script project, then click on Executions. This option is located on the sidebar menu, which is initially collapsed. To expand it, hover your cursor over the sidebar and click on the arrow that appears.

      Google Apps Script's Sibebar menu

    2. Go to https://scripts.google.com, find your project, then click the three dots menu

      enter image description here

      then click Executions
      enter image description here

    Another option, might be to use try..catch and set the catch clause to show the error details

    function example(){
      try {
        // your code here
      } catch (error) {
        SpreadsheetApp.getUi().alert(error.message + 'n' + error.stack)
      }
    }
    

    Alert showing error details

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search