Class: SimpleXlsxReader::Document::Xml

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_xlsx_reader.rb

Overview

For internal use; stores source xml in nokogiri documents

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#shared_stringsObject

Returns the value of attribute shared_strings.



72
73
74
# File 'lib/simple_xlsx_reader.rb', line 72

def shared_strings
  @shared_strings
end

#sheetsObject

Returns the value of attribute sheets.



72
73
74
# File 'lib/simple_xlsx_reader.rb', line 72

def sheets
  @sheets
end

#stylesObject

Returns the value of attribute styles.



72
73
74
# File 'lib/simple_xlsx_reader.rb', line 72

def styles
  @styles
end

#workbookObject

Returns the value of attribute workbook.



72
73
74
# File 'lib/simple_xlsx_reader.rb', line 72

def workbook
  @workbook
end

Class Method Details

.load(file_path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simple_xlsx_reader.rb', line 74

def self.load(file_path)
  self.new.tap do |xml|
    SimpleXlsxReader::Zip.open(file_path) do |zip|
      xml.workbook = Nokogiri::XML(zip.read('xl/workbook.xml')).remove_namespaces!
      xml.styles   = Nokogiri::XML(zip.read('xl/styles.xml')).remove_namespaces!

      # optional feature used by excel, but not often used by xlsx
      # generation libraries
      ss_file =  (zip.to_a.map(&:name) & ['xl/sharedStrings.xml','xl/sharedstrings.xml'])[0]
      if ss_file
        xml.shared_strings = Nokogiri::XML(zip.read(ss_file)).remove_namespaces!
      end

      xml.sheets = []
      i = 0
      loop do
        i += 1
        break if !zip.file.file?("xl/worksheets/sheet#{i}.xml")

        xml.sheets <<
          Nokogiri::XML(zip.read("xl/worksheets/sheet#{i}.xml")).remove_namespaces!
      end
    end
  end
end