ruby spreadsheet row background color

亡梦爱人 提交于 2019-12-19 19:07:28

问题


I am trying to parse an excel spreadsheet using "spreadsheet". How could I get the background color of each row?


回答1:


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 style exactly where you want




回答2:


I was looking around for colors that you can use for the background color of a cell. For example:

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

I couldn't find good info on which colors I could use for :pattern_fg_color. I decided to look for Excel help and found: http://dmcritchie.mvps.org/excel/colors.htm (at "The DOS assignments of the 16 colors").

It looks like the top 16 colors work:

0 Black, 1 Navy, 2 Green, 3 Teal, 4 Maroon, 5 Purple 6 Olive, 7 Silver, 8 Gray, 9 Blue, 10 Lime, 11 Aqua, 12 Red, 13 Fuschia, 14 Yellow, 15 White




回答3:


I've just been trying to figure out the same, and seems like in the current version (0.6.5.9) of Spreadsheet gem the attribute of cell's background color is not supported in reader (you can only define background color in cells for writing).

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 experimentation however I figured out that not all of them are properly 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 :)



来源:https://stackoverflow.com/questions/7730112/ruby-spreadsheet-row-background-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!