Class: SimpleXlsxReader::Loader::ZipReader
- Inherits:
-
Struct
- Object
- Struct
- SimpleXlsxReader::Loader::ZipReader
- Defined in:
- lib/simple_xlsx_reader/loader.rb
Instance Attribute Summary collapse
-
#loader ⇒ Object
Returns the value of attribute loader.
-
#string_or_io ⇒ Object
Returns the value of attribute string_or_io.
-
#zip ⇒ Object
readonly
Returns the value of attribute zip.
Instance Method Summary collapse
- #add_sheet_parser_at_index(i) ⇒ Object
- #entry_at(path, &block) ⇒ Object
-
#initialize(*args) ⇒ ZipReader
constructor
A new instance of ZipReader.
- #read ⇒ Object
Constructor Details
#initialize(*args) ⇒ ZipReader
Returns a new instance of ZipReader.
25 26 27 28 |
# File 'lib/simple_xlsx_reader/loader.rb', line 25 def initialize(*args) super @zip = SimpleXlsxReader::Zip.open_buffer(string_or_io) end |
Instance Attribute Details
#loader ⇒ Object
Returns the value of attribute loader
22 23 24 |
# File 'lib/simple_xlsx_reader/loader.rb', line 22 def loader @loader end |
#string_or_io ⇒ Object
Returns the value of attribute string_or_io
22 23 24 |
# File 'lib/simple_xlsx_reader/loader.rb', line 22 def string_or_io @string_or_io end |
#zip ⇒ Object (readonly)
Returns the value of attribute zip.
23 24 25 |
# File 'lib/simple_xlsx_reader/loader.rb', line 23 def zip @zip end |
Instance Method Details
#add_sheet_parser_at_index(i) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/simple_xlsx_reader/loader.rb', line 84 def add_sheet_parser_at_index(i) sheet_file_name = "xl/worksheets/sheet#{i}.xml" return unless (entry = entry_at(sheet_file_name)) parser = SheetParser.new( file_io: entry.get_input_stream, loader: loader ) relationship_file_name = "xl/worksheets/_rels/sheet#{i}.xml.rels" if (rel = entry_at(relationship_file_name)) parser.xrels_file = rel.get_input_stream end loader.sheet_parsers << parser end |
#entry_at(path, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/simple_xlsx_reader/loader.rb', line 67 def entry_at(path, &block) # Older and newer (post-mid-2021) RubyZip normalizes pathnames, # but unfortunately there is a time in between where it doesn't. # Rather than require a specific version, let's just be flexible. entry = zip.find_entry(path) || # *nix-generated zip.find_entry(path.tr('/', '\\')) || # Windows-generated zip.find_entry(path.downcase) || # Sometimes it's lowercase zip.find_entry(path.tr('/', '\\').downcase) # Sometimes it's lowercase if block entry.get_input_stream(&block) else entry end end |
#read ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/simple_xlsx_reader/loader.rb', line 30 def read entry_at('xl/workbook.xml') do |file_io| loader.sheet_toc, loader.base_date = *WorkbookParser.parse(file_io) end entry_at('xl/styles.xml') do |file_io| loader.style_types = StyleTypesParser.parse(file_io) end # optional feature used by excel, # but not often used by xlsx generation libraries if (ss_entry = entry_at('xl/sharedStrings.xml')) ss_entry.get_input_stream do |file| loader.shared_strings = SharedStringsParser.parse(file) end else loader.shared_strings = [] end loader.sheet_parsers = [] # Sometimes there's a zero-index sheet.xml, ex. # Google Docs creates: # xl/worksheets/sheet.xml # xl/worksheets/sheet1.xml # xl/worksheets/sheet2.xml # While Excel creates: # xl/worksheets/sheet1.xml # xl/worksheets/sheet2.xml add_sheet_parser_at_index(nil) i = 1 while(add_sheet_parser_at_index(i)) do i += 1 end end |