class Crek::SharedStrings

Attributes

book[R]
dictionary[R]

Public Class Methods

new(book) click to toggle source
# File lib/crek/shared_strings.rb, line 10
def initialize book
  @book = book
  parse_shared_shared_strings
end
parse_shared_string_from_document(xml) click to toggle source
# File lib/crek/shared_strings.rb, line 28
def self.parse_shared_string_from_document(xml)
  dictionary = Hash.new

  xml.css('si').each_with_index do |si, idx|
    text_nodes = si.css('t')
    if text_nodes.count == 1 # plain text node
      dictionary[idx] = text_nodes.first.content
    else # rich text nodes with text fragments
      dictionary[idx] = text_nodes.map{ |n| Crek::Styles::Converter.unescape_string(n.content) }.join('')
    end
  end

  dictionary
end

Public Instance Methods

parse_shared_shared_strings() click to toggle source
# File lib/crek/shared_strings.rb, line 15
def parse_shared_shared_strings
  path = "xl/sharedStrings.xml"
  if @book.files.file.exist?(path)
    doc = @book.files.file.open path
    xml = Nokogiri::XML::Document.parse doc
    parse_shared_string_from_document(xml)
  end
end
parse_shared_string_from_document(xml) click to toggle source
# File lib/crek/shared_strings.rb, line 24
def parse_shared_string_from_document(xml)
  @dictionary = self.class.parse_shared_string_from_document(xml)
end