Skip to Content

How To Add Sort Button In Google Sheets Using Apps Script

Google Sheets is a powerful tool for organizing and analyzing data, but managing large datasets can sometimes become time-consuming, especially when sorting rows becomes routine. 

Wouldn’t it be great if you could simplify this process with just a click of a button? That’s where Apps Script comes into play. 

In this article, we’ll explore how to add sort button in Google Sheets using Apps Script, even if you’re a beginner with little to no coding experience.

Scenario/Use Case

Imagine you’re a small business owner keeping track of your inventory in a Google Sheets document. Your sheet contains a list of products, their quantities, and their prices. As your business grows, this list becomes more extensive, making it harder to find specific products quickly. You often need to sort the products based on various criteria, such as alphabetically by product name or price, to identify your bestsellers.

Manually rearranging rows whenever you need to sort the data can be tiresome. This is where adding a sort button in Google Sheets using Apps Script becomes immensely helpful. 

Instead of spending valuable time rearranging rows, you can create a button that automates the sorting process for you. With a simple click, your sheet will rearrange itself based on your chosen criteria, allowing you to focus on more important aspects of your business.

In the following sections, we’ll guide you through the process step-by-step, explaining each stage in a beginner-friendly manner. 

By the end of this article, you’ll clearly understand how to add sort button in Google Sheets using Apps Script and will be able to implement this feature in your own spreadsheets.

So, let’s dive into the world of Apps Script and discover how you can streamline your data management tasks by adding a sort button in Google Sheets using Apps Script.

Add sort button in Google Sheets Using Apps Script- Practical Example

After discussing the basics and giving an example situation, we are sure you’re excited to start applying what you’ve learned. So, let’s skip the long explanations and dive right in.

To show you how to add a sort button in Google Sheets using Apps Script, we’ll work with a simple example. Here’s the sample data we’ll be using:

Here is how to go about it:

Step 1: Draw Your Sort Button

Let’s start by creating our sorting button. To do this, go to the Insert menu, scroll down, and choose the Drawing option.

Once you’ve picked the Drawing option, a window called Drawing will pop up. Here’s where you’ll create your button using the tools available. Don’t worry; we’ll keep it simple. Our goal here is to make a square shape and then write the word “sort” inside it.

After doing what we explained earlier, click “Save and Close.” Now, go back to your spreadsheet. You should see the button you drew right there.

Check out how ours turned out:

Step 2: Launch Apps Script Editor

Sure, we’ve made the Sort button, but here’s the scoop: the button won’t actually do anything unless we give it a special job using Apps Script. So, let’s not waste time and create that special function for our button.

To make it happen, go to the Extensions menu. From the choices there, click on Apps Script. This will open up the Apps Script Editor for us.

After launching the Apps Script editor, it should look something like this. 

Step 3: Write the Code

Having launched the Apps Script editor, click on the code window and delete everything you currently have there. With that done, copy and paste the following code into the code window: 

function sortDataByQuantity() {

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

var sheet = spreadsheet.getActiveSheet();

var range = sheet.getRange("B3:B7"); // Adjust the range based on your data

range.sort(2); // 2 represents the second column (Quantity)

}

Step 4: Save The Project

After writing your code in the provided window, click the floppy disk icon to save the project. This action will save the project. 

Step 5: Assign Script To Button

With our Apps Script function now created. We need to now assign the script to the button we created earlier. And guess what, the process is super easy. 

Simply head back to your spreadsheet and click on the button you created earlier. You’ll see a three-dot icon; click on that. From the options available, select the option for Assign script. 

This action will launch a small pop-up window where you need to assign the script’s name. Simply use the name you used to save the Apps Script function. For this example, we will use the following name: sortDataByQuantity

With the name now entered, click OK, and your script should be assigned. 

Step 6: Test Button

After assigning the script to our button, let’s quickly test it to see if it works. The video below provides a better insight. 

If you watch the video closely, you’ll see that the column for Quantity was sorted after clicking the sort button we created. 

Now, you can see that our button works perfectly. We are sure you now see how easy it is to add sort button in Google Sheets using Apps script. 

Final Thoughts 

Congratulations, you’ve taken your first steps into the world of adding functionality to your Google Sheets using Apps Script. We’ve covered a lot. We first showed you how to add sort button in Google Sheets using Apps Script. From there, we showed you how to make the button actually work. Let’s quickly recap:

  • Button Creation: Drawing and labeling your sorting button is just the beginning. Remember, we’re simplifying our tasks here. Think about what you want to sort and draw your button accordingly.
  • Apps Script Function: Your button won’t do its magic without the right spell – or in our case, the right script. We crafted a basic script to sort data by the “Quantity” column, but you can easily tweak it to match your needs.
  • Apps Script Editor: Don’t be intimidated by the Apps Script Editor. It’s just a tool to make your button functional. You paste your script, save it, and you’re good to go.
  • Click to Sort: The best part? With everything set up, you can now enjoy sorting your data by clicking a button. No more tedious manual sorting.

Remember, the purpose of all this is to save you time and make your spreadsheet work for you. Experiment, play around, and don’t hesitate to explore more complex functionalities as you get comfortable. The more you learn, the more power you’ll have at your fingertips to streamline your data tasks.