lib/io_streams/xlsx/reader.rb in iostreams-0.8.2 vs lib/io_streams/xlsx/reader.rb in iostreams-0.9.0

- old
+ new

@@ -1,42 +1,32 @@ -begin - require 'creek' -rescue LoadError => e - puts "Install the 'creek' gem for xlsx streaming support" - raise(e) -end require 'csv' module IOStreams module Xlsx class Reader attr_reader :worksheet - def initialize(workbook) - @worksheet = workbook.sheets[0] - end - - def each_line(&block) - worksheet.rows.each do |row| - block.call(row.values.to_csv(row_sep: nil)) - end - end - - # Read from a xlsx file or stream. + # Read from a xlsx, or xlsm file or stream. # # Example: # IOStreams::Xlsx::Reader.open('spreadsheet.xlsx') do |spreadsheet_stream| # spreadsheet_stream.each_line do |line| # puts line # end # end def self.open(file_name_or_io, options={}, &block) + begin + require 'creek' unless defined?(Creek::Book) + rescue LoadError => e + raise(LoadError, "Please install the 'creek' gem for xlsx streaming support. #{e.message}") + end + options = options.dup buffer_size = options.delete(:buffer_size) || 65536 raise(ArgumentError, "Unknown IOStreams::Xlsx::Reader option: #{options.inspect}") if options.size > 0 - if file_name_or_io.respond_to?(:read) + if IOStreams.reader_stream?(file_name_or_io) temp_file = Tempfile.new('rocket_job_xlsx') file_name = temp_file.to_path ::File.open(file_name, 'wb') do |file| IOStreams.copy(file_name_or_io, file, buffer_size) @@ -47,9 +37,20 @@ block.call(self.new(Creek::Book.new(file_name, check_file_extension: false))) ensure temp_file.delete if temp_file end + + def initialize(workbook) + @worksheet = workbook.sheets[0] + end + + # Returns each [Array] row from the spreadsheet + def each(&block) + worksheet.rows.each { |row| block.call(row.values) } + end + + alias_method :each_line, :each end end end