Monday, October 25, 2010

Collaborative Fun in Spreadsheets

Editor’s Note: Zach is a co-founder at Olark- a dead simple way to talk with customers on your website. But when he isn’t at work, he enjoys making and playing games, like this one he created at the Google Apps Script hackathon in Mountain View.
Spreadsheets.
That word conjures up nightmarish questions for me. What’s our cash flow this month? Are there enough Foo widgets being made BarBaz factory? Endless columns, questionable assumptions, mistakes in your calculations... when will it all end?
But it doesn’t have to be that way! Besides being a powerful business tool, the grid layout in a spreadsheet makes a great game board, and what better way to explore the versatility of Google Apps Script than by writing a game with a few simple rules:
  • Players alternate turns
  • Each turn you choose an unpainted square as the middle of your pattern, but the rest of your pattern can paint over your opponent.
  • When all the squares in the 8x8 grid are painted, whoever has the most painted their color wins!
When working with a spreadsheet, whether it is to make a game or a business report, there are a few things to keep in mind:
First, you don’t know how the user had the spreadsheet set up before installing the script, so make sure you go through and set column width, colors, text, and any other properties you specifically want.
Second, there are several ways your code can get called from the spreadsheet. I chose to have the user solely interact via a menu. You configure the menu by writing an onOpen() function; it will get called whenever the spreadsheet is opened. You can also set up triggers that call your code on a certain time interval, or when a cell is edited.
Lastly, think about where you’re going to store state for your script. I designed my game so it didn’t require any hidden state, but if you’re implementing Sudoku or Battleship you may need to be clever about where you store that information. Fortunately, there are several services in Apps Script you can call out to, including pretty much any webservice via the UrlFetch Services.


If you haven’t checked out Google Apps Script yet, you should. After looking at the documentation and sketching out the idea on paper, it took about 2 hours to write and debug this 120-line app (and honestly, I’m kind of a javascript n00b). If you’re looking for a starting point or just to have some fun with Spreadsheets, check out the code here: http://code.google.com/p/paintwar/

6 comments:

  1. This particular feature, the "onOpen" function, only works intermittently. It has been broken for a long time. It was reported as a bug (#297) on November 1st, after being discussed since September in the groups. It is now November 28th and the bug hasn't even been acknowledged.

    You'd figure that a bug reported by a bunch of people in which scripts only work when they want to would get a pretty high priority, but not for Google.

    ReplyDelete
  2. This post is inspiring me to come back to your site.

    ReplyDelete