If you come from Excel, you might have seen some fancy spreadsheets with clickable buttons that trigger VBA macros. Luckily, Google Spreadsheets and Google Apps Script offer the same functionality. If you are just starting out with Google Apps Script, check out this Google Apps Script macro tutorial for a beginner’s guide. This tutorial will demonstrate how to add custom buttons to a Google Spreadsheet that run Google Apps Script functions when they are clicked. There are also examples of buttons that sort ranges, add timestamps, send emails, and increment cell values.
Let’s Make a Spreadsheet Button
Creating a spreadsheet button is actually easier than creating a custom UI menu. There are just three steps:
1. Create Your Custom Button
There are two ways to create a button in Google Spreadsheets. You can either insert an image or insert a drawing. For this example, we will insert this amazing image of a Gorilla high-fiving a Shark. (I bet you’re glad you picked this tutorial now.)
In the Google Spreadsheet top menu, go to Insert > Image or Insert > Drawing. There are several ways to embed images. You can upload them or add them by URL. That’s how I found this great example. Creating a drawing is easy. Just make a shape that looks like something you might want to click and click Save & Close. The benefit of drawing a button is that you can write descriptive text for the button to signal what the function does. Now that you have a button image on your spreadsheet. Let’s write the function that it will trigger.
2. Create Your Custom Function
For this example, we are going to keep it simple. This custom function, called highFive
, simply displays a pop-up box that says “High Five!” Don’t worry, we will get into more interesting functions later in the tutorial. Go to Tools > Script Editor and define and insert this function.
function highFive(){ Browser.msgBox("High Five!"); }
That was easy. Now in the Google Apps Script menu and save your script file: File > Save.
3. Assign Your Function to your Button
Right-click the image or button and at the top right corner, there will be a small drop-down menu. Click Assign script…
You will be prompted, “What script do you want to assign?” Enter the name of the function that you will use. In this case highFive.
4. Click the Button!
Now for the grand finale! Click the button and you will see the message box saying “High Five!”
If you need to reposition your button, right-click the image. The button will then be outlined in light blue and you can drag the button to wherever you might need it. Placing the button in a frozen row is also a good idea in case you are using a big spreadsheet.
More Useful Functions for Buttons
There are many more useful ways to use Google Spreadsheet buttons. Here are a few.
Set a Cell Value to the Current Date and Time
If you are using a spreadsheet to keep track of times, you might need to know exactly what time you started or completed a task. In that case, you can make your button trigger a function that sets the value of the active cell to the current time.
function timeStamp(){ var rng = SpreadsheetApp.getActiveRange(); var d = new Date(); var timeStamp = d.getTime(); // Unix Timestamp var currentTime = d.toLocaleTimeString(); // eg. 10:23:30 AM HKT rng.setValue(currentTime); }
Send an Email to a Specific Recipient
Using a button is a really convenient way to run a function that takes spreadsheet values as arguments. The following code will allow you to send the contents of the active range as the email address, email subject, and email body.
function email(){ var rng = SpreadsheetApp.getActiveSheet().getActiveRange() var email = rng.getValues()[0]; GmailApp.sendEmail(email[0], email[1], email[2]); }
Highlight the three-cell range, click the button, and your email is on its way. Of course, you can further customize the function with the recipient’s name, for example. But that is for another tutorial.
Increment a Cell Value
Sometimes it’s just easier to click buttons than edit the value inside of a cell. This function named,increment
will add one to the value of cell A1 every time it is clicked.
function increment(){ // define the cell to be incremented var cell = SpreadsheetApp.getActiveSheet().getRange("A1"); // get and set the cell value var cellValue = cell.getValue(); cell.setValue(cellValue + 1); // this increments by 1 but could be any number }
Sorting a Range
This would be a great functionality to attach to a button click. However, the sort()
function seems to have a known bug. Here is how to do this according to the Google Apps Script documentation.
function sortActive(){ // get the active range var rng = SpreadsheetApp.getActiveSheet().getActiveRange() // sort it alphabetically rng.sort() }
Last Notes
Unfortunately, you can not insert the button inside a cell. The button will always be effectively floating above the spreadsheet. But as I said before, you can right-click and drag the button around a freeze the row that the spreadsheet will stay on to keep the button in a static position. I hope this has helped you understand how to use Google App Script buttons in Google Spreadsheets and maybe even give you some ideas. If you are still looking for another example, tell me in the comments. If you have an interesting use for Google Spreadsheet Buttons, share it in the comments as well!
Thank you for these clear instructions! I am a 3rd grade teacher was able to create a table group point tracker using buttons thanks to this info! Much appreciated!
Thanks for the comment, this is the most meaningful comment I’ve ever had on my blog. Good luck!