How to Create a Button in Google Sheets to Run Scripts

You can create a button in Google Sheets to run Apps Script from its user interface. The button is essential when you design Google Sheets for others who do not know App Scripts.

This post will show you how you can create a button in Google Sheets and attach the Google Apps Script function to it. Users can use this button to run the Apps Script without going to the Apps Script editor. This is very useful to build custom applications within the Google Sheets user interface itself.

You may also read, How to create a Data Entry form in Google Sheets.

How to create the button

At the time of writing this post, there is no button option in Google Sheets.

But, you can draw an image of a button using the Drawing tool.

Follow the below steps to create the button. If you do not like reading, watch the demo video below, which demonstrates the same.

  1. Go to Insert > Drawing.
  2. In the Drawing dialog box, you can either draw a button using the Shape tools, or you can use the image tool. In this case, I will use the Imag tool.
    Go to Image and then to the Search tab.
  3. Then search for a button using the “Button” keyword.
  4. Then click on a button image and click Select.
  5. Resize the button to make it easy to edit.
  6. Draw a text box on top of the button using the Text box tool.
  7. Then type the name of the button inside the text box.
  8. Resize the button/ text box and select font colors to match your requirements.
  9. Click the Save and Close button at the Top Right Corner of the Drawing tool.
  10. Now the button is inserted into the Google Sheet and resize and move to the required place.
Create a Button in Google Sheets

Assign the Apps Script function to the button

In this case, I assume that you have a basic understanding of Google Apps Script. If not, I recommend you to read the Google Apps Script guide from this link.

For this example, I will assign a script to the button that can remove duplicate rows from Google Sheets. So, once you click the button, the duplicate rows are deleted from the Google Sheet. This code sample is available in Google Apps Script tutorials and can be accessed from this link.

Follow the below steps to assign the Apps Script to the button we created above. If you do not like reading, watch the demo video below, which demonstrates the same.

  1. Click on the button to edit it.
  2. Click the menu ( ⋮ ) icon at the top right corner.
  3. Select “Assign Script
  4. Go to Script Editor and copy the name of the function you need to run. In this example, it is “removeDuplicates“.
  5. Go back to Google Sheets and paste the function name inside the text box of the “Assign script” dialog box.
  6. Click Ok.
  7. Now, click on the button to run the code. If this is the first time you run the code, it will require you to grant permission to run the code inside your Google Account.

If you want to edit the button again, Right-click on the button first. It will run the code otherwise.

Assign Script to Button in Google Sheets

You can make a copy of the Google Sheet with the sample code from the following link.

Wrapping Up

There is no direct option to insert a button in Google Sheets. However, you can draw and insert a button image using the Google Sheets Drawing option. Then you can assign your Apps Script to this button. That way you can let users easily run the Apps Script without going to Script Editor.

Leave a Comment

Share via
Copy link