2011-10-11 38 views

回答

16
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 

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

你可以在每行迭代/單元格和應用風格正是你想要的地方

+2

善良緣故docs是可怕的!謝謝! – yekta

+0

不知何故,它不適合我 - http://stackoverflow.com/questions/24608513/not-able-to-style-excel-with-spreadsheet-gem-ruby –

+2

這是設置背景顏色的代碼,而不是讀它是OP要求的。 –

1

我剛剛試圖找出相同的問題,並且好像在Spreadsheet gem的當前版本(0.6.5.9)中,讀者不支持單元背景色的屬性(您只能在背景顏色中定義細胞寫作)。

以下是如何檢查所有目前可用的電池屬性:

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

經過一些實驗,但是我想通了,不是所有的人都能正確擷取。好消息是,開發商承諾落實在未來的版本單元格格更好的支持,所以我們只需要耐心等待:)

+0

http://stackoverflow.com/questions/24608513/not-able-to-style-excel-with-spreadsheet-gem-ruby –

4

我四處尋找可用於單元格背景顏色的顏色。例如:

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

我無法找到關於哪些顏色可用的好信息:pattern_fg_color。我決定尋找Excel幫助,並找到:http://dmcritchie.mvps.org/excel/colors.htm(在「16種顏色的DOS分配」)。

它看起來像頂部16色工作:

0黑,1海軍,2綠色,3蒂爾,4栗色,5紫6橄欖油,7銀, 8灰色9藍,10石灰, 11水族,12紅色,13 Fuschia,14黃色,15白色

+1

您可以使用更多的着色選項 - http://www.softwaremaniacs.net/2013/11/setting-cell-color-using-ruby.html –