Class: LazyWombat::Xlsx

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy-wombat.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Xlsx

Returns a new instance of Xlsx.



9
10
11
12
13
# File 'lib/lazy-wombat.rb', line 9

def initialize options = {}, &block
  @package_options = options
  @package = Axlsx::Package.new
  instance_exec &block
end

Instance Method Details

#build_rowObject



46
47
48
# File 'lib/lazy-wombat.rb', line 46

def build_row
  @row ||= spreadsheet.add_row.tap{ @cell = nil }
end

#build_spreadsheetObject



29
30
31
# File 'lib/lazy-wombat.rb', line 29

def build_spreadsheet
  @spreadsheet ||= @package.workbook.add_worksheet.tap { @row = @cell = nil }
end

#cell(value, options = {}) ⇒ Object Also known as: td



50
51
52
53
54
55
56
57
58
# File 'lib/lazy-wombat.rb', line 50

def cell value, options = {}
  unless @row_options.respond_to?(:empty?) ? @row_options.empty? : !@row_options
    options = @row_options.merge(options){ |key, row_option, cell_option| Array.new << row_option << cell_option }
  end
  @cell = row.add_cell value.to_s, style: style_from_options(options)
  unless options[:colspan].respond_to?(:empty?) ? options[:colspan].empty? : !options[:colspan]
    spreadsheet.merge_cells "#{pos[:x]}#{pos[:y]}:#{shift_x(pos[:x], 5)}#{pos[:y]}"
  end
end

#row(options = {}, &block) ⇒ Object Also known as: tr



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lazy-wombat.rb', line 33

def row options = {}, &block
  if block_given?
    @row_options = options
    spreadsheet.add_row do |row|
      @row = row
      instance_exec &block
    end
  else
    build_row
  end
end

#save(file_name) ⇒ Object



61
62
63
64
# File 'lib/lazy-wombat.rb', line 61

def save file_name
  @package.serialize file_name
  File.open file_name
end

#spreadsheet(options = {}, &block) ⇒ Object Also known as: table



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lazy-wombat.rb', line 15

def spreadsheet options = {}, &block
  if block_given?
    @package.workbook.add_worksheet do |spreadsheet|
      @spreadsheet = spreadsheet
      @spreadsheet.name = options[:name] unless options[:name].respond_to?(:empty?) ? options[:name].empty? : !options[:name]
      build_spreadsheet_styles
      instance_exec &block
    end
  else
    build_spreadsheet
  end
end

#to_temp_fileObject



66
67
68
69
70
71
72
# File 'lib/lazy-wombat.rb', line 66

def to_temp_file
  stream = @package.to_stream
  Tempfile.new(%w(temporary-workbook .xlsx), encoding: 'utf-8').tap do |file|
    file.write stream.read
    file.close
  end
end

#to_xmlObject



74
75
76
# File 'lib/lazy-wombat.rb', line 74

def to_xml
  spreadsheet.to_xml_string
end