- Introduction to Macros
- Creating Macros in Apps Script
- Coding Macros in Apps Script
- Recording Macros in Sheets
- How to Schedule Your Macro
- How to Edit Your Macro
- Importing Functions as Macros
- Best Practices for Using Macros
- Things You Cannot Do with Macros
- For the Visual Learners - Courtesy of Saperis
INTRODUCTION TO MACROS
Automating repetitive data cleaning tasks saves time, reduces errors and ensures consistency, especially as your datasets grow.
Google Sheets allows you to record macros, which duplicate a specific series of user interface interactions.
Once recorded, you can link a macro to a keyboard shortcut like ctrl+alt+Shift+Number.
This shortcut lets you quickly repeat data cleaning steps. You can also activate macros from the Extensions > Macros menu.
When you record a macro, Google Sheets automatically creates an Apps Script function that replicates your steps. This function is added to a file named macros.gs within a script project bound to your sheet.
Google Sheets also updates the script's manifest file, recording the macro's name and shortcut.
Since every macro is defined in Apps Script, you can edit them directly in the script editor, write them from scratch or convert existing functions into macros.
CREATING MACROS IN APPS SCRIPT
You can take data cleaning functions written in Apps Script and use them as macro functions. The easiest way is by importing an existing function from the Google Sheets editor.
If that method doesn't suit your needs, you can also create macros by coding them directly or recording your actions in the sheet.
Coding Macros in Apps Script
- In the Google Sheets user interface, select Extensions > Apps Script to open the script bound to the sheet in the Apps Script editor.
- Write the macro function. Macro functions should take no arguments and return no values.
- Edit your script manifest to create the macro and link it to the macro function. Assign it a unique keyboard shortcut and name.
- Save the script project. The macro is then available for use in the sheet.
- Test the macro function in the sheet to verify that it functions as intended.
Recording Macros in Sheets
- In the spreadsheet menu, click Extensions > Macros > Record macro.
- At the bottom, choose which type of cell reference you want your macro to use, that is, Use absolute references and Use relative references.
- Complete the task you want to record. When you are done, click Save.
- Name the macro, create a custom shortcut and then click Save.
- Use absolute references:
The macro will perform tasks on the exact cell you record.
For example, if you bold content in cell A1, the macro will only ever bold content in cell A1, regardless of which cell you clicked. - Use relative references:
The macro will perform tasks on the cell you select and its neighbors.
For example, if you record a macro on cell A1 and then run it on cell B1, it will perform the same actions relative to B1. This is generally more flexible.
HOW TO SCHEDULE YOUR MACRO
You can set your macro to run based on actions, calendar updates, time intervals or a chosen time and date.
- On your computer, open a spreadsheet at sheets.google.com.
- Click Tools > Script editor.
- At the top, click Edit and then Current project's triggers.
- At the bottom right, click Add trigger and select your options.
- Click Save.
HOW TO EDIT YOUR MACRO
Once your macros are set up, you may need to adjust them as your data cleaning needs evolve.
You can edit macros attached to a sheet by doing the following:
- In the Google Sheets user interface, select Extensions > Macros > Manage macros.
- Find the macro you want to edit and select ⋮ > Edit macro. This opens the Apps Script editor to the project file containing the macro function.
- Edit the macro function to change the macro behaviour.
- Save the script project. The macro is then available for use in the sheet.
- Test the macro function in the sheet to verify that it functions as intended.
IMPORTING FUNCTIONS AS MACROS
In addition to recording and editing macros, you can also import existing functions to expand your automation toolkit.
If there is already a script bound to a sheet, you can import a function in the script as a new macro and then assign it a keyboard shortcut. You can do this by editing the manifest file and adding another element to the sheets.macros[] property.
You can also follow these steps to import a function as a macro from the Sheets user interface:
- In the Google Sheets user interface, select Extensions > Macros > Import.
- Select a data cleaning function from the list presented and then click Add function.
- Click the close dialog button.
- Select Extensions > Macros > Manage macros.
- Locate the function you just imported in the list and assign a unique keyboard shortcut to the macro. You can also change the macro name in this section and that name will default to the name of the function.
- Click Update to save the macro configuration.
BEST PRACTICES FOR USING MACROS
- Macros are more performant when they are lightweight. Where possible, limit the number of actions a macro takes to finish, especially given how intensive data cleaning functions can be.
- Macros are best for simple operations that need to be repeated frequently with little configuration. For other tasks, consider using a custom menu item instead.
- Always remember that macro keyboard shortcuts must be unique, and a given sheet can only have ten macros with shortcuts at any one time.
- Any additional macros can only be executed from the Extensions > Macros menu.
- Macros that change a single cell can be applied to a range of cells by first selecting the full range and then activating the macro. This often makes it unnecessary to create macros that duplicate the same operation across a predefined range.
THINGS YOU CANNOT DO WITH MACROS
While macros are powerful, there are some important limitations to keep in mind.
-
Use macros outside bound scripts:
Macros are defined in scripts bound to specific Google Sheets but are ignored if defined in a standalone script or web application. -
Define macros in Sheets add-ons:
You cannot distribute macro definitions using a Sheets add-on. Any macro definitions in a Sheets add-on project are ignored by users of that add-on. -
Distribute macros in script libraries:
You cannot distribute macro definitions using Apps Script libraries. -
Use macros outside Google Sheets:
Macros only work in Google Sheets and are not a feature in Google Docs, Slides or Forms.
BEYOND MACROS WITH AI-POWERED AUTOMATION
While macros are excellent for automating repetitive sequences of actions, Flookup takes automation a step further with its AI-powered capabilities.
These features can handle complex data cleaning tasks that are difficult or impossible to achieve with traditional macros, such as:
- Intelligent deduplication that understands semantic variations.
- Automated standardization of inconsistent data formats.
- Predictive completion of missing data based on context.
- Error prevention by identifying unusual patterns in real-time.