Created
April 19, 2011 19:13
-
-
Save jbgo/929321 to your computer and use it in GitHub Desktop.
An example that uses the Osheet::Cell rowspan attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "http://rubygems.org" | |
gem 'osheet', | |
:git => 'git://github.com/jbgo/osheet.git', | |
:branch => 'cell-index' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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