Class: OoxmlParser::SharedStringTable

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb

Overview

Class for parsing shared string table

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, #boolean_attribute_value, #parse_xml, #with_data?

Methods included from OoxmlObjectAttributeHelper

#attribute_enabled?, #option_enabled?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(parent: nil) ⇒ SharedStringTable

Returns a new instance of SharedStringTable.



14
15
16
17
# File 'lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb', line 14

def initialize(parent: nil)
  @string_indexes = []
  super
end

Instance Attribute Details

#countInteger (readonly)

Returns count of shared strings.

Returns:

  • (Integer)

    count of shared strings



8
9
10
# File 'lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb', line 8

def count
  @count
end

#string_indexesArray<StringIndex> (readonly)

Returns String Index.

Returns:



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb', line 12

def string_indexes
  @string_indexes
end

#unique_countInteger (readonly)

Returns unique count of shared strings.

Returns:

  • (Integer)

    unique count of shared strings



10
11
12
# File 'lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb', line 10

def unique_count
  @unique_count
end

Instance Method Details

#parse(file) ⇒ SharedStringTable

Parse Shared string table file

Parameters:

  • file (String)

    path to file

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ooxml_parser/xlsx_parser/workbook/shared_string_table.rb', line 22

def parse(file)
  return nil unless File.exist?(file)

  document = parse_xml(file)
  node = document.xpath('*').first

  node.attributes.each do |key, value|
    case key
    when 'count'
      @count = value.value.to_i
    when 'uniqueCount'
      @unique_count = value.value.to_i
    end
  end

  node.xpath('*').each do |node_child|
    case node_child.name
    when 'si'
      @string_indexes << StringIndex.new(parent: self).parse(node_child)
    end
  end
  self
end