Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug web development in Google Application Script?

I am testing/learning webhook. How to send, receive it. So I thought I would use GAS.

I have this simple script and I wonder why Logger does not work. In Projects/Executions I can see that the script doPost was executed but no logs. Email was sent and the script returned the value.

  • using old, Legacy Editor (no idea how to get the new one)
  • in Menu-Resources-Cloud Platform Project is said "This script has an Apps Script-managed Cloud Platform project."
  • when I open the project in editor I get this message "This project is running on our new Apps Script runtime powered by Chrome V8."
  • exception logging set to "exceptionLogging": "STACKDRIVER" is set to default

I tried console.log(e); but it did not work for me.

function doPost(e) {
  
 Logger.log("I was called")
 if(typeof e !== 'undefined'){

    Logger.log(e.parameter);
    Logger.log("I was called 2")
 
    MailApp.sendEmail({
     to: "[email protected]",
     subject: "Call Sucessful",
     htmlBody: "I am your <br>" +
               JSON.stringify(e)+ "<br><br>" +
               JSON.stringify(e.parameters)
      
    });
    
  }  
  return ContentService.createTextOutput(JSON.stringify(e))
}

Question1: Can I make Logger work?

Question2: I would like to see accepted data in Debugger, is that possible?

Question3: Is there any way the GAS pushes the data it received to my web browser. Of course the browser is NOT sending the data.

Question4: No related to the topic but ... Would you know what I need to do in order to be able to use new Editor?

like image 637
Radek Avatar asked Jan 27 '26 00:01

Radek


1 Answers

As lots of people specified, you can use console.log for this.

However, I also work with webhooks from time to time. And I find it much more comfortable to debug directly into google spreadsheets, using code like this

function doPost(e) {
  log('# doPost', JSON.stringify(e));
  try {
    // Some webhook-processing logic here
    if(e.parameter.action == 'test-error') {
      item.something = nothing;
    }
    if(e.parameter.action == 'test-log') {
      log('# custom log', 'Some data');
    }
  } catch(error) {
    log('# error', JSON.stringify([error, error.stack]));
  }
}

function log(event, message){
  SpreadsheetApp.getActive().getSheetByName('Log').appendRow([new Date(), event, message])
}

Example spreadsheet: https://docs.google.com/spreadsheets/d/144i9pxDIB_C8ZRZ-6vd9DiPIw8Rt85hTtVoqk7dtHQg/edit?usp=sharing

You can trigger logging by something like this

curl -X POST https://script.google.com/macros/s/AKfycby3EoaQ8DOt8H_Mc4vjH6JZkhsaAwYHk_sa9HE5Be3qVo0Ym0b2/exec?action=test-error

or

curl -X POST https://script.google.com/macros/s/AKfycby3EoaQ8DOt8H_Mc4vjH6JZkhsaAwYHk_sa9HE5Be3qVo0Ym0b2/exec?action=test-log

You can use same log for your custom logging of some intermediate variable during webhook resolution.

The reason I prefer this over standard stackdriver logging is that google sheets are more explicit and easier to manage.

like image 179
roma Avatar answered Jan 28 '26 22:01

roma