Hello.
I’m trying to program a custom function for a spreadsheet. I want to create a function that takes a cell range and a reference cell as input.
This function should calculate and return the number of cells in the range with the same background color as the reference cell.
Here’s the code I wrote:
(function()
{
/**
* Function that returns the number of color cell in selection
* @customfunction
*
* @param {range} rangeCell The Cell range (required).
* @param {range} cellref The color cell ref (required).
* @returns {number} nbcolor The number of color cell on selection.
*/
function colorcounter(rangecell, cellref) {
// save ref color
const colorref = cellref.GetFillColor();
// number of color detected
let nbcolor = 0;
let nbrow = 0;
let nbcol = 0;
// transform rangecell to Range type
//let range = Api.GetRange(rangecell);
// get Row and Col size
nbrow = rangecell.GetRow();
nbcol = rangecell.GetCol();
// explor tab
for (let r = 0; r < nbrow; r++) {
for (let c = 0; c < nbcol; c++) {
// save cell color
let colorcell = rangecell(r, c).GetFillColor();
if (colorcell === colorref) {
nbcolor++;
}
}
}
return nbcolor;
}
Api.AddCustomFunction(colorcounter);
})();
Unfortunately, it doesn’t work; it’s not recognized in the list of functions.
I’m not very good with JavaScript. Thank you in advance for your help.
