I wrote this seemingly simple script to copy the background colors from one column to another.
When I run the script, I get no error messages, but nothing seems to happen.
This is what I type into a cell on the sheet:
=copyColor("B:B", "A:A")
I read a post on the Google Apps Script forum that implied that this type of procedure isn't possible, but I am determined to write a script that pulls it off.
Here is what the post said:
"As it is clearly explained in the documentation, Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. That is of course also true for other methods such as setBackground etc."
This is why I tried to get around the problem, by NOT USING the setBackgrounds()
function.
Is there another way? Or is there a way I can fix mine to make it work?
function copyColor(rangeToCopy, rangeToPaste)
{
//an array to store the first background colors
var firstColors = [];
//an array to store the second background colors
var secondColors = [];
//this will assign the first range into a variable
var firstRange = SpreadsheetApp.getActiveSheet().getRange(rangeToCopy);
//this will store the colors of the range into the firstColors array
firstColors = firstRange.getBackgrounds();
//this will assign the second range to a variable
var secondRange = SpreadsheetApp.getActiveSheet().getRange(rangeToPaste);
//this will store the colors of the range into the secondColors array
secondColors = secondRange.getBackgrounds();
//compare the two color arrays. if they do not match, apply the first array to the second array
if (firstColors != secondColors)
{
secondColors = firstColors
}
}