Skip to content

Instantly share code, notes, and snippets.

@omad
Created February 24, 2017 03:54
Show Gist options
  • Save omad/22fa6dce78344bbb4883cb686b29f914 to your computer and use it in GitHub Desktop.
Save omad/22fa6dce78344bbb4883cb686b29f914 to your computer and use it in GitHub Desktop.
Shuffle Tabular Rows
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<h1>Shuffle Table</h1>
<ol>
<li>Copy your selection from Excel into the text area below</li>
<li>Press the shuffle button. (go on, press it a few times if you want)</li>
<li>Copy everything in the text area and paste back into Excel</li>
</ol>
<textarea name="" id="table" cols="80" rows="20"></textarea>
<button type="button" id="shuffle">Shuffle</button>
<p>
by Damien Ayers
</p>
</body>
<script>
$("#shuffle").on("click", function() {
var table = $("#table");
var newText = "";
var lines = table.val().split("\n");
for (var i = 0; i < lines.length; i++) {
var cells = lines[i].split("\t");
newText += shuffle(cells).join("\t");
newText += "\n";
}
table.val(newText);
});
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment