Development

Easy Google Apps Script Tutorial: Understanding Macros, Functions, and Triggers

3 min. read

Google Sheets is a widely used online app that allows users to create and format spreadsheets with the capability to edit and collaborate with other people in real-time. One way to extend the power of Google Sheets is with Google Apps Script.

Google Apps Script lets you customize and automate workflows within your spreadsheet. Some of the most useful features for customizing a Google Sheet spreadsheet are macros and functions.

What is a function?

In programming, a function is a “chunk” of code that can be used multiple times, rather than repeatedly writing it out in various places; it contains instructions used to create an output from its input.

What is a macro?

In Google Sheets, a macro is a series of recorded actions that can be used to automate repeatable tasks. Macros can significantly reduce time spent on users' manual, tedious processes by automating these processes for them. Once the set of actions are recorded (or manually defined in code/function), it can be scheduled to execute based on time specifications, or execute on a different trigger (either simple or custom).

Utilizing functions and macros

Start by creating a function

  1. Log into Google Sheets and open the Google Sheets document in your browser.

  2. From the top navigation bar, navigate to Extensions > Apps Script.

  3. The Apps Script website should open in a new window or tab. Ensure you are on the "Editor" tab on the left navigation bar on the screen.
    Apps Script Code Editor

  4. Define the function's body (written in JavaScript). Rename that function (myFunction()) a reserved function name to use a "Simple Trigger". Otherwise, rename it to be more descriptive without using a reserved function name to be used in a custom trigger.

  5. Press "Ctrl + C" on your keyboard to save the function.

Importing a function (with a Simple Trigger) as a macro

  1. Create a function using the steps in section above, "Steps to create a function".
  2. Ensure you are using a reserved function name for your macro.
  3. Go back to the Google Sheets document.
  4. Navigate to Extensions > Macros > Import Macros.
  5. Find the desired function and click "Add function". Close dialog.
  6. Navigate to Extensions > Macros > Import Macros and ensure your function is displayed in the list.
    Adding a Macro to a Google Sheets document

Creating a Custom Trigger for a function

  1. Create a function following the steps above.

  2. Ensure you are not using a reserved function name for your macro.

  3. Staying in the Apps Script website, navigate to the "Trigger" tab on the left navigation bar.

  4. On the bottom right, click "Add Trigger".
    Apps Script "Triggers" Page

  5. Select "On Change" for the "Event Type" dropdown menu. Click "Save".

Conclusion

Implementing custom code for your Google Sheet can be very useful, and it can be as easy as creating a macro and importing with a simple trigger, or creating a macro and pairing it with your own custom trigger in Google Apps Script.

Feel free to reach out with any questions. We hope you found this article useful and look forward to hearing any feedback.

Kim Pham

Senior Front-end Web Developer