Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created June 13, 2017 16:32
Show Gist options
  • Save rheajt/9e9f7f1faa7faa1f5dba6510c69cfa29 to your computer and use it in GitHub Desktop.
Save rheajt/9e9f7f1faa7faa1f5dba6510c69cfa29 to your computer and use it in GitHub Desktop.
google apps script code to randomize a list in a document
function onOpen() {
DocumentApp.getUi()
.createMenu('Multiple-Choice-Randomizer')
.addItem('Run', 'randomizer')
.addToUi();
}
function randomizer() {
var doc = DocumentApp.getActiveDocument();
var selection = doc.getSelection();
var elements = selection.getRangeElements()
.map(function(element) {
return element.getElement().asText().getText();
});
var shuffled = shuffle(elements.slice());
var selectedText = doc.getSelection().getRangeElements();
for(var i = 0; i < elements.length; i++ ) {
selectedText[i].getElement().asText().setText(shuffled[i]);
}
}
function shuffle(a) {
var j, x, i;
for(var i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment