Skip to content

Instantly share code, notes, and snippets.

@misterhay
Last active December 30, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterhay/2055dbbbf7a9ae521d16c53a27096f63 to your computer and use it in GitHub Desktop.
Save misterhay/2055dbbbf7a9ae521d16c53a27096f63 to your computer and use it in GitHub Desktop.
Generates a subject, verb, and location for a drawing game (in Google Sheets with Google Apps Script)
// in a Google Sheet that has two sheets: Cards and Drawn
// The 'Cards' sheet should have three columns: subject, verb, and location
// This code is public domain, but let me know if there are any copyright concerns.
// https://developers.google.com/apps-script/guides/sheets
// https://docs.google.com/spreadsheets/d/1mrnsKUCSkum-ZmAvGtK10uAf1eypdxsoFP7tbjWynsQ
function addMenu() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Game').addItem('Draw Something', 'drawSomething').addToUi();
}
function drawSomething() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Cards');
var lastRow = sheet.getLastRow();
var random1 = pickRandom(lastRow);
var random2 = pickRandom(lastRow);
var random3 = pickRandom(lastRow);
var who = sheet.getRange(random1, 1).getValue();
var what = sheet.getRange(random2, 2).getValue();
var where = sheet.getRange(random3, 3).getValue();
var phrase = who + ' ' + what + ' ' + where + '.';
logPhrase(phrase);
SpreadsheetApp.getUi().alert(phrase);
}
function pickRandom(max) {
// a number between 2 and max
var min = 2;
for (i = 1; i<20; i++) {
var random = Math.floor(Math.random() * (max - min + 1)) + min;
}
return random;
}
function logPhrase(phrase) {
var time = new Date();
var logSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Drawn');
var lastRowOfLog = logSheet.getLastRow() + 1;
logSheet.getRange(lastRowOfLog, 1).setValue(time);
logSheet.getRange(lastRowOfLog, 2).setValue(phrase);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment