The second parameter is the number you want to return. Ex. If you want the top 4, then the number should be 4.
=GETTOP(A1:Z1, 4)
returns the top 4 values from the range A1:Z1
| /** | |
| * Gives you the top 4 values from a range | |
| * @param {array} range of numbers to get the top 4 from | |
| * @param {number} number of values to return | |
| * @return top 4 | |
| * @customfunction | |
| */ | |
| function GETTOP(gradeRange, topNumber) { | |
| var allGrades = gradeRange.reduce(function(a,b) { | |
| // flatten the multidimensional array into a single array | |
| return a.concat(b); | |
| }).filter(function(a) { | |
| // remove any blank values from the array | |
| return a !== ''; | |
| }); | |
| var top = allGrades.sort(function(a, b) { | |
| // sort the array with the top values first | |
| return b - a; | |
| }).slice(0, topNumber); // slice the array to only return the 4 highest values | |
| return [top]; | |
| } |