3

I am using axlsx gem to generate Excel sheets in Ruby on Rails.

I have this line which adds a row to an Excel sheet

current_row = sheet.add_row [v1, v2, v3], :style => style1

Now, v1, v2 & v3 in the above code are currency values. And any of these 3 values could be a negative value as well.

Now, whichever value is negative, I would like that value to be displayed in red text font (NOT background color). If the value is positive, then the text should be in normal black color.

How can I achieve this conditional text color?

Biju
  • 820
  • 1
  • 11
  • 34

1 Answers1

2

below is sample step

# first, create style
black_cell = s.add_style :bg_color => "FF", :fg_color => "00", :sz => 14, :alignment => { :horizontal=> :center }
red_cell = s.add_style :bg_color => "FF", :fg_color => "FF0000", :sz => 20, :alignment => { :horizontal=> :center }

# second create array to manage style, if many element you can do loop the array instead using v1, v2, v3
arr1 = Array.new
sv = v1 >= 0 ? black_cell : red_cell
arr1 << sv 
sv = v2 >= 0 ? black_cell : red_cell
arr1 << sv 
sv = v3 >= 0 ? black_cell : red_cell
arr1 << sv 

# third create the row
current_row = sheet.add_row [v1, v2, v3], :style => arr1
widjajayd
  • 6,090
  • 4
  • 30
  • 41