Class: OpenXML::SpreadsheetML::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx/parser.rb,
lib/xlsx/workbook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xlsx_path) {|_self| ... } ⇒ Workbook

Returns a new instance of Workbook.

Yields:

  • (_self)

Yield Parameters:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/xlsx/parser.rb', line 4

def initialize xlsx_path
  @sheets = []
  @shared_strings = nil
  Zip::File.open(xlsx_path) do |zf|
    zf.each do |entry|
      content = entry.get_input_stream.read
      case entry.name
      when %r{^xl/workbook\.xml$}i
        workbook_parser content
      when %r{^xl/sharedStrings\.xml$}i
        shared_strings_parser content
      when %r{^xl/worksheets/sheet\d*\.xml$}i
        sheet_parser content
      end
    end
  end
  merge_sheets(@sheet_names) if @sheet_names
  merge_shared_strings(@shared_strings) if @shared_strings
  yield(self) if block_given?
end

Instance Attribute Details

#calc_chainObject

Returns the value of attribute calc_chain.



4
5
6
# File 'lib/xlsx/workbook.rb', line 4

def calc_chain
  @calc_chain
end

#sheetsObject

Returns the value of attribute sheets.



4
5
6
# File 'lib/xlsx/workbook.rb', line 4

def sheets
  @sheets
end

#stylesObject

Returns the value of attribute styles.



4
5
6
# File 'lib/xlsx/workbook.rb', line 4

def styles
  @styles
end

#themeObject

Returns the value of attribute theme.



4
5
6
# File 'lib/xlsx/workbook.rb', line 4

def theme
  @theme
end

#xml_mapsObject

Returns the value of attribute xml_maps.



4
5
6
# File 'lib/xlsx/workbook.rb', line 4

def xml_maps
  @xml_maps
end

Instance Method Details

#[](index) ⇒ Object



6
7
8
9
10
11
# File 'lib/xlsx/workbook.rb', line 6

def [] index
  self.sheets.each do |sheet|
    return sheet if sheet.name == index
  end
  nil
end

#merge_shared_strings(shared_strings_reference) ⇒ Object



22
23
24
25
26
# File 'lib/xlsx/workbook.rb', line 22

def merge_shared_strings shared_strings_reference
  sheets.each do |sheet|
    sheet.merge_shared_strings shared_strings_reference
  end
end

#merge_sheets(sheets) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/xlsx/workbook.rb', line 13

def merge_sheets sheets
  new_sheets = []
  self.sheets.zip(sheets) do |x, y|
    new_sheets << x + y
  end
  sheets = new_sheets
  self
end