Topic: spreadsheet gem - how to set a backgrond color for a specified row

Does anybody know how to set a background color for a specified row by using spredasheet gem 0.6.4?
I'm on Ubuntu 9.10, Ruby 1.8.7, OpenOffice Calc.
Thank you.

Re: spreadsheet gem - how to set a backgrond color for a specified row

I found a solution on a japaneese site:

require 'rubygems'
require 'spreadsheet'

Spreadsheet.client_encoding = 'UTF-8'

book = Spreadsheet::Workbook.new

class ColorFormat < Spreadsheet::Format
  def initialize(color)
    super :pattern => 1, :pattern_fg_color => color
  end
end

sheet = book.create_worksheet :name => 'My fruits'

fruits = {
  'apple' => :red,
  'lemon' => :yellow,
  'orange' => :orange
}.each_with_index do |(fruit, color), i|
  sheet[0, i] = fruit
  sheet.row(0).set_format(i, ColorFormat.new(color))
end

sheet.row(0).height = 14 

book.write '/home/serge/Documents/fruits.xls'

where col - is a column number

Last edited by Javix (2010-03-08 17:19:41)