Class: Xlsxtream::Workbook

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

Constant Summary collapse

FONT_FAMILY_IDS =
{
  ''           => 0,
  'roman'      => 1,
  'swiss'      => 2,
  'modern'     => 3,
  'script'     => 4,
  'decorative' => 5
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Workbook.



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

def initialize(output = nil, options = {})
  output ||= StringIO.new
  @options = options
  io_wrapper = options[:io_wrapper] || IO::RubyZip
  if output.is_a?(String) || !output.respond_to?(:<<)
    @file = File.open(output, 'wb')
    @io = io_wrapper.new(@file)
  else
    @io = io_wrapper.new(output)
  end
  @sst = SharedStringTable.new
  @worksheets = Hash.new { |hash, name| hash[name] = hash.size + 1 }
end

Class Method Details

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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xlsxtream/workbook.rb', line 23

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

Instance Method Details

#closeObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xlsxtream/workbook.rb', line 73

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

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

Yields:

  • (worksheet)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xlsxtream/workbook.rb', line 52

def write_worksheet(name = nil, options = {})
  if name.is_a? Hash and options.empty?
    options = name
    name = nil
  end
  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