Class: IOStreams::Xlsx::Reader
- Inherits:
-
Object
- Object
- IOStreams::Xlsx::Reader
- Defined in:
- lib/io_streams/xlsx/reader.rb
Class Method Summary collapse
-
.open(file_name_or_io, _ = nil) ⇒ Object
Convert a xlsx, or xlsm file or stream into CSV format.
Instance Method Summary collapse
-
#each ⇒ Object
Returns each [Array] row from the spreadsheet.
-
#initialize(file_name) ⇒ Reader
constructor
A new instance of Reader.
Constructor Details
#initialize(file_name) ⇒ Reader
Returns a new instance of Reader.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/io_streams/xlsx/reader.rb', line 25 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.}") end workbook = Creek::Book.new(file_name, check_file_extension: false) @worksheet = workbook.sheets[0] end |
Class Method Details
.open(file_name_or_io, _ = nil) ⇒ Object
Convert a xlsx, or xlsm file or stream into CSV format.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/io_streams/xlsx/reader.rb', line 7 def self.open(file_name_or_io, _ = nil) if file_name_or_io.is_a?(String) file_name = file_name_or_io else temp_file = Tempfile.new('iostreams_xlsx') IOStreams.copy(file_name_or_io, temp_file) file_name = temp_file.to_path end csv_temp_file = Tempfile.new('iostreams_csv') new(file_name).each { |lines| csv_temp_file << lines.to_csv } csv_temp_file.rewind yield csv_temp_file ensure temp_file.delete if temp_file csv_temp_file.delete if csv_temp_file end |
Instance Method Details
#each ⇒ Object
Returns each [Array] row from the spreadsheet
37 38 39 |
# File 'lib/io_streams/xlsx/reader.rb', line 37 def each @worksheet.rows.each { |row| yield row.values } end |