Easy Google Apps Script Tutorial: Understanding Macros, Functions, and Triggers
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 is 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
Log into Google Sheets and open the Google Sheets document in your browser.
From the top navigation bar, navigate to Extensions > Apps Script.
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.
Press "Ctrl + C" on your keyboard to save the function.
Importing a function (with a Simple Trigger) as a macro
- Create a function using the steps in section above, "Steps to create a function".
- Ensure you are using a reserved function name for your macro.
- Go back to the Google Sheets document.
- Navigate to Extensions > Macros > Import Macros.
- Find the desired function and click "Add function". Close dialog.
- Navigate to Extensions > Macros > Import Macros and ensure your function is displayed in the list.
Creating a Custom Trigger for a function
Create a function following the steps above.
Ensure you are not using a reserved function name for your macro.
Staying in the Apps Script website, navigate to the "Trigger" tab on the left navigation bar.
On the bottom right, click "Add Trigger".
Select "On Change" for the "Event Type" dropdown menu. Click "Save".
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.