Holiday Gift Exchange

November 28, 2017G Suite Add-onholidays, gift exchange, secret santa, google forms, google apps script

It is the holiday season and I want to share a fun way to engage your students with the spirit of sharing!

Many customs and cultures exchange gifts at the holiday times. This idea was shared with me from my Dutch friends who celebrate Sinterklaas. They exchange gifts and poems.

That gave me the idea that I could use Google forms to ask students to give their names and a few details about themselves. Then when the teacher decides, end the submissions and pair all of the students secretly!

It even sends emails to their gmail accounts. Check out the video above for a demonstration on how to setup the project with your class. Below, I am going to talk a little bit about the code

The makeList() function is the main entry point.

function makeList() {
  //1. Close the responses to the form
  var formUrl = SpreadsheetApp.getActiveSheet().getFormUrl();
  FormApp.openByUrl(formUrl)
    .setAcceptingResponses(false)
    .setCustomClosedFormMessage(
      'Sorry but the pairs have been made and entry is closed!'
    );

  //2. Pair the people together
  var pairs = getPairs();

  //3. Create an answer sheet
  writeSheet(pairs);

  //4. Send emails to everyone!
  sendEmails(pairs);

  //Done, signal the sheet
  Browser.msgBox('Done creating the pairs and emailing the members');
}

Then, I am using the array method .slice() to force a copy of the array of students and an array shuffle function that I found on StackOverflow to shuffle the copy.

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;
}

I check to make sure that the two arrays are not equal, and if they aren’t, I match up the students. This way, every student has a unique pair.

function isEqual(vals, copy) {
  for (var i = 0; i < vals.length; i++) {
    if (vals[i][1] === copy[i][1]) {
      return true;
    }
  }
  return false;
}

Find the complete code and help me to clean up my code a little bit on the Github repository

jordan rhea

Jordan Rhea - I’m a front-end web developer. Follow me at @rheajt if you want to talk, discuss, or fix any of my code! I am passionate about using JavaScript in Google projects, but there are many more applications. I want to learn them all.