Showing posts with label google-apps-script. Show all posts
Showing posts with label google-apps-script. Show all posts

Saturday, November 14, 2026

For beginners: Christmas gift exchange using Google Apps Script

My family does an annual Christmas gift exchange, but we had to keep redealing the names because someone invariably drew his own name. With long-distance family members, this became inconvenient. We could also have had someone check all the names before we passed them out, but that ruins someone's fun. So I made a program that organizes a Christmas gift exchange without problems. I'm posting it here so that others can use it as well.

You can probably do this if you can copy and paste on your computer. Otherwise, no. 

Since we'll be using Google Apps Script, you'll need a Google account, obviously. Others involved need not be using Gmail. Google just needs someone to blame if you turn out to be a spammer. 

Following these directions requires granting various permissions, possibly enabling some sort of developer mode on your Google account, and otherwise repeatedly telling Google that, yes, I trust this program with access to my email. I'll be skipping that bit; it's too boring to explain or remember.

Relatedly, you will be giving code I wrote access to your email. You should glance over the code and have a general idea of what it does. It's only forty lines. If you run the code without looking at it, then you must trust me a LOT.

The bits where you have to edit anything are underlined. While you edit, pay attention to the text coloring. If you see colors change or a little red squiggly somewhere as though for spell checking, you've accidentally changed something and you should undo (Ctrl+Z) and try again.

The steps:

  1. Go to https://script.google.com and click "New project". You'll start in Code.gs, where you want to be.
  2. Delete all the starting code (Ctrl+A, backspace).
  3. Paste in the code below.
  4. In the array "participants", copy one of the lines to your clipboard, then paste that line to duplicate it until you have a line for each participant. Don't delete the commas at the ends of the lines!
  5. On each line, change the name and email address to those of one of your participants. Don't touch anything that's not inside the single-quotes, and don't accidentally remove any of the single quotes. (For clarity: The name and the email address on each line should be for the same person. You are not doing the shuffling now! The computer will do that later.)
  6. Mess with the email subject in the "sendEmail" function as you wish.
  7. Hit the Save button in the top row, next to the Undo/Redo buttons.
  8. When you are ready to run this thing, hit the Run button next to the Save button. This will send the emails, so don't hit it again unless you're sure it didn't go the first time. (Don't touch the Deploy button. I don't know what that does.)

The code:

 


let participants = [
    { name: 'Donald', email: 'donald@example.com' },
    { name: 'Chip', email: 'chip@example.com' },
    { name: 'Dale', email: 'dale@example.com' },
]

// returns an integer of at least min and less than max
function randInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

function shuffle(a) {
    // Sattolo's algorithm
    // Shuffles an array such that no item is in its original position
    // Method: For each item in the array, swap it with a random item that comes after it.
    // Source: https://danluu.com/sattolo/

    let count = a.length
    for (let i = 0; i < count - 1; i++) {
        let j = randInt(i+1, count);
        let tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}

function sendEmail(sender, recipient) {
    let subject = 'Duck family Christmas gift exchange';
    let message = 'You will buy a gift for: ' + recipient.name;
    GmailApp.sendEmail(sender.email, subject, message);
}

function main() {
    let recipients = participants.slice();  // clone the array
    shuffle(recipients);

    participants.forEach(function(sender, i) {
        sendEmail(sender, recipients[i]);
    });
}

For beginners: Christmas gift exchange using Google Apps Script

My family does an annual Christmas gift exchange, but we had to keep redealing the names because someone invariably drew his own name. With ...