Have you ever copied and pasted something in Google Sheets only to be frustrated with the fact that it stubbornly refuses to paste the column widths? I mean, everything else is perfect, the formatting, the formulas the comments, they all were pasted across just fine. However, that dastardly column width just does not budge.
I was working on a Google Apps Script project lately in Google Sheets that set up parameters in a sidebar and then ran the process once the user clicked the “Submit” button. The problem was that the process was taking a while and that “Submit” Button was ripe to be clicked multiple times by the impatient user, before the server-side code could even finish it’s operation.
To fix this I needed to disable the submit button once it had been clicked and then enable it again once the server-side process was complete. Here, I need to:
Do something awesome with it server-side.
Upon the completion of the server-side awesome, call back to the html file and enable the button again.
I have a very simple Google Sheets Side Bar with a “Submit” and “Cancel” button. When the “Submit” button is clicked it calls the function submittington (can he get any more creative? No. No he can’t).
This function then disables the “Submit” button sends a variable to the client side code.gs and displays it stylishly in the sheet multiple times for the users viewing pleasure. After the code.gs function is executed, it calls back to the client-side submittington function and enables the button.
Then, what’s the trick?
The thing is, the explanations were a bit vague for me to work out clearly so I really needed to create an example of my own to work through how to use it. The example below breaks down the steps to create file relationships to make your code look neater. It also dives into some uses of template statements in html.
The Final Result
Imagine that you have a Google Sheet that you have sorted by a certain column. You might be sorting by the surname of your sales team, class sections or regions. To make the sheet easier to read for your team, you want to alternate the background colors after each category in your sort column is complete.
I have the following list of numbers in column 1 and 2. I have sorted these numbers by my Grouping Column of planets in column 3. After each grouping, I have alternated the background color to make the transition easier to read.
Quite often I will need to get the range of each category in an item and do something with it in Google Sheets. For example, I work in education, I will often have rows of students that are categorized by class sections. I will then be asked to do something like those sections like put each section of students in their own sheet or set alternating colors for each section to make the sheet easier to read.
Alternatively, you may want to grab sales data by region or sales items by a particular category and work with them in Google Apps Script.
Let’s say we want to get the range values of the follow sheet by planets. We will be categorizing our data by the Grouping, column C.
First we don’t want to take into account the headers on the first row. Our first grouping will be Mars, followed by Jupiter, Uranus and Mercury. We want to know which column that each category starts on and how many of that category there are.
You’ve probably come across the problem where you need to know when a piece of data has been added to your spreadsheet. You probably have been equally frustrated that there is no out-of-the-box function that will do just this.
You’ve tried TODAY() and NOW(), but they change dynamically. What you really need here is something that does not change.
Let’s look at two workarounds that can help you out with this problem.
I was trying to rush out some Google Apps Script code to deal with a task on Google Sheets recently. Basically, I had to search through a heap of data and find certain values and do something too them.
My column was reaching across the page to something like Column BK and my rows were over 1000 deep. Running this code was taking forever!!!
My immediate instinct was:
What have I done wrong?
…and my instinct was right.
The Good and Bad Way to Search Through Code
So after looking at my code again, I discovered that for some reason I go it into my head that I should be searching each cell for the value I needed and then doing something with it.
Sounds logical right? It’s sorta what you are meant to do.
The problem is that I was calling the sever and asking for the range in each cell as I was looping through the entire document. This is super costly and inefficient in terms of time.
Also, if you do run a costly code like this, then you will get a little red light in your Script tool bar that represents your Execution Hints:
Clicking on Execution Hints and expanding the side bar with provide you with a far-too-deserving-polite dressing down about your slow and server costly code.
So instead of calling the server for each cell I need to get the full range of the data I am working on, search through it client-side to find what cells require modifying and then invoke the modifications.
Let’s look at a simple example:
In this example I want to search through all the results over multiple quizzes and if there is a dash “-” or a zero “0” change the background accordingly.
Yeah! Yeah! I can do this with conditional formatting, but this is an example, damn it, Jim!!!
Below is a sample of the data I will use and here is the link.
Both Good and Bad examples have the same end result. The result should look like below:
Hopefully everything is self-explanatory here. We call the spreadsheet first and then look for the active sheet. Inside the active sheet we want the range of the data (rangeData) which will contain all the data in the range. We will use that data to get the last row and column number of the data. Finally we will call the server to get he range we want to work with (searchRange).
Once done, we will create our function, onOpen(). When it is called it will create a menu called Checker with the sub menu Bad Way and Good Way. This isn’t necessary, but it might be easier for you to physically test the difference in the slow method versus the fast (correct) one.
The Bad Way
As mentioned above in the Bad (slow) Way we call the server each time to look at what is in a cell.
The Bad Way
BAD - Go to each cell and see if it contains a value
and then fill in the background if it contains a dash or
//Loop through each column and each row in the sheet.
As I loop across the columns and then the rows, I am using my search range to get the value of the cell in Line 25. This means I am contacting the server a total for 436 times. This significantly slows things down.
The Good Way
GOOD - Create a client-side array of the relevant data
search through the array and if there is a dash or zero,
then add the relevant background color.
// Get array of values in the search Range
// Loop through array and if condition met, add relevant
// background color.
In the preferred approach I am taking the array that I created from searchRange.getValues() in Line 36 and searching through it before I make my calls to change the background when a dash or a zero occurs.
Why is this better?
I only make server call to collect the range data once. Then client-side (on my computer in this instance), I do all my searching before calling Google who collects all the changes in a cache until the loops are done before creating background colors all at once.
Take a look at the speed differences over ten tests:
The Good Way is the clear winner. You can try it out for yourself if you have been playing along by going to <View><Execution Transcript> in the Script
The take home from this is that, make as little calls to the server as possible. It significantly improves your speed.
You sure can. Google has a fully supported script editor that you integrate with your Sheets, Docs, Forms, Slide, Gmail, Calendar and pretty much every aspect of the Google Suite. It’s name: Google Apps Script.
Google Apps Script allows you to do all sorts of things like building short code to modify sheets and docs, create macros, develop add-ons mess around with gmail and so much more.