Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created January 18, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheajt/24798d0e383453921a13 to your computer and use it in GitHub Desktop.
Save rheajt/24798d0e383453921a13 to your computer and use it in GitHub Desktop.
custom function to create a numerical average from text fields in google sheets
function AVGCOMPLETES(arr) {
//declare the array that will be used to get the average
//push 100 to the array if the field is 'complete' and 0 to the array if the field is 'incomplete'
var averageArray = [];
for(i = 0; i < arr[0].length; i++) {
if(typeof arr[0][i] === 'string') {
if(arr[0][i].toLowerCase() === 'complete') {
averageArray.push(100);
} else if(arr[0][i].toLowerCase() === 'incomplete') {
averageArray.push(50);
} else if(arr[0][i].toLowerCase() === 'missing') {
averageArray.push(0);
}
} else {
averageArray.push(arr[0][i]);
}
}
//average all the values in the array
var length = averageArray.length;
var average = averageArray.reduce(function(first, second) {return first + second;}) / length;
//return the rounded average
return Math.round(average);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment