Code Snippets

Occasionally I come up with some useful code snippets, I write Python and JavaScript (for Google Apps Script). I’ll share some that I find useful below. They won’t be ground breaking for everyone but if they help a few people out then that’s cool.

So far I have one useful snippet, who knows if there’ll be more?

How to create an execution log for Google Apps Script (GAS) web apps

GAS web apps do not log to the script project Execution log. This has proven a big problem for debugging my latest project in development. Looking on Stackoverflow showed me that I’m not alone (but I can’t add an answer there as I’m too much of a newb on SO). One solution is to associate your script project with a Google Cloud Project. That’s overkill for my project right now. It then occurred to me that I could use the Script Properties service and create a log there. Below is a function that logs the current time (i.e. execution time) the function that called the logger and a customisable execution message.

function propertiesLogMessage(functionCalledFrom, payload){
  var executionLog = PropertiesService.getScriptProperties().getProperty('webAppExecutionLog');
  if(executionLog == null){
    var executionLog = '';
  var messageHeader = '[' + String(new Date()) + ', function: ' + functionCalledFrom + ']';
  executionLog = executionLog + messageHeader + '\n' + payload + '\n\n';
  PropertiesService.getScriptProperties().setProperty('webAppExecutionLog', executionLog);

Code snippet updated 20/11/2020 – this function is more suitable as it now adds all logs to a string in a single script property rather than making a new property for each log. Making multiple logs proved difficult to maintain (deleting script properties in a GAS project from the viewer pane is flaky).

To create a log somewhere in my code I create a message payload var executionPayload = 'API response: ' + response; and call this function specifying the function I’m calling it from as a string, e.g. propertiesLogMessage('uploadData', executionPayload); I sometimes also include the code line number with the function.
Viewing the logs is easy, open File > Project Properties > Script Properties tab. And since each key has the timestamp in it they will all be unique and will not overwrite previous logs. When the log is getting too large they can simply be deleted / culled.
If this is useful for you – great!

Create an error log in GAS web app

Extend the above snippet to create really handy error logs. I use this in try – catch blocks. In the catch block we extract the error message and the stack trace so we can locate where the error was thrown.

catch (error) {
  // Update error log stored in Script Properties
  var errorMessage = 'Error message:\n' + error.message;  // Get the error message
  var errorTrace = 'Error stack trace:\n' + error.stack;  // Get the error stack trace
  propertiesErrorLog('doPost', errorMessage + '\n' + errorTrace);  // Log error message and trace in the properties

Note that in order to use this you would need to first set up the propertiesErrorLog() function. Use the code snippet for creating an execution log (above) and name the function propertiesErrorLog instead. These snippets have proven their worth to me within five minutes of implementation! I hope they similarly help you.