To achieve the desired functionality in Google Sheets, you can use an Apps Script that triggers on every edit. This script will compare the new value with the old value in cells C4 to C17 across all sheets in your Google Sheets file. If the number increases, it will change the background color to light green; if it decreases, it will change it to light red. Here's how you can set it up:
Step 1: Open the Script Editor
- Open your Google Sheets file.
- Click on
Extensions in the toolbar at the top.
- Select
Apps Script.
Step 2: Paste the Script
In the script editor window that opens, paste the following code:
function onEdit(e) {
try {
var range = e.range;
var sheet = range.getSheet();
var editedRow = range.getRow();
var editedColumn = range.getColumn();
// Check if the edited cell is in column C and rows 4 to 17
if (editedColumn == 3 && editedRow >= 4 && editedRow <= 17) {
var newNum = range.getValue(); // Get the new numerical value
// Get the old value from the event object
var oldValue = e.oldValue;
// If oldValue is undefined (i.e., the cell was previously empty), do nothing
if (oldValue === undefined || oldValue === null || oldValue === '') {
return;
}
// Replace comma with dot in oldValue to handle decimal commas
oldValue = oldValue.toString().replace(',', '.');
// Parse the old value to a number
var oldNum = parseFloat(oldValue);
// If the new value or old value is not a number, do nothing
if (isNaN(newNum) || isNaN(oldNum)) {
return;
}
// Compare the new and old values to set background color
if (newNum > oldNum) {
// Set background to light green
range.setBackground("#C6EFCE"); // Light green color
} else if (newNum < oldNum) {
// Set background to light red
range.setBackground("#FFC7CE"); // Light red color
} else {
// If the value hasn't changed, you can choose to clear the background or do nothing
// range.setBackground(null); // Uncomment this line to clear the background
}
}
} catch (error) {
Logger.log("Error in onEdit: " + error);
}
}
Step 3: Set Up an Installable Trigger
To ensure the script has access to the oldValue property, you need to create an installable trigger:
- In the script editor, click on the clock icon (Triggers) on the left sidebar.
- Click on the "+ Add Trigger" button in the bottom right corner.
- In the dialog that appears:
- Choose which function to run:
onEdit
- Choose which deployment should run:
Head
- Select event source:
From spreadsheet
- Select event type:
On edit
- Leave the rest as default.
- Click Save.
- You might be prompted to authorize the script. Follow the prompts to allow the necessary permissions.
Explanation of the Script:
- The
onEdit(e) function runs every time an edit is made in the spreadsheet.
- It checks if the edited cell is within column C and rows 4 to 17.
- It retrieves the new value (
newNum) and the old value (oldNum).
- It handles decimal commas by replacing them with dots before parsing.
- If the old value is undefined (e.g., the cell was previously empty), it does nothing.
- It compares the new and old numerical values:
- If the new value is greater than the old value, it sets the background to light green.
- If the new value is less than the old value, it sets the background to light red.
- If the value hasn't changed, it leaves the background as is (you can modify this to clear the background if desired).
- Errors are caught and logged to help with debugging if needed.
Note:
- This script works across all sheets in your Google Sheets file.
- Ensure that your number formatting is consistent (e.g., using commas or dots for decimals) to avoid parsing issues.
Testing the Script:
- Go back to your Google Sheets file.
- In any of the sheets, go to cells C4 to C17.
- Enter a number (e.g.,
0). Since there's no previous value, the background should remain unchanged.
- Change the number to a higher value (e.g.,
10). The background should turn light green.
- Decrease the number (e.g.,
9). The background should turn light red.
- Try entering numbers with decimals using commas or dots to ensure it works correctly.
This script should fulfill your requirements and function across all three sheets in your Google Sheets file.
Let me know if you need any further assistance or modifications to the script!