Skip to content

Instantly share code, notes, and snippets.

@jbgo
Created April 19, 2011 19:13
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 jbgo/929321 to your computer and use it in GitHub Desktop.
Save jbgo/929321 to your computer and use it in GitHub Desktop.
An example that uses the Osheet::Cell rowspan attribute
source "http://rubygems.org"
gem 'osheet',
:git => 'git://github.com/jbgo/osheet.git',
:branch => 'cell-index'
require 'rubygems'
require 'bundler/setup'
Bundler.setup
Bundler.require
# Generates a spreadsheet with rowspans like this:
#
# |---|---|---|
# | a | b | e |
# |---|---|---|
# | | c | |
# | |---| |
# | | d | |
# |---|---|---|
# | f | | h |
# |---| |---|
# | g | | |
# |---|---|---|
# | | i | k |
# | |---|---|
# | | j | l |
# |---|---|---|
data_rows = [
[ [:a], [:b, :c, :d], [:e] ],
[ [:f, :g], [], [:h] ],
[ [], [:i, :j], [:k, :l] ]
]
puts "building workbook..."
def log(col, row, message)
puts "(row: #{row}, col: #{col}) -- #{message}"
end
ws = Osheet::Workbook.new do
worksheet do
name "nil"
data_rows.each do |aggregate_row|
max_rows = aggregate_row.max{ |a, b| a.length <=> b.length }.length
max_rows.times do |row_num|
row do
aggregate_row.each_with_index do |column_data, i|
if column_data.nil?
log(row_num, i, nil)
next
else
value = column_data.shift
if value
log(row_num, i, value)
cell {
data value
index i + 1
}
else
log(row_num, i, "empty, rowspan = #{max_rows - row_num}")
cell {
rowspan max_rows - row_num
index i + 1
}
aggregate_row[i] = nil
end
end
end
end
end
end
end
end
puts "done."
fname = 'rowspan-example.xls'
ws.to_file(fname)
puts "spreadsheet written to '#{fname}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment