Sha256: ef870710ed5328aa05a0b7b356c832d7175c96b7e593fb643b115ddab10ad53f

Contents?: true

Size: 1020 Bytes

Versions: 20

Compression:

Stored size: 1020 Bytes

Contents

require "csv"

module IOStreams
  module Xlsx
    class Reader < IOStreams::Reader
      # Convert a xlsx, or xlsm file into CSV format.
      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

      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

      # Returns each [Array] row from the spreadsheet
      def each
        @worksheet.rows.each { |row| yield row.values }
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
iostreams-1.10.3 lib/io_streams/xlsx/reader.rb
iostreams-1.10.2 lib/io_streams/xlsx/reader.rb
iostreams-1.10.1 lib/io_streams/xlsx/reader.rb
iostreams-1.10.0 lib/io_streams/xlsx/reader.rb
iostreams-1.9.0 lib/io_streams/xlsx/reader.rb
iostreams-1.8.0 lib/io_streams/xlsx/reader.rb
iostreams-1.7.0 lib/io_streams/xlsx/reader.rb
iostreams-1.6.2 lib/io_streams/xlsx/reader.rb
iostreams-1.6.1 lib/io_streams/xlsx/reader.rb
iostreams-1.6.0 lib/io_streams/xlsx/reader.rb
iostreams-1.5.1 lib/io_streams/xlsx/reader.rb
iostreams-1.5.0 lib/io_streams/xlsx/reader.rb
iostreams-1.4.0 lib/io_streams/xlsx/reader.rb
iostreams-1.3.3 lib/io_streams/xlsx/reader.rb
iostreams-1.3.2 lib/io_streams/xlsx/reader.rb
iostreams-1.3.1 lib/io_streams/xlsx/reader.rb
iostreams-1.3.0 lib/io_streams/xlsx/reader.rb
iostreams-1.2.1 lib/io_streams/xlsx/reader.rb
iostreams-1.2.0 lib/io_streams/xlsx/reader.rb
iostreams-1.1.1 lib/io_streams/xlsx/reader.rb