That is an incredibly frustrating situation, and I want to assure you we can solve it. The good news is that your conclusion is based on a symptom, but not the root cause.
Let's be very clear: rangeToColor.setBackgrounds(colors) is a permanent command. It is the correct syntax and it behaves exactly like manually selecting a cell and using the "Fill color" bucket.
The fact that the color disappears after a manual edit is conclusive proof that something else is actively overriding your script's changes. The command is working perfectly, but it's losing a fight for priority against another rule or script.
Here are the most likely culprits, in order from most common to least common.
This is almost certainly the cause. Conditional Formatting rules have higher priority than manually set backgrounds. When you edit a cell, the sheet re-evaluates all conditional formatting rules. If a rule applies to that cell, it will overwrite the background color your script set.
In your Google Sheet, click on one of the cells that is losing its color.
Go to the menu: Format > Conditional formatting.
A sidebar will appear on the right. It will show you any and all conditional formatting rules that apply to the selected cell.
If you see a rule there, you have found the problem.
Solution: You must either delete this conditional formatting rule or adjust its logic so it doesn't conflict with what your script is trying to do.
Another possibility is the "Alternating colors" feature, which is just a user-friendly type of conditional formatting. Check Format > Alternating colors and remove any rules applied to your range.
If you (or a colleague) have another script running in the same sheet, it might be using an onEdit(e) trigger. This type of function runs automatically every single time a user edits any cell. If that onEdit script has any logic that formats cells (e.g., re-applying row striping, clearing formatting, etc.), it will run immediately after your manual edit and override the color your first script set.
Go to Extensions > Apps Script.
Look through all your .gs files for a function named onEdit(e).
Examine the code inside. Does it contain any .setBackground(), .setBackgrounds(), or .clearFormat() commands?
Solution: You need to either disable this onEdit script or, more likely, merge the logic from your coloring script into the onEdit script so they work together instead of fighting each other.
To prove that the command works as expected, let's run it in a completely clean environment.
Create a brand new, blank Google Sheet.
Go to Extensions > Apps Script.
Paste this simple code:
/**
* A simple function to demonstrate that setBackgrounds() is permanent.
*/
function setPermanentColor() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const rangeToColor = sheet.getRange("A1:B5");
// Create a 2D array of colors that matches the dimensions of the range (5 rows, 2 columns)
const colors = [
['#ff0000', '#00ff00'], // Row 1: red, green
['#0000ff', '#ffff00'], // Row 2: blue, yellow
['#ff00ff', '#00ffff'], // Row 3: magenta, cyan
['#cccccc', '#888888'], // Row 4: light gray, dark gray
['#ff9900', '#9900ff'] // Row 5: orange, purple
];
// This command makes a PERMANENT change to the sheet's formatting.
rangeToColor.setBackgrounds(colors);
}
Save the script, select the setPermanentColor function, and click Run.
Go back to your sheet. The range A1:B5 will be colored.
Now, manually type anything into any of those cells. You will see the background color does not disappear.
This demonstrates that the command is permanent. The problem lies in your existing sheet's environment, where another rule or script is overriding it.
Check Conditional Formatting first. This is the cause 9 times out of 10. Go to Format > Conditional formatting and clear any rules affecting your target range.
If that doesn't work, check for any onEdit(e) triggers in your script files that might be re-formatting the sheet.
Rest assured that setBackgrounds() is the correct tool for the job. We just need to find and remove the conflicting rule that is undoing its work.
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/89398102-d2a9-4b62-9acd-dbfb15eb39a9n%40googlegroups.com.