I have a function which counts number of cells in a column and returns the number in a cell.
function countColoredCells(countRange,colorRef) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
var BackGround = activeSht.getRange(colorRefAddress).getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == BackGround )
countCells = countCells + 1;
return countCells;
};
function getFirstCellInRow() {
var sheet = SpreadsheetApp.getActiveSheet();
var activeCell = sheet.getActiveCell();
var firstCell = activeCell.offset(0, 1-activeCell.getColumn());
return firstCell;
}
function getFirstAppt() {
var sheet = SpreadsheetApp.getActiveSheet();
var activeCell = sheet.getActiveCell();
var firstAppt = activeCell.offset(activeCell.getRow()+4, activeCell.getColumn()+4);
return firstAppt
}
I then put =countColoredCells(getFirstAppt(),getFirstCellInRow()) in a cell to return a number, which gives me a range error. For the record, =countColoredCells(G26:G,A19) works fine.
Additionally, the function does not fire when I add new colored cells in the column due to the nature of the function. How do I fire countColoredCells on this cell with onChange function? For the record, the location of where this cell is will change everyday, which is why I need to fire the function itself on the cell. I just don't know how to fire a function onChange.
function onChange(e) {
if (e.range.getColumn() == 7 && e.changeType == 'FORMAT' )
{
return
}
}
I'm not sure how to get it to fire functions on the cells that have that function.