Created
January 18, 2016 13:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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