Macros in Google Sheets

What Is Macros in Google Sheets?

Google Sheets macros allow one to automate repetitive tasks within a spreadsheet. Most of us know what a macro is, but if you don’t, a macro is a series of actions that are recorded and can be replayed later to automate repetitive tasks. Users can record a sequence of actions—and replay those steps with a single click. Therefore, macros are essentially Apps Script functions that are automatically generated based on the actions you record.

For example, you may want to apply a specific number format to a particular column regularly. For this, you can record a macro step-by-step once and then run it whenever you need to apply the formatting. To do this, click on Extensions -> Macros -> Record macro.  Next, perform the steps for formatting. Save the macro with a name. This allows you to run the macro anytime from the same menu.

Macros in Google Sheets Intro
Key Takeaways
  1. Macros help automate repetitive tasks to save time and reduce manual work by automating these tasks.
  2. Every recorded macro is saved as a Google Apps Script function. To record a macro, go to Extensions -> Macros -> Record macro and perform the actions to automate.
  3. Macros are stored within the specific Google Sheet where they are created.
  4. One can view, edit, import, or delete macros using the Apps Script editor.
  5. To access macros, go to Extensions > Macros in the sheet’s menu.

How To Create Macros in Google Sheets?

Let us look at the two methods of creating macros in Google Sheets.

  1. Using the Google Sheets UI
  2. Using Apps Script

Using the Google Sheets UI

Step 1: Navigate to the spreadsheet where you want to create the macro. In this example, we will insert a chart.

Using the Google Sheets UI 1

Step 2: To start recording, go to Extensions > Macros > Record macro. 

Using the Google Sheets UI 1-1

Step 3: You can choose whether to use absolute or relative references. Absolute references use specific cell coordinates, while relative references are based on the current selection. 

Using the Google Sheets UI 1-2

Step 4: Now, perform the actions you want to automate. Here, we are inserting a chart. You can also perform formatting and do other tasks.

Using the Google Sheets UI 1-3

Once you’re done recording, save the macro and assign a custom keyboard shortcut (e.g., Ctrl + Alt + Shift + number).

Step 6: Now you can run the macro. Press the assigned keyboard shortcut or find it under Tools > Macros.

Using the Google Sheets UI 1-4

Using Apps Script

Step 1: In a spreadsheet, go to Extensions -> Apps Script to open the Apps Script editor.

Using Apps Script 1

Step 2: Write the JavaScript code for your macro. For example, here, we create a function to format a header row: 

function makeHeadersBold() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var headerRange = sheet.getRange(1, 1, 1, sheet.getLastColumn()); // First row

  headerRange.setFontWeight(“bold”);

}

Using Apps Script 1-1

Step 4: In the manifest (or manifest.json file) in the Apps Script editor, add your function to the addOn section. This creates the macro and links it to your function. 

Using Apps Script 1-2

Step 5: Save your script and run the formatHeaderRow function. 

Using Apps Script 1-3

You can access the macro in Google Sheets under Extensions > Apps Script > (your function name) or by assigning a custom keyboard shortcut. 

Using Apps Script 1-4

How To Run/Use Macros in Google Sheets?

UI Created Macro

  1. Open the Google Sheet where the macro was created. Go to Extensions -> Macros.
How to run-use macros in GS 1
  1. You’ll see a list of your saved macros.
How to run-use macros in GS 1-1
  1. Click the name of the macro you want to run — it will automatically perform the recorded actions.

You can also assign a shortcut key when saving the macro for faster access.

For Macros Using Apps Script:

  1. Open the required spreadsheet and go to Extensions > Apps Script.
How to run-use macros in GS 1-2
  1. Select the macro you want to run from the dropdown at the top.
How to run-use macros in GS 1-3
  1. Click the Run button.
How to run-use macros in GS 1-4
  1. Authorize the script the first time if prompted. Check the result. A chart is created.
How to run-use macros in GS 1-5

How To Edit Macros in Google Sheets?

  1. Open the Google Sheet containing the macro.
  2. Go to Extensions > Apps Script.
  3. In the script editor, look for the name you gave it.
  4. Edit the code as needed.
How to edit Macros in Google Sheets

How To Import A Macro in Google Sheets?

Let us follow these simple steps to import a macro into Google Sheets.

  1. Open the Google Sheet where you want to use the macro. Click on “Extensions,” then “Apps Script.” It will open a new tab with the Google Apps Script editor.

