Sha256: 06a5091dafb8e94b3c133ffb513f9c0538330af520a1048e23e592f7c6f01b66

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Soroban
  module Import

    # Use the RubyXL gem to load an xlsx file, returning a new Soroban::Sheet
    # object. Specify the path to the xlsx file, the index of the sheet to be
    # imported, and a hash of name => label bindings.
    def self.rubyXL(path, sheet, bindings)
      require 'rubyXL'
      require 'soroban/import/ruby_xl_patch'
      RubyXLImporter.new(path, sheet, bindings).import
    end

    private

    class RubyXLImporter

      def initialize(path, index, bindings)
        @path, @index, @bindings = path, index, bindings
      end

      def import
        workbook = RubyXL::Parser.parse(@path)
        @sheet = workbook.worksheets[@index]
        @model = Soroban::Sheet.new
        @bindings.values.each do |label_or_range|
          if Soroban::range?(label_or_range)
            LabelWalker.new(label_or_range).each do |label|
              _addCell(label)
            end
          else
            _addCell(label_or_range)
          end
        end
        while label = @model.missing.first
          _addCell(label)
        end
        @model.bind(@bindings)
        return @model
      end

      private

      def _addCell(label)
        row, col = Soroban::getPos(label)
        cell = @sheet[row][col]
        data = cell.formula rescue nil
        data = "=#{data}" unless data.nil?
        data ||= cell.value.to_s rescue nil
        puts "#{label} => #{row},#{col} = #{data}"
        @model.set(label.to_sym => data)
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soroban-0.2.0 lib/soroban/import/ruby_xl_importer.rb