Class: RubyXL::SharedStringsTable

Inherits:
OOXMLTopLevelObject show all
Defined in:
lib/rubyXL/objects/shared_strings.rb

Overview

Constant Summary collapse

CONTENT_TYPE =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'.freeze
REL_TYPE =
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings'.freeze

Constants inherited from OOXMLTopLevelObject

OOXMLTopLevelObject::ROOT, OOXMLTopLevelObject::SAVE_ORDER

Instance Attribute Summary

Attributes inherited from OOXMLTopLevelObject

#root

Attributes included from OOXMLObjectInstanceMethods

#local_namespaces

Instance Method Summary collapse

Methods inherited from OOXMLTopLevelObject

#add_to_zip, #file_index, parse_file, set_namespaces

Methods included from OOXMLObjectInstanceMethods

#==, included, #index_in_collection, #init_child_nodes, #write_xml

Constructor Details

#initialize(*params) ⇒ SharedStringsTable

Returns a new instance of SharedStringsTable.



21
22
23
24
25
26
27
# File 'lib/rubyXL/objects/shared_strings.rb', line 21

def initialize(*params)
  super
  # So far, going by the structure that the original creator had in mind. However,
  # since the actual implementation is now extracted into a separate class,
  # we will be able to transparently change it later if needs be.
  @index_by_content = {}
end

Instance Method Details

#[](index) ⇒ Object



35
36
37
# File 'lib/rubyXL/objects/shared_strings.rb', line 35

def [](index)
  strings[index]
end

#add(v, index = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubyXL/objects/shared_strings.rb', line 43

def add(v, index = nil)
  index ||= strings.size

  strings[index] =
    case v
    when RubyXL::RichText then v
    when String then RubyXL::RichText.new(:t => RubyXL::Text.new(:value => v))
    when RubyXL::Text               then RubyXL::RichText.new(:t => v)
    when RubyXL::RichTextRun        then RubyXL::RichText.new(:r => [ v ])
    when RubyXL::PhoneticRun        then RubyXL::RichText.new(:r_ph => [ v ])
    when RubyXL::PhoneticProperties then RubyXL::RichText.new(:phonetic_pr => v)
    end

  @index_by_content[v.to_s] = index
end

#before_write_xml ⇒ Object



29
30
31
32
33
# File 'lib/rubyXL/objects/shared_strings.rb', line 29

def before_write_xml
  super
  self.unique_count = self.count
  self.count > 0
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rubyXL/objects/shared_strings.rb', line 39

def empty?
  strings.empty?
end

#get_index(str, add_if_missing = false) ⇒ Object



59
60
61
62
63
# File 'lib/rubyXL/objects/shared_strings.rb', line 59

def get_index(str, add_if_missing = false)
  index = @index_by_content[str]
  index = add(str) if index.nil? && add_if_missing
  index
end

#xlsx_path ⇒ Object



65
66
67
# File 'lib/rubyXL/objects/shared_strings.rb', line 65

def xlsx_path
  ROOT.join('xl', 'sharedStrings.xml')
end