If you’re importing from a file, first click the + icon next to “Files”` in the left sidebar. Then,  choose Script and paste the macro code there.

How To Import A Macro in Google Sheets 1

Save the script by using the option File -> Save and give the project a name.

How To Import A Macro in Google Sheets 1-1

Close the editor. Again, in the sheets, go to Extensions > Macros > Import macro.

How To Import A Macro in Google Sheets 1-2

You’ll see a list of all functions in the script. Select the macro you want to use.

How To Import A Macro in Google Sheets 1-3

Click Add function shown above to import it as a macro.

Examples

Let us look at some Google sheets macros examples to understand macros in detail.

Example #1 – Show All Hidden Columns in the Active Sheet

In this example, let us try to unhide all the hidden columns in an active sheet of a Google Sheet using a macro. For this, we can use the showColumns method. Let us look at a simple macro that achieves this:

function showAllHiddenColumns() {

  var sheet = SpreadsheetApp.getActiveSheet();

  var lastColumn = sheet.getMaxColumns();

   for (var col = 1; col <= lastColumn; col++) {

    if (sheet.isColumnHiddenByUser(col)) {

      sheet.showColumns(col);

    }

  }

}

Explanation:

function showAllHiddenColumns(): This defines a new function named showAllHiddenColumns.

  1. var sheet = SpreadsheetApp.getActiveSheet(): This gets the active sheet in the current Google Sheets file and stores it in a variable sheet.
  2. var lastColumn = sheet.getMaxColumns(): This gets the total number of columns in the active sheet and stores the value in the variable lastColumn.
  3. for (var col = 1; col <= lastColumn; col++): It starts a loop that goes through each column from 1 to the last column number.
  4. if (sheet.isColumnHiddenByUser(col)): It checks if the column at the current position is hidden by the user.
  5. sheet.showColumns(col): If the column is hidden, this line makes it visible.

Look at the steps below:

Step 1: Go to the Google Sheet which you must check the hidden columns for.

Macros in Google Sheets Example 1

Step 2: Go to “Extensions” > “Apps Script.” Go to the next step.

Macros in Google Sheets Example 1-1

Step 3: Copy – paste the code into a new script, in the Macros Apps Script editor. Save the script.

Macros in Google Sheets Example 1-2

Step 5: Run the script by clicking on the “Run” button.

Macros in Google Sheets Example 1-3

Step 6: Any hidden columns in the active sheet will now be unhidden. 

Macros in Google Sheets Example 1-4

Example #2 – Highlight Rows with “Pending” Status

In this example, we have a table which consists of several tasks. Their status is either In progress, Completed and Pending. Let us identify and highlight all the rows which are pending.

Step 1: Enter the data in a sheet.

Macros in Google Sheets Example 2

Step 2: Under Extensions -> App Script, enter the following code and save it.

Macros in Google Sheets Example 2-1

function highlightPendingRows() {

  var sheet = SpreadsheetApp.getActiveSheet();

  var range = sheet.getDataRange();

  var values = range.getValues();

for (var i = 1; i < values.length; i++) {

    var row = values[i];

    if (row.includes(“Pending”)) {

      sheet.getRange(i + 1, 1, 1, row.length).setBackground(“#FFA500”);

    }

  }

}

Here,

  1. We define a new function named highlightPendingRows.
  2. Next,  var sheet = SpreadsheetApp.getActiveSheet() gets the currently active sheet in the open Google Sheets document and stores it in a variable, sheet.
  3. Next, we get the range of all cells that have data and store it in a variable called range.
  4. We then start a loop in the second row as the index is 1.
  5. var row = values[i] is used to store the current row of data from the values array.

Step 3: Run the code as shown in the previous example and check the result.

Macros in Google Sheets Example 2-2

Important Things to Note

  1. Remember that when you record a macro in Google Sheets, it automatically generates the script for the same. You can go to Extensions -> Apps Script and edit the same.
  2. Macros are only sheet-specific. To run it for another sheet, you have to manually copy-paste the script.

Frequently Asked Questions (FAQs)

What are some important points to be considered when writing macros in Google Sheets?

1. Macros are basically Google Apps Script code. You can access them by going to Extensions -> Apps Script.
2. All the keyboard shortcuts you create for macros must be unique.
3. A sheet can have up to ten macros with shortcuts at any time.
4. Remember to choose the appropriate reference type based on your needs.

How to optimize the use of macros?

To optimize the use of macros and improve performance and productivity, you should:

1. Limit the number of actions a macro performs.
2. Use macro shortcuts that are unique and easy to remember.
3. Use macros only for frequently repeated operations.
4. Remember that Macro scripts are specific to individual sheets.

How to delete a recorded macro?

If you have a recorded macro, go to Extensions -> Apps Script. In the editor, locate the functions that are your recorded macros.

You can delete the function corresponding to the macro you want to remove or its entire file by clicking on the three dots to its right and pressing Delete.

Download Template

This article must help understand Macros in Google Sheets with its formulas and examples. You can download the template here to use it instantly.

Recommended Articles

Guide to What Is Macros in Google Sheets. We learn how to create macros to automate tasks by recording or through App scripts with examples. You can learn more from the following articles. –

Stock Chart in Google Sheets

Themes in Google Sheets

CHISQ.DIST in Google Sheets

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

CHATGPT & AI FOR MICROSOFT EXCEL COURSE - Today Only: 60% + 20% OFF! 🚀

X