A Google Sheets button is an essential tool in your Google Sheets arsenal. Buttons in Google Sheets can be an awesome way to easily trigger custom scripts, greatly increasing speed and functionality.
We have already written some tutorials about adding and running custom scripts to Google Sheets in order to expand its functionality. One of these is about importing data from MySQL database to Google Sheets. The custom scripts can either run automatically or you can run them by typing the function name into the cell as an equation. Can we improve on this by, let's say, adding a button that we can click to run the script? Of course! We will learn how to do that in this tutorial. Are you ready?
Step 1: Click Insert on the main menu, then click Drawing in the drop-down list.
The Drawing canvas will appear.
Step 2: Click the Shape option. Several types of shapes will appear. After selecting the shape, draw it across the canvas to whatever size you want it to be.
Afterwards, click the Text box option to add a textbox over the button. Drag it to a certain size within the shape and then type the label that you want.
The button can look like as follows:
Ultimately, the design of the button will be your choice, and given the range of shapes available in Google Sheets, you will not have a hard time finding the one that fits your idea.
Step 3: If you are satisfied with your button, click Save and Close
The button will now appear as an image in the Google Sheets.
We will show you a quick example below, but click here to read our complete tutorial and FAQs for using a Google sheets button to run a script.
For this example, we will use the Google Apps script below:
var server = 'server_url';
var port = port_number;
var dbName = 'database_name';
var username = 'username';
var password = 'password';
var url = 'jdbc:mysql://'+server+':'+port+'/'+dbName;
function readData() {
var conn = Jdbc.getConnection(url, username, password);
var stmt = conn.createStatement();
var results = stmt.executeQuery('SELECT * FROM table_title’);
var metaData=results.getMetaData();
var numCols = metaData.getColumnCount();
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Sheet1');
sheet.clearContents();
var arr=[];
for (var col = 0; col < numCols; col++) {
arr.push(metaData.getColumnName(col + 1));
}
sheet.appendRow(arr);
while (results.next()) {
arr=[];
for (var col = 0; col < numCols; col++) {
arr.push(results.getString(col + 1));
}
sheet.appendRow(arr);
}
results.close();
stmt.close();
sheet.autoResizeColumns(1, numCols+1);
}
Step 1: Click the three dots on the upper-right corner of the button. A drop-down list will appear. Click Assign script.
Step 2: A pop-up box will appear, asking you what script you want to assign to the image. Type the function name, then click Ok.
You are now done!
You can test it by clicking the image. The notification Running script will appear.
When you run the script for the first time in that spreadsheet, you will receive a prompt that the script attached to this document needs your permission to run. Click Continue.
The add-ons created by third-party users for Google apps undergo a validation process by Google to ensure that the add-ons are not intended for malicious purposes by its creators. Click Advanced, then click Go to *name of your script* (unsafe).
The usual procedure for permission requests will appear. Once done, the script will run. You can click the button again to make sure that the script runs as intended.
Voila!
Your button works. You can now use this to improve the dashboard you are creating in Google Sheets.
-How to Create a Google Sheets Dashboard
-Automatically Send Emails from Google Sheets
We hope this article has helped you and given you a better understanding of how to make a Google Sheets button. You might also like our articles on how to assign a script to a button in Google Sheets and how to add a radio button in Google Sheets.
To optimize your workflow, we recommend reading our guide on how to send emails based on a date.