Google Sheets – Sum or count values based on cell color

Overview

If you’re using Google sheets and you have a list of amounts that you want to sum or count based on the background color of the cells, there’s no built-in function to do it. However, there are a relatively easy set of steps to make your own functions to get it done. You’ll be able to count based on cell color and you’ll be able to sum as well.

**This technique has been updated. There is a new YouTube video showing an easier way with this Sheet**.

Custom formulas in action

Cells being summed and counted
Light blue cells being summed and counted by color

Count by cell color

If you look in the live spreadsheet, you will see the custom formulas being used for summing and counting by cell color. The counting is done by a formula with the nice little name of countColoredCells which takes the range and cell with the background color that you want as its two inputs. You want to count B4 through B9 and you want to count the number of cells that have the background color used in cell B8, which is the light blue cell with the dotted line around it.

Formula for count by cell color
Formula for count by cell color

If you change the second parameter above from B8, a light blue cell, to B5, a yellow cell, the result would be 3 because there’s three yellow cells.

Sum by cell color

sumColoredCells is returning a 24 which is the sum of all the light blue cells. It is similar to the count function – it is summing the colored cells in B4 through B9 that are formatted like B8.

Formula for sum by cell color
Formula for sum by cell color

Creating custom formulas

How do we make these functions and any other custom function that you’re so inclined to write? First, go to Tools and you go to Script editor... Don’t worry, we’re just going to copy and paste code below. The code for countColoredCells was obtained from this page at iGoogleDrive.

 /**
* @param {range} countRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in countRange
* @return {number}
* @customfunction
*/

