Google Apps Script:
Organizing code into functions

How to:

In Google Apps Script, which is based on JavaScript, you define functions using the function keyword, followed by a unique function name, parentheses () which can contain parameters, and curly brackets {} that encapsulate the function’s code block. Here’s a basic example:

function greetUser() {
  var user = Session.getActiveUser().getEmail();
  Logger.log('Hello, ' + user + '!');
}

greetUser();

Sample output:

Now, let’s consider a more practical example related to Google Sheets where we separate the functionality into two functions: one for setting up the sheet and another for filling it with data.

function setupSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  sheet.setName('Sales Data');
  sheet.appendRow(['Item', 'Quantity', 'Price']);
}

function populateSheet(data) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sales Data');
  data.forEach(function(row) {
    sheet.appendRow(row);
  });
}

// Initialize array of data
var salesData = [
  ['Widgets', 15, 2.5],
  ['Gadgets', 8, 3.75]
];

// Run the functions
setupSheet();
populateSheet(salesData);

In this example, setupSheet prepares the sheet, and populateSheet takes an array of sales data to populate the sheet. Separating these concerns makes the code cleaner and more adaptable to changes.

Deep Dive

The concept of dividing code into functions isn’t new or unique to Google Apps Script; it’s a fundamental programming practice advocated in almost all programming languages. Historically, functions evolved from the mathematical concept of mapping inputs to outputs, which became a cornerstone in structured programming. This approach promotes modularity and code reuse, offering clear pathways for testing individual parts of the script.

Google Apps Script, being JavaScript-based, benefits significantly from JavaScript’s first-class functions, allowing functions to be passed as arguments, returned from other functions, and assigned to variables. This feature opens up advanced patterns like callbacks and functional programming, although these patterns can introduce complexity that might be unnecessary for simple automation tasks in Google Apps Script.

For larger projects or more complex applications, developers might explore using JavaScript’s newer features like arrow functions, async/await for asynchronous operations, and even TypeScript for static typing. TypeScript, in particular, can be compiled to run as Google Apps Script, providing an avenue for developers seeking more robust type checking and advanced object-oriented features.

However, for most scripting needs within Google Apps suite, sticking to simple, well-organized functions as demonstrated provides a solid foundation. It’s always a balancing act between leveraging advanced features for efficiency and maintaining simplicity for ease of maintenance and readability.