Instantly share code, notes, and snippets.

Embed
What would you like to do?
Secret santa mixmatch with google sheets and apps script : Use this script following the instructions in the gist to create a Secret santa mix-match and if needed send each user their partner's details Read whole post here @Hashnode https://hashnode.com/post/secret-santa-mix-match-with-google-sheets-and-apps-script-cjptefe1d00rikas2z9joelqa
/** Dummy data
gslondon@yahoo.ca Thomas Bolan  
csilvers@hotmail.com Rafael Ridgley  
north@yahoo.com Shoshana Zarrella  
garyjb@live.com Shavon Cales  
euice@att.net Mirian Fasano  
scitext@att.net Wilfred Patch  
ingolfke@live.com Allan Bankes  
johndo@hotmail.com Edwin Acheson  
gozer@gmail.com Emanuel Oriley  
marnanel@gmail.com Vincenzo Rosenfeld  
errxn@verizon.net Kaylene Sauter  
xnormal@comcast.net Miguel Gerstner  
erynf@aol.com Theresia Heffington  
rogerspl@optonline.net Carl Hendrie  
jbarta@mac.com Mazie Kilmon  
jshirley@optonline.net Marietta Pressman  
boftx@yahoo.com Tesha Chatham  
isorashi@yahoo.com Erasmo Walcott  
rande@msn.com Julia Harrah  
jhardin@optonline.net Deloris Birch  
*/
function readCurrentSheet(){
var currentActiveSheet = SpreadsheetApp.getActiveSheet();
return currentActiveSheet.getDataRange().getValues()
}
function shuffle(array) {
var currentIndex = array.length, temp, randomIndex;
while (0 !== currentIndex){
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temp = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temp;
}
return array;
}
function main(){
var shuffledList = shuffle(readCurrentSheet());
var newSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet('Final List');
var firstHalf = shuffledList.splice(0, shuffledList.length/2);
var newShuffledGivers = [].concat(firstHalf);
var newShuffledGivers = shuffle(newShuffledGivers);
for(var i=0;i<shuffledList.length;i++){
temp = [];
temp = temp.concat(firstHalf[i] || ['','','']).concat(shuffledList[i]).concat(newShuffledGivers[i] || ['','','']);
//firstHalf[i][0] !== '' && MailApp.sendEmail(firstHalf[i][0], 'You secret santa partner', 'Hello there, your secret santa partner is ' + shuffledList[i][1]); // to 1st about 2nd
//newShuffledGivers[i][1] !== '' && MailApp.sendEmail(shuffledList[i][0], 'You secret santa partner', 'Hello there, your secret santa partner is ' + newShuffledGivers[i][1]); // to 2nd about 1st
newSheet.appendRow(temp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment