Class: IOStreams::Xlsx::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/io_streams/xlsx/reader.rb

Instance Attribute Summary

Attributes inherited from Reader

#input_stream

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reader

open, stream

Constructor Details

#initialize(file_name) ⇒ Reader

Returns a new instance of Reader.



15
16
17
18
19
20
21
22
23
24
# File 'lib/io_streams/xlsx/reader.rb', line 15

def initialize(file_name)
  begin
    require "creek" unless defined?(Creek::Book)
  rescue LoadError => e
    raise(LoadError, "Please install the 'creek' gem for xlsx streaming support. #{e.message}")
  end

  workbook   = Creek::Book.new(file_name, check_file_extension: false)
  @worksheet = workbook.sheets[0]
end

Class Method Details

.file(file_name, original_file_name: file_name, &block) ⇒ Object

Convert a xlsx, or xlsm file into CSV format.



7
8
9
10
11
12
13
# File 'lib/io_streams/xlsx/reader.rb', line 7

def self.file(file_name, original_file_name: file_name, &block)
  # Stream into a temp file as csv
  Utils.temp_file_name("iostreams_csv") do |temp_file_name|
    ::File.open(temp_file_name, "wb") { |io| new(file_name).each { |lines| io << lines.to_csv } }
    ::File.open(temp_file_name, "rb", &block)
  end
end

Instance Method Details

#eachObject

Returns each [Array] row from the spreadsheet



27
28
29
# File 'lib/io_streams/xlsx/reader.rb', line 27

def each
  @worksheet.rows.each { |row| yield row.values }
end