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, options = {}) ⇒ Workbook

Returns a new instance of Workbook.



37
38
39
40
41
42
# File 'lib/xlsxtream/workbook.rb', line 37

def initialize(output, options = {})
  @writer = ZipKitWriter.with_output_to(output)
  @options = options
  @sst = SharedStringTable.new
  @worksheets = []
end

Class Method Details

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



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

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

Instance Method Details

#add_worksheet(*args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xlsxtream/workbook.rb', line 44

def add_worksheet(*args, &block)
  if block_given?
    # This method used to be an alias for `write_worksheet`. This was never publicly documented,
    # but to avoid breaking this private API we keep the old behaviour when called with a block.
    Kernel.warn "#{caller.first[/.*:\d+:(?=in `)/]} warning: Calling #{self.class}#add_worksheet with a block is deprecated, use #write_worksheet instead."
    return write_worksheet(*args, &block)
  end

  unless @worksheets.all? { |ws| ws.closed? }
    fail Error, "Close the current worksheet before adding a new one"
  end

  build_worksheet(*args)
end

#closeObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/xlsxtream/workbook.rb', line 68

def close
  write_workbook
  write_styles
  write_sst unless @sst.empty?
  write_workbook_rels
  write_root_rels
  write_content_types
  @writer.close

  nil
end

#write_worksheet(*args) {|worksheet| ... } ⇒ Object

Yields:

  • (worksheet)


59
60
61
62
63
64
65
66
# File 'lib/xlsxtream/workbook.rb', line 59

def write_worksheet(*args)
  worksheet = build_worksheet(*args)

  yield worksheet if block_given?
  worksheet.close

  nil
end