Reputation: 367
I have a simple function to count colors in google sheets. It is working well when called within the script editor but its not working in sheets.... I imported function to sheets and it still loading and loading...
Is there something I am doing wrong? it is my first attempt to custom function. the way I am calling it from cells:
=countbackground("#ff0000", "F11:F27")
my function:
function countBackground(color, inputRange) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(inputRange);
var rowColors = range.getBackgrounds();
var count = 0;
var newArr = [];
// flatten array
for(var i = 0; i < rowColors.length; i++)
{
newArr = newArr.concat(rowColors[i]);
}
//count color
for(var r = 0; r < newArr.length; r++) {
if(newArr[r] === color){
count++;
}
}
Logger.log(rowColors);
Logger.log(newArr);
return count;
}
Upvotes: 0
Views: 96
Reputation: 27302
The function worked fine for me. Note that the code can also be shortened to
function countBackground (color, inputRange) {
return SpreadsheetApp.getActiveSpreadsheet()
.getRange(inputRange)
.getBackgrounds()
.flat()
.filter(bg => bg == color)
.length;
}
Upvotes: 2