Class: Xlsxtream::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxtream/workbook.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, options = {}) ⇒ Workbook

Returns a new instance of Workbook.



30
31
32
33
34
35
36
# File 'lib/xlsxtream/workbook.rb', line 30

def initialize(data = nil, options = {})
  @options = options
  io_wrapper = options[:io_wrapper] || IO::RubyZip
  @io = io_wrapper.new(data || StringIO.new)
  @sst = SharedStringTable.new
  @worksheets = Hash.new { |hash, name| hash[name] = hash.size + 1 }
end

Class Method Details

.open(data = nil, options = {}) ⇒ Object



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

def open(data = nil, options = {})
  workbook = new(data, options)
  if block_given?
    begin
      yield workbook
    ensure
      workbook.close
    end
  else
    workbook
  end
end

Instance Method Details

#closeObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/xlsxtream/workbook.rb', line 55

def close
  write_workbook
  write_styles
  write_sst unless @sst.empty?
  write_workbook_rels
  write_root_rels
  write_content_types
  @io.close
  nil
end

#write_worksheet(name = nil, options = {}) {|worksheet| ... } ⇒ Object Also known as: add_worksheet

Yields:

  • (worksheet)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/xlsxtream/workbook.rb', line 38

def write_worksheet(name = nil, options = {})
  use_sst = options.fetch(:use_shared_strings, @options[:use_shared_strings])
  auto_format = options.fetch(:auto_format, @options[:auto_format])
  sst = use_sst ? @sst : nil

  name ||= "Sheet#{@worksheets.size + 1}"
  sheet_id = @worksheets[name]
  @io.add_file "xl/worksheets/sheet#{sheet_id}.xml"

  worksheet = Worksheet.new(@io, :sst => sst, :auto_format => auto_format)
  yield worksheet if block_given?
  worksheet.close

  nil
end