ruby-on-rails-4axlsx

Axlsx Formatting


Is it possible to format both a row and column?

For instance I am doing a loop that uses the index to style rows different colors based on if it is even or odd but I also want to style a column that has percentages to use the :num_fmt => 9

Also why when I am presenting the number as something like 1.2 does that end up changing it to 120%, all I want is for that data to look like 1.2%

@people.each_with_index | person, index |
if index.odd?
sheet.add_row [person['name'], person['rate']]
else
sheet.add_row [person['name'],person['rate']], :style => even_row
end
end

(my even row style is set at the top of my code)


Solution

  • I figured this out, you need to do set your style using something like

    percent = s.add_style(:num_fmt => 9)
    even_row_percent = s.add_style(:bg_color => 'blue', :fg_color => 'white', :b => false, :format_code => 0%)
    even_row = s.add_style(:bg_color => 'blue', :fg_color =>'white', :b => false)
    

    then in your loop just use an each with index and then use a if statement like

    if index.odd
     sheet.add_row[
     item[:value], 
     item[:value_percent]], :style => [nil, percent]
    else
     sheet.add_row[
     item[:value], 
     item[:value]], :style => [even_row, even_row_percent]
    end