Prabhakaran
Prabhakaran

Reputation: 4019

Set width and background for cell in excel using spreadsheet rails

I used the following code

title_format = Spreadsheet::Format.new(:color => :blue, :pattern_fg_color => :red, :weight => :bold, :size => 18)
sheet1.row(0).set_format(0, title_format)

Font color is working but I am unable to get background color and also i need to increase the width of the cell. How can i do.

Edit 1

I changed like this

bg_color = Spreadsheet::Format.new({
    :weight => :bold,
    :pattern_fg_color => :red,
    :size => 8
    })

[1,2,3,4,5,6,7,8,9,10,11].each{
    |col|
    sheet1.row(0).set_format(col,bg_color) 
}

What mistake i did in this. It is not wokring

Edit 2

This solves and working

bg_color = Spreadsheet::Format.new({
        :weight => :bold,
        :patterb_fg_color => :red,
        :size => 8
        })

sheet1.row(0).default_format = bg_color

Upvotes: 1

Views: 2329

Answers (1)

crackedmind
crackedmind

Reputation: 928

You can set background color through:

title_format = Spreadsheet::Format.new({
  :weight           => :bold,
  :pattern_bg_color => :grey,
  :size             => 8
})

sheet.row(0).set_format(column_index, title_format)

The width of the cell you can set like this:

sheet1.column(column_index).width = 1 # but i don't really know in what units it is measured

Upvotes: 2

Related Questions