Sha256: acd0b0383f8b9076a81d8cf599ad7d63ade0b5f31d5524d1084b6f0da1758de8

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

class RemoteTable
  class Format
    module ProcessedByRoo
      def each(&blk)
        require 'iconv'
        require 'roo'

        spreadsheet = roo_class.new t.local_file.path, nil, :ignore
        if t.config.sheet
          spreadsheet.default_sheet = t.config.sheet
        end
        
        first_row = if t.config.crop
          t.config.crop.first + 1
        else
          t.config.skip + 1
        end
          
        last_row = if t.config.crop
          t.config.crop.last
        else
          spreadsheet.last_row
        end
        
        if t.config.output_class == ::Array
          (first_row..last_row).each do |y|
            output = (1..spreadsheet.last_column).map do |x|
              assume_utf8 spreadsheet.cell(y, x).to_s.gsub(/<[^>]+>/, '').strip
            end
            yield output if t.config.keep_blank_rows or output.any? { |v| v.present? }
          end
        else
          headers = ::ActiveSupport::OrderedHash.new
          if t.config.use_first_row_as_header?
            (1..spreadsheet.last_column).each do |x|
              v = spreadsheet.cell(first_row, x)
              v = spreadsheet.cell(first_row - 1, x) if v.blank? # look up
              if v.present?
                v = assume_utf8 v
                headers[v] = x # 'foobar' is found at column 6
              end
            end
            # "advance the cursor"
            first_row += 1
          else
            t.config.headers.each_with_index do |k, i|
              headers[k] = i + 1
            end
          end
          (first_row..last_row).each do |y|
            output = ::ActiveSupport::OrderedHash.new
            headers.each do |k, x|
              output[k] = assume_utf8 spreadsheet.cell(y, x).to_s.gsub(/<[^>]+>/, '').strip
            end
            yield output if t.config.keep_blank_rows or output.any? { |k, v| v.present? }
          end
        end
      ensure
        t.local_file.cleanup
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remote_table-1.4.0 lib/remote_table/format/mixins/processed_by_roo.rb