Osheet

Description

A DSL for specifying and generating spreadsheets using Ruby.

Installation

$ gem install osheet

Basic Example

require 'osheet'

fields = ['Sex', 'Age', 'Height', 'Weight']
data = {
  'Tom' => ['M', 52, "6'2\"", '220 lbs.'],
  'Dick' => ['M', 33, "6'5\"", '243 lbs.'],
  'Sally' => ['F', 29, "5'3\"", '132 lbs.']
}

# this will dump the above data to a single-sheet workbook w/ no styles

Osheet::Workbook.new {
  title "basic"

  template(:column, :data) { |field, index|
    width 80
    meta(
      :label => field.to_s,
      :index => index
    )
  }

  template(:row, :title) {
    cell {
      colspan columns.count
      data worksheet.name
    }
  }

  template(:row, :empty) {
    cell {
      colspan columns.count
      data ''
    }
  }

  template(:row, :header) {
    columns.each do |column|
      cell {
        data column.meta[:label]
      }
    end
  }

  template(:row, :data) { |name, stats|
    cell {
      data name
    }
    stats.each do |stat|
      cell {
        data stat
      }
    end
  }

  worksheet {
    name "Stats: #{fields.join(', ')}"

    column {
      width 200
      meta(
        :label => "Name"
      )
    }
    fields.each_with_index do |f, i|
      column :data, f, i
    end

    row :title
    row :empty
    row :header

    data.each do |name, stats|
      row :data, name, stats
    end
  }
}.to_file('stats.xls')

API

Check out the wiki: github.com/kelredd/osheet/wiki. It covers the full Osheet API.

Examples

I’ve add a few examples to ./examples. Please refer first to the API then to these for examples on basic usage, using templates, formatting data, and styling data.

License

Copyright © 2010-Present, Kelly Redding ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.