Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Last active March 16, 2018 20:18
Show Gist options
  • Save AndresMWeber/3d9a20cbd5006fb931d7a7acab915cde to your computer and use it in GitHub Desktop.
Save AndresMWeber/3d9a20cbd5006fb931d7a7acab915cde to your computer and use it in GitHub Desktop.
/**
* Counts a guest's name that is not highlighted nor has strikethrough
*
* @return The value of all the guests.
* @customfunction
*/
function COUNTGUESTS(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var formula = SpreadsheetApp.getActiveRange().getFormula();
var args = formula.match(/=\w+\((.*)\)/i)[1].split("!");
try {
if (args.length == 1) {
var range = sheet.getRange(args[0]);
}
else {
sheet = ss.getSheetByName(args[0].replace(/'/g, ''));
range = sheet.getRange(args[1]);
}
}
catch(e) {
throw new Error(args.join("!") + " is not a valid range");
}
var weights = range.getFontLines();
var values = range.getValues();
var colors = range.getBackgrounds()
var guests = 0;
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[0].length; j++) {
if (weights[i][j] != "line-through" && colors[i][j] == "#ffffff" && values[i][j] != "") {
guests += 1;
}
}
}
return guests;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment