2

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?

Milen Kovachev
  • 4,801
  • 4
  • 36
  • 55
  • You can call the doPost() function from within the script, like done [here](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas) for triggers. Also, adding [try and catch block](https://www.w3schools.com/js/js_errors.asp) and logging the errors on a different sheet in google sheets is an option. – Jack Brown Feb 19 '19 at 18:07

2 Answers2

8

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().

TheAddonDepot
  • 7,422
  • 2
  • 15
  • 24
  • 1
    Thanks! This works exactly as you have described it. Just a minor observation that while in the StackDriver Logging UI, the logs do not refresh with the refresh button. You need to go to your script View -> StackDriver Logging, load the StackDriver Logging UI again in order to see the new logs. – Milen Kovachev Mar 26 '19 at 10:12
1

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.

Dharman
  • 26,923
  • 21
  • 73
  • 125
aNewb
  • 153
  • 1
  • 10