Class: AxlsxReport::Base

Inherits:
Object
  • Object
show all
Includes:
ColumnNameConv
Defined in:
lib/axlsx_report/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • package (Axlsx::Package) (defaults to: nil)
    • provide existing package to add report sheet to combined document.



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 = {}

Parameters:

  • name (String)
    • column header

  • callable (Proc)
    • use proc or lambda instead of block to get cell value. Optional.

  • options (Hash)
    • last param,

  • block

See Also:



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 ||= []
  options =
    if args.last.is_a? Hash
      args.pop
    else
      {}
    end
  options[:group] = @current_group if @current_group
  callable = args.first || block
  @columns << Column.new(col_name, callable, options)
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

Parameters:

  • name (String)

    group name



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

Parameters:

  • num (Integer)

    group row height



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

Parameters:

  • num (Integer)

    head row height



91
92
93
# File 'lib/axlsx_report/base.rb', line 91

def self.head_height(num)
  define_method(:head_height) { num }
end

.numerate_columnsObject

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.

Parameters:

  • name (String) (defaults to: nil)

    (nil) static sheet name.

  • block

    dinamic sheet name (if given)



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.

Parameters:

  • obj (Any)

    object providing the data for columns



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, row_options
end

#doneObject

Note:

Called during #save.

Note:

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

Parameters:

  • file (String)

    File name the data to be serialized



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

#sheetAxlsx::Worksheet

Axlsx sheet.

Returns:

  • (Axlsx::Worksheet)


171
172
173
# File 'lib/axlsx_report/base.rb', line 171

def sheet
  @sheet ||= init_sheet(sheet_name)
end

#sheet_nameObject

Returns sheet name.

May be overriden to provide custom sheet name

See Also:



124
125
126
# File 'lib/axlsx_report/base.rb', line 124

def sheet_name
  'Sheet1'
end