function countColoredCells(countRange,colorRef) {
  var activeRange = SpreadsheetApp.getActiveRange();
  var activeSheet = activeRange.getSheet();
  var formula = activeRange.getFormula();
  
  var rangeA1Notation = formula.match(/\((.*)\,/).pop();
  var range = activeSheet.getRange(rangeA1Notation);
  var bg = range.getBackgrounds();
  var values = range.getValues();
  
  var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  var colorCell = activeSheet.getRange(colorCellA1Notation);
  var color = colorCell.getBackground();
  
  var count = 0;
  
  for(var i=0;i<bg.length;i++)
    for(var j=0;j<bg[0].length;j++)
      if( bg[i][j] == color )
        count=count+1;
  return count;
};
   

And this code for sumColoredCells was obtained from the same blog, but a different page.

 /**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/

function sumColoredCells(sumRange,colorRef) {
  var activeRange = SpreadsheetApp.getActiveRange();
  var activeSheet = activeRange.getSheet();
  var formula = activeRange.getFormula().toString();
  formula = formula.replace(new RegExp(';','g'),',');
  
  
  var rangeA1Notation = formula.match(/\((.*)\,/).pop();
  var range = activeSheet.getRange(rangeA1Notation);
  var bg = range.getBackgrounds();
  var values = range.getValues();
  
  var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  var colorCell = activeSheet.getRange(colorCellA1Notation);
  var color = colorCell.getBackground();
  
  var total = 0;
  
  for(var i=0;i<bg.length;i++)
    for(var j=0;j<bg[0].length;j++)
      if( bg[i][j] == color )
        total=total+(values[i][j]*1);
  return total;
};   

Script editor

After you go to Tools then Script editor, you come up with a blank screen. But if you don’t, just do a new script file. Paste the code into the blank window. Repeat for each code section above and name them countColoredCells and sumColoredCells. For each file, the script editor puts the “.gs” at the end of the file name which indicates that it is a Google Script. After you make these two, save them, come back to your spreadsheet, type in the formulas, and it should work for you. See the video and live spreadsheet for further clarification.

Watch the video

 Live examples in Sheets

Go to this spreadsheet for examples of counting cells by cell color that you can study and use anywhere you would like.

Using a plugin

As an alternative to the technique above, you may want a plugin to do the heavy lifting for you. I like to use a plugin called Power Tools. This will give you a menu option with, among other things, the ability to run functions by color.

The Functions by Color Menu
Functions by Color Menu

Disclosure: This is an independently owned website that sometimes receives compensation from the company's mentioned products. Prolific Oaktree tests each product, and any opinions expressed here are our own.

Snipping Tool in the Start menu

How to insert a YouTube video link into a Google Doc with a thumbnail

They are many reasons why you may want to inserts a YouTube Video link into your Google Doc but there is no menu option for doing this. Following is a series of steps that will allow you to create a link to a YouTube video in your document complete with a clickable thumbnail. To the reader of the document, this will be an obvious YouTube link that looks like one from the web. When clicked, it will bring the reader to the video.

These steps give you a clickable thumbnail, but they will not embed the player into your document.

Screen clip

Start by finding the video that you want on YouTube. Once you have found the video, use the Snipping Tool built into windows and grab a screen clip of the video thumbnail.

Snipping Tool in the Start menu
Snipping Tool in the Start menu

To find the Snipping Tool hit your Start button and type “Snip”. Press enter once it is highlighted.

Save the snip to your computer.

Video explanation which is actually a video!

Insert the images

Go back to your Google Doc and place your cursor where you would like to insert the video. Go to Insert and then Drawing.

Insert drawing option
Insert drawing option

While you are in the insert drawing window, you will be inserting two images.

Insert image option
Insert image option

For the first image, use the upload option and choose the screenshot that you just took with the Snipping Tool.

Upload image option
Upload an image

After inserting your screen shot file, go back to Insert image, then More, and choose search. Search for “YouTube Play Button” and choose which one you would like.

Search YouTube button
Search for a YouTube button

All ready

Shrink this button and move it over the middle of the screen shot. Your drawing is now ready. You have a thumbnail pictures of your YouTube video that will take you directly to the YouTube video page with it ready to play.

Finished YouTube thumbnail image
Finished YouTube thumbnail image

Follow image below for the live Google Doc with a YouTube link

docs share icon

Google Sheets Charts – Deciding which chart to use

Which chart type you use depends on what type of data you have and what story you want to tell with it.  Google Sheets offers many types of charts.  Picking the chart that is most appropriate for each situation can make the difference between someone understanding your data or just being more confused. 

What are you trying to show?

  • Relationship
    • How do two or three variables compare to each other?
    • Two variables – Scatter Charts
    • Three variable – Bubble chart
  • Comparison
    • You are tyring to contrast two or more variables in relation to each other. Imagine that you have a long list of variables and you want to see how they relate to each other. Do they move in unison? Is there a close correlation between the values?
    • Among items
    • Over time
  • Distribution
    • You want to show where the data falls when aggregated together.
    • One variable – Historgram
    • Two variables – Scatter Charts
    • Three variables – 3D Area chart
  • Composition
    • What is my data made up of?
    • Over time
      • Column chart
      • Area chart
    • One point in time
      • Pie chart
      • Waterfall chart
      • Stack column chart?
Scatter chart with some correlation

Scatter Charts in Google Sheets – Step-by-step with examples

What is a scatter chart?

A scatter chart (AKA scatter plot or XY graph) uses points along a two-dimensional graph to show the relationship between two sets of data. Its simplicity can make it quite effective at cutting through the noise of large amounts of data in a Google spreadsheet. Once you can see the relationship, it can be used to predict future outcomes within certain confidence levels.

When to use a scatter chart

Before you jump into using a scatter chart, be sure that it is the right type of chart for you. This chart type decision tool can help you decide.

As an example, if you want to see if more people will use the water park as it gets warmer, a scatter chart could be a good tool. If you use any more than two variables, the chart can start to appear confusing and thus reduce its usefulness.  

These charts can be customized in many ways. The first example below has had its points reduced to 2 px, a linear trendline added, and the correlation shown in the upper right hand corner of the chart. These options can be found on the customize menu when inserting a chart.

Customization Menu
Customization menu
Example scatter chart
Example scatter chart

Video Explanation

Correlation

Mathematically speaking, creating a scatter chart lets you visualize the correlation between data points, if there is any. In the example above, the correlation is .928, with the highest value of any correlation being 1. The chart was created in Google Sheets using random values for the Y axis and (Y + random values) for the X axis.  The random value range was kept small so that it would create a tight correlation. Most users for which a spreadsheet chart is made will probably not remember what a correlation value means. A trendline is the layman’s tool for seeing the correlation.

Trendline

You can draw a line through the data along the path which is the best fit based on the points already on the chart. You can see how close the correlation is to 1 or negative 1 by simply looking at the distance that the dots are away from the line.  This line can be used to extrapolate correlations outside of the current data. If you are trying to find data points inside the current set, it is called interpolation. This line can be straight (linear), or it can be a curve (exponential or polynomial).

Choices of trendlines
You can choose from several types of trendlines

Choices of trendlines
You can choose from several types of trendlines

Linear

Below is a simple scatter chart with a correlation value of 1. This means that each of the two variables move in lock step with eachother in the same direction. If the left shoes size increases by one, so does the right. The line of best fit with a perfect correlation is linear and extends 45 degrees from left to right. Note that it will only be 45 degrees on the chart if the scale of the x and y axis are the same scale.

Scatter chart with correlation of 1
Scatter chart with correlation value of 1

Some types of data could have a negative correlation. For example, the relationship between BTU’s needed to heat a room and the outside temperature would create a graph that slants down as it moves along the x axis.

Scatter chart with correlation of -1
Scatter chart with correlation value of -1

Of course, not all data are so highly correlated. If it were, there would be no “scatter” in a scatter chart. This chart shows data with a correlation between -1 and 1. Note that the slope calculation and correlation value were included in the chart by choosing “Use Equation” as the label and placing a checkmark next to R2 to the correlation in the customization are of the chart menu.

Trendline Labels
Setting labels for trendlines

Scatter chart with some correlation
Scatter chart with correlation value shown on chart

Exponential

Exponential trendlines are a good fit for data that increase or decreases at a constantly increasingn rate.

Scatter chart with an exponential trendline
Scatter chart with an exponential trendline

Polynomial

A polynomal trendline should be used when data flucuates between values. Note that the polynomial trenline is more tightly correlated with this data. The data is the same as the data used for the exponential trendline.

Scatter chart with a polynomial trendline
Scatter chart with a polynomial trendline

Error Bars

To illstrate that your data in not precise and may be within a certain range, error bars can be used. These bars are found in the customization menu and be set for different ranges in percentage or absolute terms. The chart below shows error bars added to a simple scatter chart to show uncertainty in the data. They were created by using a constant value of 2.25. These can be used with our without a trendline depending on the point that you are trying to make to the viewer of your chart.

Scatter chart with error bars
Scatter chart with error bars
Double Underline

Google Sheets – Create a double underline bottom border

Lots of spreadsheet users can’t imagine life without their double underline feature. There has been much gnashing of teeth for those making the switch to Google Sheets when they find that their faithful double underline function is not there. This is one of the few ways in which Sheets is inferior to Excel. What is one to do? Before you abandon ship, you should realize that there are a few decent work arounds for the issues. None of them are perfect, but you should be able to get the effect that you are looking for.

Update – As of late February 2017, a double underline is available in Google sheets without needing to use any workarounds.

Double Underline
Double underline option

The remainder of this article is preserved just in case you want to know how hard we had it way back when!

Bottom border and underline

The first option is to use a single bottom border on a cell and then use the underline font style. As you can see, this does creates two lines, but the top line is only as wide as the characters in the cell. Therefore, it won’t really look like a true double underline.

underline font
Single underline with an underline font format
Watch the video

Underscores and strikethrough

The next option is to type use insert a line of underscores (to the right of the zero hold down shift) in a cell and then apply the strikethrough font style. This will create a double line. The disadvantage of this technique though is that it will be a fixed width. Therefore, if the width of the cell changes, this “underline” will not be the right size anymore.

dash strikethrough
Underscores with the strikethrough
dash strikethrough align right
Underscores after column width changes

Two single bottom borders

The next option is to use the single bottom border on two rows and then shrink the row height to make it look like a double underline. In my opinion, this is most likely going to be your best bet.

two single borders
Two bottom borders
two single borders
Two bottom borders with a shortened row