Ex Nihilo

A Processing Sketch Blog

Archive for the ‘Google Docs’ Category

Reading a Google Docs Spreadsheet

Wednesday, August 12th, 2009

There doesn’t seem to be much on the web specifically about tying together Processing and Google Docs, but it’s fairly simple when you realize the latter has a ton of export options that work pretty easily with Processing. I’m starting with CSV, though the option to work with XML will likely come in handy down the line.

Starting with a spreadsheet filled with a few dozen values, I can get at the CSV with a simple export:

Picture 2

The loadStrings function pulls in a local file or a remote URL as a String array:


String[] inputFileLines;
inputFileLines = loadStrings(
  "http://spreadsheets.google.com/pub? ->
    key=tSLqWX2BTMg1-fUyRV5shBQ&output=csv"
);

It’s fairly trivial to walk through the array and do something useful with the numbers:


inputFileIndex = 0;

while (inputFileIndex < inputFileLines.length) {
  String pieces = inputFileLines[inputFileIndex];
  if (pieces.length() > 0) {

    // do stuff with the 'pieces' string here,

  };
  inputFileIndex++;
};

The data exists as strings though, so it’s probably more useful to convert each value to float() or int() before using it. With the simple CSV file I linked up earlier as a source, I used the basic loop to assign the data points as the radii values for each of a set of spheres plotted randomly along the Y axis, or the height of a set of bars slightly tilted along the X axis:

spheres-53867
spheres-53667
blocks-52180
blocks-53883

If we were to say the linear-increasing X value is time, that makes for a couple of fun 3D graphs that even Tufte can be proud of. (Or not.)

Tags: , , ,
Posted in Google Docs | No Comments »