I would like to be able to use the Axlsx gem to write out an Excel spreadsheet with different styles applied to different columns.
I am writing an Array of Hashes an entire row at a time and, as a result, I cannot seem to format the columns differently as needed.
I don't know how to say, "for columns A-D use default styling, however for column E and F, use horizontal center alignment".
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
# Default Style for all cells
standard_text = wb.styles.add_style( :alignment => {:vertical=>:center, :wrap_text=>true} )
# Custom styling to be applied to cells in rows E and F only
custom_text = wb.styles.add_style( :alignment => {:horizontal=>:center} )
assessment_technology_hashes.each do |rows|
sheet.add_row(rows.values, :height => 35, :style => standard_text)
end
Here is the structure of the Array of hashes for rows
that is being written with add_row
:
{:vendor_name=>"vendor", :link=>"Link\n", :importance=>"Low Priority", :score=>"5", :overall_score=>"4.5", :match=>"Yes", :access=>"Anywhere", :title=>"Full Title"}
{:vendor_name=>"vendor2", :link=>"Link2\n", :importance=>"Medium Priority", :score=>"7", :overall_score=>"8.5", :match=>"Yes", :access=>"Typical", :title=>"Full Title"}
...
...
In this scenario, is the correct approach to write all of the data with the primary formatting standard_text
that is desired, and then apply the custom formatting AFTER all the data has been written?
I thought this might work in the loop to only write out rows when the condition is true but it always returns false:
sheet.add_row(rows.values, :height => 35, :style => custom_text) if rows.key?(:overall_score) || rows.key?(:match)
Can someone point me in the correct direction to do this?