In this article we will show you how to assign a script to a button in google sheets in just a few simple steps.
Click here for our overview button on the Google Sheet Button.
To access Google Apps Script, click Extension, then select Apps Script.
A new tab will open where you can add the script.
Add the function in the space given. For example, we want a function that outputs “Hello world” in a pop-up box when a button is clicked. The function is as short as follows:
function myFunction() {
Browser.msgBox("Hello World!");
}
Click the Save button or press Ctrl+S on the keyboard to save the script.
Take note of the name of your function. We will need it in the next steps. For this example, the function is named myFunction, the default set by Google Apps Script.
We need to create a figure that will serve as a button. Click Insert on the main menu, then select Drawing.
The drawing canvas will appear with some objects you can add to it.
For our button, we will have a rounded rectangle with the label “Hello World!”. First, we click Shape.
We can add a wide variety of shapes through this option. We will select the Rounded Rectangle.
Click and drag the cursor through the canvas to create the rounded rectangle in its desired size.
We will now add the label “Hello World!” on top of it. Select the Text box from the main toolbar of the Drawing canvas.
Click and drag the cursor through within the shape to create the rounded rectangle in its desired size.
Click Save and Close on the upper-right corner of the Drawing canvas. The drawing will appear on your active sheet.
You can resize the button image before connecting it to a script. Here are the steps:
1. Click the image
2. Hold-click one of the blue boxes that appear on its borders
3. Drag the cursor until the preferred size of the button is achieved
After resizing, you can reposition the button by clicking it and then dragging it to where you want it located:
Now that we have a button, we can connect the script to it. Hover your cursor over the button and a set of three dots will appear on its upper-right corner. Click it.
A list of options will appear. Select Assign script.
A small box labeled Assign script will appear, asking you to input the script function. Type the function name set in the script. For our example, it’s simply the default myFunction.
Once you have added the function name, click OK.
As you will run the function for the first time, Google Sheets will ask you for authorization with a box labeled Authorization Required. Click Continue.
A new window will appear, allowing you to select the Google account associated with the spreadsheet containing the button.
After selecting the account, a warning will appear reminding you that Google has not verified the app. Click the small link on the lower-left corner labeled Advanced.
Another reminder will appear to continue only if you understand the risks and trust the developer. Click Go to Untitled project (unsafe) or whatever the project name is in Google Apps Script.
A new page will appear listing the permissions needed by the script. Click Allow.
Voila! The button will now run your script.
No. You only need to do this once for every script, and in the subsequent clicks on the button the script will automatically run.
Yes! The same three dots will appear on the upper-right corner of the image when you hover your cursor over it. Select Assign script just like what has been highlighted in the previous steps.
Just make sure that when you insert the image, select Insert image over cells.
Yes. Follow Step 3 but instead of a left click, you can either right-click the button or hold Ctrl then left-click the button.
We hope this article has helped you and given you a better understanding of how to create a button to run a script in Google Sheets. You might also like our articles on adding a script in Google Sheets and how to add a button in Google Sheets.
To optimize your workflow, we recommend reading our guide on how to set reminders in Google Sheets.