Tuesday, March 10, 2015
Instant voting with Apps Script
From nations choosing presidents to offices selecting which coffee to brew, we often find ourselves involved in election systems designed to choose the best option. This spring my alma maters solar vehicle team, CalSol, needed to elect new leaders. Our previous system was painfully slow, involved "raising hands" in a room, and excluded any team members who could not attend a specific meeting. I set out to solve these problems and the result was an easy method for running fair elections in a matter of minutes.
I was able to build the system completely on Google products and technologies:
- Google Forms: Allows members to submit their votes from anywhere.
- Google Spreadsheets: Makes it easy to audit the votes and configure the system.
- Google Apps Script: Simple way to access the results and determine the winner.
I used a lesser known voting system called instant-runoff voting (IRV), or the alternative vote, which asks voters to rank candidates rather than cast a single vote. These votes, along with a secret voting key which I provided to each member, are recorded with a Google Form that automatically populates a spreadsheet. The code in Apps Script looks through the spreadsheet to count the votes while ensuring that each voting key is only used once. The secret keys not only prevent voters from casting multiple votes, but they also allow voters to change their vote by submitting the form again.
Below is a simplified snippet of code that shows the general process used to calculate the winner.
/* Some code omitted for clarity */
/* candidates is a list of names (strings) */
var candidates = get_all_candidates(results_range);
/* votes is an object mapping candidate names -> number of votes */
var votes = get_votes(results_range, candidates, keys_range, valid_keys);
/* winner is candidate name (string) or null */
var winner = get_winner(votes, candidates);
while (winner == null) {
/* Modify candidates to only include remaining candidates */
get_remaining_candidates(votes, candidates);
if (candidates.length == 0) {
Browser.msgBox("Tie");
return;
}
votes = get_votes(results_range, candidates, keys_range, valid_keys);
winner = get_winner(votes, candidates);
}
Browser.msgBox("Winner: " + winner);
I learned that putting a little effort into Apps Script can make people happy and save a lot of time. The team feedback was outstanding. One CalSol member said the process was an "Excellent, clean, and professional voting process. This should become a standard [for the team]." I was elated when I was able to close the polls during a meeting and announce the winners of twelve independent elections in just a matter of minutes.
If you like, you can watch a video demonstrating how to create and run your own election using this script:
Try the script yourself to make sure your coffee preferences are heard!
Chris Cartland profile | GitHub Chris is a Developer Programs Engineer based in Mountain View on the Google+ team. He previously worked on solar vehicles at UC Berkeley and wants developers to write software that makes our lives better. In his spare time he likes to play soccer and throw the ball in after doing a front handspring. |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.