Milen Kovachev
Milen Kovachev

Reputation: 5381

Debug a Google Apps Script which is triggered by post request?

I have a simple script in Google Sheets which is trigger by a command in Slack and just adds the Slack message as a new row. It is quite a simple function and is deployed as a web app and does work:

function doPost(req) {
var sheet = SpreadsheetApp.openById('[My Sheet Id]');
var params = req.parameters;

Logger.log(params.text);

sheet.appendRow(params.text);

return ContentService.createTextOutput("Saved your entry:" + params.text);
}

However the Logger.log function never logs anything in the debugging logs. I expect it to be here:

enter image description here

Bizarrely the Executions lists is also empty:

enter image description here

But the script is being triggered and is appending the text message to the Google sheet.

So the question I suppose comes down to how exactly can I log from a script (deployed as a web app) when triggered by a post request and also how can I see its executions? In other words how do you debug such scripts?

Upvotes: 5

Views: 6442

Answers (4)

jrgibson
jrgibson

Reputation: 1

As a followup to aNewb and Dharman's post above, here is an improved version that has fewer App Script issues.

Note that I'm passing in a reference to the Google Sheets doc because I'm certain that you already have a reference to the doc if you writing a doPost() on a Google Sheet.

var LOG_SHEET_NAME = "Debug Logs";
function sheetLog(doc, msg) {
  Logger.log(msg); // To make sure you see it in App Script debugging as well

  var logSheet = doc.getSheetByName(LOG_SHEET_NAME);
  if (!logSheet) {
    doc.insertSheet(LOG_SHEET_NAME);
    logSheet = doc.getSheetByName(LOG_SHEET_NAME);
    logSheet.getRange(1,1,1,2).setValues( [["Timestamp", "Log Messages"]]);
  }

  var nxtLogRow = logSheet.getLastRow() + 1;
  logSheet.getRange(nxtLogRow,1,1,2).setValues( [[ new Date(), msg ]] );
}

Upvotes: 0

Jeromin
Jeromin

Reputation: 1

I'm reviving this topic as it still annoying. I tried different answers from this question, but unfortunately it didn't work work either in 2023 ! Didn't want to associate my apps script to a google cloud project.

So I get the idea of using Pipedream to debug my script. It's working like a charm:

function doPost (e) {
  var url = "PIPEDREAM_URL";
  var parameters = e.parameter; 
  var payload = JSON.stringify({data: parameters});

  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': payload
  };
  var response = UrlFetchApp.fetch(url, options);
}

Upvotes: 0

aNewb
aNewb

Reputation: 198

In the server side code for my webapp I write log messages to a spreadsheet

/**
  log entries stored in spreadsheet
  The msg must be a string so use + not comma
 */
function myLog(msg) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let logSht = ss.getSheetByName('DebugLog'); 
  if (!logSht)
    ss.insertSheet('log'); 
  const nxtLogRow = getLastRow (logSht.getRange('A1:A')) + 1;
  logSht.getRange('A'+ nxtLogRow)
        .setValue(msg );
}

In the code itself be sure to use plus sign not comma as the routine expects a single string.

  myLog('Begin buildContributorArr - userSession:\n' + userSession );

I am still fuzzy on how you can see what is going on in the client size script.

Upvotes: 1

TheAddonDepot
TheAddonDepot

Reputation: 8974

When a doPost(e) is invoked remotely, it creates a server-side session whose logs you cannot access via Logger.log().

However, there is an alternative, ie. StackDriver Logging (accessible from the Apps Script editor menu via View -> StackDriver Logging ).

Once you have StackDriver Logging enabled, you'll need to replace all your Logger.log() calls with console.log().

Upvotes: 10

Related Questions