Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created October 5, 2015 08:56
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/1381eff885f7688e6336 to your computer and use it in GitHub Desktop.
Save rheajt/1381eff885f7688e6336 to your computer and use it in GitHub Desktop.
randomly generate seating groups

#Randomly create groups ##A tool to randomly generate seating groups

This tool will select the students from a spreadsheet, break them into their classes, and randomly assign them to groups. I created this tool to quickly generate random seating groups for my students.

function createGroups() {
//create a new document
var date = new Date();
var document = DocumentApp.create("Seating Groups " + date.toDateString()).getBody();
//declare the object classList filled with an array for each class
var classList = {};
var newGroups = {};
//set the number of groups to create (later change this to a function parameter)
var numGroups = 6;
//range of cells to get the students names from
var nameRange = SpreadsheetApp.getActiveSheet().getRange(2, 2, 86, 4).getValues();
var classNames = [];
nameRange.map(function(each) {
if(classNames.indexOf(each[2]) === -1) {
classNames.push(each[2]);
}
});
//add classes to object and shuffle the classes
for(var i = 0; i < classNames.length; i++) {
var currentClass = nameRange.filter(function(each) { return each[2] === classNames[i]; });
classList[classNames[i]] = shuffle(currentClass);
}
//break classes into even groups
for(var key in classList) {
var groupMax = Math.ceil(classList[key].length / numGroups);
var groupExtra = classList[key].length % numGroups;
newGroups[key] = {};
for(i = 1; i <= numGroups; i++) {
newGroups[key]["Group "+i] = classList[key].splice(0, groupMax);
if(--groupExtra === 0) {
groupMax--;
}
}
document.appendParagraph(key).setHeading(DocumentApp.ParagraphHeading.HEADING1);
for(var groupKey in newGroups[key]) {
document.appendParagraph(groupKey).setHeading(DocumentApp.ParagraphHeading.NORMAL);
for(var i = 0; i < newGroups[key][groupKey].length; i++) {
document.appendParagraph(newGroups[key][groupKey][i][0] + " " + newGroups[key][groupKey][i][1]);
}
document.appendHorizontalRule();
}
document.appendPageBreak();
}
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment