Class: ChartCandy::Builder::XlsBuilder
- Inherits:
-
Object
- Object
- ChartCandy::Builder::XlsBuilder
- Defined in:
- lib/chart-candy/builder/xls_builder.rb
Instance Attribute Summary collapse
-
#current_column ⇒ Object
readonly
Returns the value of attribute current_column.
-
#current_row ⇒ Object
readonly
Returns the value of attribute current_row.
-
#workbook ⇒ Object
readonly
Returns the value of attribute workbook.
Class Method Summary collapse
Instance Method Summary collapse
- #add_align_right_formats(formats) ⇒ Object
- #build_formats ⇒ Object
- #cell(content = nil, options = {}) ⇒ Object
- #format_data(data) ⇒ Object
- #generate ⇒ Object
- #generate_chart_donut_table ⇒ Object
- #generate_chart_line_table ⇒ Object
- #header ⇒ Object
-
#initialize(chart) ⇒ XlsBuilder
constructor
A new instance of XlsBuilder.
- #reset_columns_width ⇒ Object
- #row(data = [], format = :normal) ⇒ Object
- #set_cell_format(nature) ⇒ Object
- #set_columns_width ⇒ Object
- #skip_row ⇒ Object
Constructor Details
#initialize(chart) ⇒ XlsBuilder
Returns a new instance of XlsBuilder.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 13 def initialize(chart) @chart = chart @workbook = Spreadsheet::Workbook.new @sheet = @workbook.create_worksheet @formats = build_formats @current_row = -1 @current_row_format = :normal @current_column = 0 @columns_width = [] @max_column_width = 50 @default_column_width = 10 end |
Instance Attribute Details
#current_column ⇒ Object (readonly)
Returns the value of attribute current_column.
4 5 6 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 4 def current_column @current_column end |
#current_row ⇒ Object (readonly)
Returns the value of attribute current_row.
4 5 6 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 4 def current_row @current_row end |
#workbook ⇒ Object (readonly)
Returns the value of attribute workbook.
4 5 6 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 4 def workbook @workbook end |
Class Method Details
.chart_to_xls(chart) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 6 def self.chart_to_xls(chart) xls = self.new(chart) xls.generate return xls.workbook end |
Instance Method Details
#add_align_right_formats(formats) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 28 def add_align_right_formats(formats) align_right = {} formats.each do |k,origin| new_format = origin.dup new_format.horizontal_align = :right align_right["#{k}_right".to_sym] = new_format end formats.merge! align_right end |
#build_formats ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 41 def build_formats f = {} f[:h1] = Spreadsheet::Format.new(weight: :bold, size: 16, horizontal_align: :left, vertical_align: :middle) f[:h2] = Spreadsheet::Format.new(weight: :normal, size: 12, horizontal_align: :left, vertical_align: :middle) f[:h3] = Spreadsheet::Format.new(weight: :normal, size: 9, horizontal_align: :left, vertical_align: :middle) f[:h4] = Spreadsheet::Format.new(weight: :bold, size: 8, horizontal_align: :left, vertical_align: :middle) f[:th] = Spreadsheet::Format.new(weight: :bold, size: 8, horizontal_align: :center, vertical_align: :middle, pattern_fg_color: :xls_color_19, pattern: 1) f[:th_foot] = Spreadsheet::Format.new(weight: :bold, size: 8, horizontal_align: :left, vertical_align: :middle, pattern_fg_color: :xls_color_19, pattern: 1) f[:normal] = Spreadsheet::Format.new(size: 8, horizontal_align: :left , vertical_align: :middle) add_align_right_formats f return f end |
#cell(content = nil, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 57 def cell(content=nil, ={}) .reverse_merge! format: nil, nature: :text row_obj = @sheet.row(current_row) row_obj.set_format current_column, @formats[[:format]] if [:format] set_cell_format [:nature] row_obj.height = row_obj.format(0).font.size * 1.6 parsed_content = format_data(content) @sheet[current_row, current_column] = parsed_content @columns_width[current_column] = parsed_content.to_s.length if @columns_width[current_column].to_i < parsed_content.to_s.length @current_column += 1 end |
#format_data(data) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 75 def format_data(data) case when data.is_a?(Time) then data.strftime('%d-%m-%Y') else data end end |
#generate ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 82 def generate header case @chart[:nature] when 'line' then generate_chart_line_table when 'donut' then generate_chart_donut_table end set_columns_width end |
#generate_chart_donut_table ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 111 def generate_chart_donut_table row [@chart[:label], @chart[:value]], :th @chart[:slices].map { |s| row [s[:label], s[:value]] } row [@chart[:total][:label], @chart[:total][:value]], :th_foot end |
#generate_chart_line_table ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 93 def generate_chart_line_table row @chart[:axis][:x][:label], :th @chart[:lines].map { |l| cell l[:label] } @chart[:lines][0][:data].each_with_index do |l,i| row cell l[:x], nature: @chart[:axis][:x][:nature] @chart[:lines].each { |line| cell line[:data][i][:y], nature: @chart[:axis][:y][:nature] } end row @chart[:lines][0][:total][:label], :th_foot @chart[:lines].map { |l| cell l[:total][:value], nature: :number } end |
#header ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 119 def header row @chart[:title], :h1 row @chart[:period], :h3 if @chart[:period] reset_columns_width skip_row end |
#reset_columns_width ⇒ Object
128 129 130 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 128 def reset_columns_width @columns_width = Array.new(@columns_width.length, @default_column_width) end |
#row(data = [], format = :normal) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 132 def row(data=[], format = :normal) @current_column = 0 @current_row += 1 @sheet.row(current_row).default_format = @formats[format] @current_row_format = format [data].flatten.each { |d| cell(d) } end |
#set_cell_format(nature) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 142 def set_cell_format(nature) if nature == :number f = "#{@current_row_format}_right".gsub('right_right', 'right').to_sym @sheet.row(current_row).set_format(current_column, @formats[f]) end end |
#set_columns_width ⇒ Object
150 151 152 153 154 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 150 def set_columns_width @columns_width.each_with_index do |c,i| @sheet.column(i).width = (c.to_i < @default_column_width) ? @default_column_width : c.to_i end end |
#skip_row ⇒ Object
156 157 158 |
# File 'lib/chart-candy/builder/xls_builder.rb', line 156 def skip_row @current_row += 1 end |