Add a Google Sheets Button to Run Scripts

Video google sheet button to run script

Imagine you have an invoice template that you regularly use, but it’s tedious to clear all the values every time you need to start over. Well, you can add a button to Google Sheets to run scripts and clear your invoice with just one click.

Google Sheets Button

Let’s start by creating a basic invoice template with placeholders to hold information:

Invoice Template with Google Sheets Button

The user can enter information in cells B5, B8, E5, and E6 (highlighted in yellow).

In the script editor, accessible via Tools > Script editor, add a very simple script to clear these specific cells:

function clearInvoice() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const invoiceNumber = sheet.getRange("B5").clearContent();
  const invoiceAmount = sheet.getRange("B8").clearContent();
  const invoiceTo = sheet.getRange("E5").clearContent();
  const invoiceFrom = sheet.getRange("E6").clearContent();
}

You can run this function from the script editor, and it will clear the content of the invoice.

But that’s tedious.

You don’t want to have to open the script editor every time. You want to do it directly from your Google Sheet.

To do this, add a Google Sheets button.

You add a button via the Insert > Drawing menu.

This opens the drawing editor where you can easily add a shape and style it to look like a button:

Drawing of a Google Sheets Button

When you click on Save and Close, this drawing is added to your Google Sheet. You can click on it to resize or drag it to reposition it.

To assign a script, click on the three dots in the top right corner of the drawing and select Assign script:

Assigning a Script to a Google Sheets Button

Then, enter the name of the function you want to run from your Apps Script code. In this example, choose the clearInvoice function (like the code above!).

Now, when you click the button, it will clear the invoice for you!

Button with Apps Script in Google Sheets

Note: To edit or move the button once you have assigned it to a script, you now need to right-click on it.

Check out the Google documentation for more information on the drawing feature.

Find more interesting articles on Crawlan.com.

Related posts