Class: LazyWombat::Xlsx
- Inherits:
-
Object
- Object
- LazyWombat::Xlsx
- Defined in:
- lib/lazy-wombat.rb
Instance Method Summary collapse
- #build_row ⇒ Object
- #build_spreadsheet ⇒ Object
- #cell(value, options = {}) ⇒ Object (also: #td)
-
#initialize(options = {}, &block) ⇒ Xlsx
constructor
A new instance of Xlsx.
- #row(options = {}, &block) ⇒ Object (also: #tr)
- #save(file_name) ⇒ Object
- #spreadsheet(options = {}, &block) ⇒ Object (also: #table)
- #to_temp_file ⇒ Object
- #to_xml ⇒ Object
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 = {}, &block @package_options = @package = Axlsx::Package.new instance_exec &block end |
Instance Method Details
#build_row ⇒ Object
46 47 48 |
# File 'lib/lazy-wombat.rb', line 46 def build_row @row ||= spreadsheet.add_row.tap{ @cell = nil } end |
#build_spreadsheet ⇒ Object
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, = {} unless @row_options.respond_to?(:empty?) ? @row_options.empty? : !@row_options = @row_options.merge(){ |key, row_option, cell_option| Array.new << row_option << cell_option } end @cell = row.add_cell value.to_s, style: () unless [:colspan].respond_to?(:empty?) ? [:colspan].empty? : ![: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 = {}, &block if block_given? @row_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 = {}, &block if block_given? @package.workbook.add_worksheet do |spreadsheet| @spreadsheet = spreadsheet @spreadsheet.name = [:name] unless [:name].respond_to?(:empty?) ? [:name].empty? : ![:name] build_spreadsheet_styles instance_exec &block end else build_spreadsheet end end |
#to_temp_file ⇒ Object
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_xml ⇒ Object
74 75 76 |
# File 'lib/lazy-wombat.rb', line 74 def to_xml spreadsheet.to_xml_string end |