Home

Put "X" to color cell - google app script

The idea

Use the script to make a simple habit tracker in google sheets and access it online. Put action categories in columns like “chess”, “workout”, “programming” etc and put dates in rows and mark cells at the end of the day to have a visual display.

Script explanation

This script does the following:

When a user enters “x” (case-insensitive) into a cell: -It removes the “x”, leaving the cell blank. -It sets the cell’s background color to black (#000000). If a user clears a cell (makes it empty), it resets the background color to default.

To use this script:

  1. Open your Google Sheets document.
  2. Go to Tools > Script editor.
  3. Replace any existing code with the above script.
  4. Save the project (File > Save).
  5. Close the script editor.
function onEdit(e) {
  var range = e.range;
  var value = range.getValue();
  
  if (value.toString().toLowerCase() === 'x') {
    range.setValue(''); // Remove the "x"
    range.setBackground('#000000'); // Set to black color
  } else if (value === '') {
    range.setBackground(null); // Reset to default color if cell is emptied
  }
}
Tags: Idea_to_develop, Google_app_script