Background color in ruby

I am trying to parse an Excel spreadsheet using a spreadsheet. How can I get the background color of each line?

+5
source share
3 answers
book = Spreadsheet::Workbook.new 
sheet = book.create_worksheet :name => 'Name'
format = Spreadsheet::Format.new :color=> :blue, :pattern_fg_color => :yellow, :pattern => 1
sheet.row(0).set_format(0, format) #for first cell in first row

or

sheet.row(0).default_format = format #for entire first row

you can iterate over each row / cell and apply the style exactly where you want

+16
source

I was looking for colors that can be used for the background color of a cell. For instance:

Spreadsheet::Format.new({ :weight => :bold, :pattern => 1, :pattern_fg_color => :silver })

I could not find good information about what colors I could use for: pattern_fg_color. I decided to find the Excel help system and found: http://dmcritchie.mvps.org/excel/colors.htm (in "DOS Destinations of 16 Colors").

The 16 best colors seem to work:

0 , 1 , 2 , 3 , 4 , 5 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15

+4

I was just trying to understand the same thing, and it seems that in the current version (0.6.5.9) of the Gem file, the background color attribute of the cell is not supported in the reader (you can only determine the background color in the cells for recording).

Here's how to check all currently available cell attributes:

a = Spreadsheet.open('/folder/spreadsheet.xls')
puts a.worksheets[0].row(<rownumber>).format(<columnnumber>).inspect

After some experiments, I realized that not all of them were correctly extracted. The good news is that the developers promise to implement better support for cell formats in future versions, so we just need to be patient :)

+1
source

All Articles