Exports an Array of Ruby objects to a Google Docs spreadsheet
Ruby
Latest commit 2f41f85 Oct 20, 2011 @Sirupsen Sirupsen Bump to 0.1.1
Permalink
Failed to load latest commit information.
lib Bump to 0.1.1 Oct 20, 2011
test Cleanup config Jul 3, 2011
.gitignore Initial version Jun 26, 2011
.rspec Initial version Jun 26, 2011
Gemfile Initial version Jun 26, 2011
README.md Added more examples Jul 3, 2011
Rakefile Initial version Jun 26, 2011
to_google_spreadsheet.gemspec Preparing for Gem release Jun 27, 2011

README.md

to_google_spreadsheet

to_google_spreadsheet makes it trivial to populate a Google spreadsheet with an Array of Ruby objects.

# Adds the users Bob and John to the default spreadsheet in the worksheet Employees
[{:name => "Bob", :age => 26}, {:name => "John", :age => 28}].to_google_spreadsheet("Employees")

Results in:

Image of Google spreadsheets result once running above command

You could also use this within Rails like so:

Account.all.to_google_spreadsheet("Accounts")

Usage

Installation

$ gem install to_google_spreadsheet

Setup

You must provide your Google Docs credentials, optionally you can supply a default spreadsheet to use:

GoogleSpreadsheet.config do |c|
  c.email               = "user@company.com"
  c.password            = "seekrit"

  # optional, see note below on how to obtain a spreadsheet key
  c.default_spreadsheet = "spreadsheet_key"
end

Obtaining your spreadsheet key

The spreadsheet key is found in the URL when visiting the spreadsheet:

Image showing how the key is found in the URL in Google Docs

Usage

#to_google_spreadsheet is simply a method defined on Enumerable, thus any subclasses hereof should work with to_google_spreadsheet as expected (e.g. Arrays). Enumerable#to_google_spreadsheet takes two arguments:

module Enumerable
  def to_google_spreadsheet(worksheet, spreadsheet = GoogleSpreadsheet::Config.default_spreadsheet)
    session = GoogleSpreadsheet.login(GoogleSpreadsheet::Config.email, 
                                      GoogleSpreadsheet::Config.password)
    spreadsheet = session.spreadsheet_by_key(spreadsheet)
    ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
    ws.set_header_columns(self.first)
    ws.populate(self)
    ws.save
  end
end

The first argument being the name of the worksheet within the spreadsheet, e.g. "Accounts", if it doesn't already exist, it will be created. The second, optional, argument is the spreadsheet key (see Obtaining your spreadsheet key above), if it is not supplied, the default spreadsheet, defined by in the configuration will be used.

Examples

# Configure
GoogleSpreadsheet.config do |c|
  c.email               = "user@company.com"
  c.password            = "seekrit"

  # optional, see note below on how to obtain a spreadsheet key
  c.default_spreadsheet = "spreadsheet_key"
end

# Populates the worksheet "Employees" in the default spreadsheet 
# with two employees
[
  {:name => "Bob", :age => 26}, 
  {:name => "John", :age => 28}
].to_google_spreadsheet("Employees")

# Populates the worksheet "Signups" in the nondefault spreadsheet 
# with two signup dates
[
  {:date => "18/7/2011", :signups => 28},
  {:date => "19/7/2011", :signups => 33}
].to_google_spreadsheet("Signups", "other_spreadsheet_key")

# Populates the worksheet "Accounts" in the default spreadsheet 
# with all # accounts in the database
Account.all.to_google_spreadsheet("Accounts")

Supported objects

assert array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }}

I.e. anything that is #kind_of? Hash, an OpenStruct or an ActiveRecord model. It is easy to add support for additional types.

License

to_google_spreadsheet is released under the MIT license.