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.
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.
Spacer is a Free Google Add-On that creates row spaces by column categories.
I use it as an academic administrator to create a space or two at the end of each class section to create a visual break between sections. This makes it easier for my staff to quickly identify class sections.
Allow you to select a column with the categories you want to use to space your data.
Set the height of your header – so that is not spaced too.
Set the thickness of your spaces.
Set the color of your spaces
Set starting and end position of your color filler.
How to use Spacer
Before You Start:
Before you start, create a Google Sheet with the data in it you want to space.
Select All the data.
Go to the menu bar and select: Data>Sort Range... and select the column you want to sort by. This will be the column you will use to space your data by the categories in that column.
In a Google Sheet, select the Add-Ons menu.
Select: Spacer>Create Spaces
A sidebar will appear to the right.
Choose the column you want to use to Space your data.
Choose how high your header is.
Chose how thick or deep your want your header to be.
If you want a color, select a color that you want to add to your spaces.
Select where you want to start the color and end the color.
If you are not happy with the color, space, color width or made a mistake, simply click: UNDO
I keep getting the following popup:
This is because the starting value of your Color Width is greater than your End value:
I have a list of people with their favorite color and fruit. Let’s say I want to sort the Google Sheet by favorite color (Fav Color) and put a space at the end of each color category to make it easier to read.
My data looks like this:
To space this data we must do the following:
Select all the data: ctrl+shift + a
Sort the data by column B. Select: Data> Sort range
Click the: Data has a header row tick box. Select the column: Fav Color. Then click the Sort button.
Your data is now sorted by favorite color:
Time to use the Spacer. Select: Add-ons > Spacer > Create spaces
Spacer will open at Sidebar on the right:
We want to space our data by Fav Color which is column B. For Column, select: B
We have a single Row Header so we can leave the header selection at 1:
We want to give our spaces a thickness of 2 rows:
Next, we choose a color. Let’s choose purple. A color picker will pop up (the picker may vary depending on your system) select the color and click: OK:
This is the result:
Now choose where to start and finish the color fill for the space. I’ll start it at column A and end it at column E.
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.