change cell color based on checkbox google sheets

"Dynamic Cell Colors: Change Google Sheets Based on Checkbox Status"

By James Lewis
AD

Looking to jazz up your Google Sheets with a touch of color? Ever dreamed of making your cells change their hue based on a simple checkbox? You're in luck! Today, we're going to explore an exciting world of conditional formatting and Google Apps Script to make your humble spreadsheet dance to the whims of your checkbox. Let's dive in!

First, it's crucial to understand that while Google Sheets offers a wealth of formatting options, changing cell color based on a checkbox alone isn't possible with built-in features. Don't worry, though! A combination of conditional formatting and a bit of Apps Script can get the job done.

Setting Up Your Checkbox and Cells

Before we dive into the Apps Script magic, let's set up our checkbox and cells.

AD
CHECK the Conditional Formatting - Teacher Tech with Alice Keeler
CHECK the Conditional Formatting - Teacher Tech with Alice Keeler
How to Highlight a Row in Google Sheets Based on a Checkbox
How to Highlight a Row in Google Sheets Based on a Checkbox
Count the Colors in Google Sheets - Teacher Tech with Alice Keeler
Count the Colors in Google Sheets - Teacher Tech with Alice Keeler

1. In the top-left corner of your Sheet, create a checkbox. To do this, simply click on the cell, then under the 'Data' menu, select 'Create a checkbox'.

2. Now, let's set up our target cells. These are the cells that will change color based on your checkbox status. Select the cells you want to format.

How To Create Checklists with Google Sheets
How To Create Checklists with Google Sheets
How to hide cells in Google Sheets
How to hide cells in Google Sheets
Google Sheets: Check Multiple Checkboxes At The Same Time - Teacher Tech with Alice Keeler
Google Sheets: Check Multiple Checkboxes At The Same Time - Teacher Tech with Alice Keeler

Using Conditional Formatting

Conditional formatting is the first step in making your cells dance. Here's how you can use it:

1. Select the cells you want to format. Then, right-click and select 'Conditional formatting' > 'Format cells if...

AD

2. In the formula bar, input the following formula: =CheckBox1 (assuming your checkbox is in cell A1). This will check if your checkbox is selected or not.

📊 Color Coded Dropdowns in Google Sheets
📊 Color Coded Dropdowns in Google Sheets
The Reason to Love Google Sheets: Make Checkboxes - Teacher Tech with Alice Keeler
The Reason to Love Google Sheets: Make Checkboxes - Teacher Tech with Alice Keeler
Google Sheets Checklists
Google Sheets Checklists
How to Insert a Checkbox in Google Sheets
How to Insert a Checkbox in Google Sheets
How to Use Checkboxes in Google Sheets
How to Use Checkboxes in Google Sheets
How to Change Status Based on a Checkbox in Google Sheets
How to Change Status Based on a Checkbox in Google Sheets

Adding Color on Checkbox Selection

Now, let's make those cells change color when the checkbox is selected:

1. Choose the formatting style you want (we'll use 'Custom cell color').

AD

2. Pick your desired color. Let's go with a vibrant red for this example. Then, click 'Done'.

Google Sheets Aesthetic
Google Sheets Aesthetic
a laptop with google sheets on it and the words google sheets for teachers above it
a laptop with google sheets on it and the words google sheets for teachers above it
How to Change Your Google Sheets Theme Color
How to Change Your Google Sheets Theme Color

Now, your cells should turn red when the checkbox is selected. But what about when the checkbox is unselected? Let's take care of that too.

Reverting to Normal: Unchecking the Box

Unfortunately, conditional formatting can't revert the color when the checkbox is unselected. That's where Apps Script comes in.

AD

Apps Script is a JavaScript-based service by Google that helps you automate tasks across Google products. Let's create a simple trigger to revert our cells' color when the checkbox is unselected.

Creating an Apps Script Function

1. Click 'Extensions' in your menu, then 'Apps Script'.

2. Delete any existing code in the 'Code.gs' file, then paste the following:

```javascript function onEdit(e) { var s = SpreadsheetApp.getActiveSpreadsheet(); var r = s.getActiveRange(); if (r.getA1Notation() === 'A1' && e.value === false) { var ss = SpreadsheetApp.openById('YOUR_SPREADSHEET_ID'); var sheet = ss.getSheetByName('Sheet1'); var range = sheet.getRange('B1:D5'); range.setBackground(null); } } ```

Replace 'YOUR_SPREADSHEET_ID' with your actual spreadsheet ID and 'Sheet1' with your sheet's name. Also, adjust the range ('B1:D5') to match your target cells.

This script will run whenever the cell in A1 changes. If the change is to false (the checkbox is unselected), it will clear the background color of our target cells.

Setting Up the Trigger

1. Save your project with a name, like 'colorChange'.

2. Under your project, right-click on 'onEdit' and select 'Edit triggers...'.

3. Click the '+ Add Trigger' button at the bottom, then select the following options:

  • Choose which function to run: onEdit
  • Select event source: From Spreadsheet
  • Select event type: On edit

Click 'Save', and you're done!

Now, whenever your checkbox is unselected, your cells will revert to their normal color. Voila! Your cells are now dancing to the whim of your checkbox.

Remember, Google Sheets can be as simple or as complex as you want. Experiment with different formulas, functions, and scripts to make your spreadsheets truly your own. Happy coding!

AD