Skip to content

Instantly share code, notes, and snippets.

@Kirens
Created September 4, 2017 22:39
Show Gist options
  • Select an option

  • Save Kirens/124b5be27c22606f727351fd8198166f to your computer and use it in GitHub Desktop.

Select an option

Save Kirens/124b5be27c22606f727351fd8198166f to your computer and use it in GitHub Desktop.
Google Apps script for spreadsheet that groups values in rows
/**
* Groups values in rows
*/
function group(vals, test, between){
vals = purify(vals);
test = purify(test);
if(vals.length!=test.length) throw "Vals ("+vals.length+") and Test ("+test.length+") must be of same size";
b = [-Infinity].concat( purify(between) ).concat([Infinity]);
var ret = [];
for(var g=1; g<b.length; g++){
ret[g-1] = [];
for(var i=0; i<test.length; i++){
if((test[i]>=b[g-1]) && (test[i]<b[g]))ret[g-1].push(vals[i]);
}
}
return ret;
}
function purify(arr){
var p = [];
for(var i=arr.length, fst=false; i-->0;){
if(+arr[i][0]) fst = true;
if(fst) p[i] = arr[i][0];
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment