Class: AxlsxReport::Base
- Inherits:
-
Object
- Object
- AxlsxReport::Base
- Includes:
- ColumnNameConv
- Defined in:
- lib/axlsx_report/base.rb
Class Method Summary collapse
-
.column(col_name, *args, &block) ⇒ Object
Define report column.
-
.group(name, &block) ⇒ Object
Define group of columns.
-
.group_height(num) ⇒ Object
Define group row height.
-
.head_height(num) ⇒ Object
Define head row height.
-
.numerate_columns ⇒ Object
Add 1,2,3…N row below head row.
-
.sheet_name(name = nil, &block) ⇒ Object
Define sheet name.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Add row to the report.
-
#done ⇒ Object
Finalize document formatting after data collecting and before saving.
-
#initialize(package = nil) ⇒ Base
constructor
Creates new report.
-
#save(file) ⇒ Object
Save xlsx file with provided data.
-
#sheet ⇒ Axlsx::Worksheet
Axlsx sheet.
-
#sheet_name ⇒ Object
Returns sheet name.
Methods included from ColumnNameConv
#column_name_to_num, #column_num_to_name
Constructor Details
#initialize(package = nil) ⇒ Base
Creates new report. By default creates own Axlsx::Package.
13 14 15 |
# File 'lib/axlsx_report/base.rb', line 13 def initialize(package = nil) @package = package || Axlsx::Package.new end |
Class Method Details
.column(col_name, *args, &block) ⇒ Object
Define report column.
Usage:
Human = Struct.new(:first_name, :last_name, :birthday)
class Report < AxlsxReport::Base
# calculate column value with block:
column 'First Name', width: 10 do |human|
human.first_name
end
# or lambda in context of given object
column 'Last Name', -> { last_name }, width: 15
# or chagne context using with:
column 'Age', -> { Date.today.year - year }, with: :birthday
end
args: callable = nil, options = {}
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/axlsx_report/base.rb', line 39 def self.column(col_name, *args, &block) @columns ||= [] = if args.last.is_a? Hash args.pop else {} end [:group] = @current_group if @current_group callable = args.first || block @columns << Column.new(col_name, callable, ) end |
.group(name, &block) ⇒ Object
Define group of columns
Usage:
Human = Struct.new(:first_name, :last_name, :birthday)
class Report < AxlsxReport::Base
group 'Name' do
column 'First', &:first_name
column 'Last', &:last_name
end
column 'Birthday', &:birthday
end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/axlsx_report/base.rb', line 65 def self.group(name, &block) @columns ||= [] unless @groups @groups = [] define_method :groups do self.class.instance_eval { @groups } end end raise 'Nested groups are not implemented yet' if @current_group @current_group = Group.new(name, @columns.length) @groups << @current_group instance_exec(&block) @current_group.end_index = @columns.length - 1 @current_group = nil end |
.group_height(num) ⇒ Object
Define group row height. Used if any groups are defiend. Autoheight is used by default
84 85 86 |
# File 'lib/axlsx_report/base.rb', line 84 def self.group_height(num) define_method(:group_height) { num } end |
.head_height(num) ⇒ Object
Define head row height. Autoheight is used by default
91 92 93 |
# File 'lib/axlsx_report/base.rb', line 91 def self.head_height(num) define_method(:head_height) { num } end |
.numerate_columns ⇒ Object
Add 1,2,3…N row below head row
96 97 98 |
# File 'lib/axlsx_report/base.rb', line 96 def self.numerate_columns define_method(:numerate_columns) { true } end |
.sheet_name(name = nil, &block) ⇒ Object
Define sheet name.
104 105 106 107 108 109 110 |
# File 'lib/axlsx_report/base.rb', line 104 def self.sheet_name(name = nil, &block) if block_given? define_method(:sheet_name, &block) else define_method(:sheet_name) { name } end end |
Instance Method Details
#<<(obj) ⇒ Object
Add row to the report.
114 115 116 117 118 |
# File 'lib/axlsx_report/base.rb', line 114 def <<(obj) row = columns.map { |column| column.value(self, obj) } add_totals row sheet.add_row row, end |
#done ⇒ Object
Called during #save.
Don’t call twice. Don’t call before all data provided.
Finalize document formatting after data collecting and before saving.
163 164 165 166 167 |
# File 'lib/axlsx_report/base.rb', line 163 def done apply_width(sheet) merge_same(sheet) sheet.add_row(@totals) if @totals end |
#save(file) ⇒ Object
Save xlsx file with provided data
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/axlsx_report/base.rb', line 149 def save(file) done begin @package.serialize(file) rescue Errno::EACCES => e puts "#{file} is protected!" file = file + ".tmp" @package.serialize(file) end end |
#sheet ⇒ Axlsx::Worksheet
Axlsx sheet.
171 172 173 |
# File 'lib/axlsx_report/base.rb', line 171 def sheet @sheet ||= init_sheet(sheet_name) end |
#sheet_name ⇒ Object
Returns sheet name.
May be overriden to provide custom sheet name
124 125 126 |
# File 'lib/axlsx_report/base.rb', line 124 def sheet_name 'Sheet1' end |