Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
add test case for file importing and fix a couple of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Hutchens committed Apr 24, 2012
1 parent 0c4a319 commit c3fbdd4
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 21 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ group :development do
gem "rubyXL", "~> 1.2.7"
gem "nokogiri", ">= 1.4.4"
gem "rubyzip", ">= 0.9.4"
# gem "rcov", ">= 0"
end

group :test do
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,20 @@ Soroban has a built-in importer for xlsx files. It requires the [RubyXL](https:/
require 'rubyXL'

BINDINGS = {
:gravity => :B1,
:planet => :B1,
:mass => :B2,
:force => :B10
:force => :B3
}

s = Soroban::Import::rubyXL("/Users/kranzky/Desktop/Physics.xlsx", 0, BINDINGS )
s = Soroban::Import::rubyXL("files/Physics.xlsx", 0, BINDINGS )

s.planet = 'Earth'
s.mass = 80
puts s.force # => 783.459251241996

s.planet = 'Venus'
s.mass = 80
puts s.force # => 710.044826106394
```

This import process returns a new Soroban::Sheet object that contains all the
Expand Down
Binary file added files/Physics.xlsx
Binary file not shown.
2 changes: 2 additions & 0 deletions lib/soroban/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def get
raise Soroban::RecursionError, "Loop detected when evaluating '#{@excel}'" if @touched
@touched = true
eval(@ruby, @binding)
rescue TypeError, RangeError, ZeroDivisionError
nil
ensure
@touched = false
end
Expand Down
11 changes: 7 additions & 4 deletions lib/soroban/functions/vlookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
Soroban::define :VLOOKUP => lambda { |value, range, col, inexact|
fc, fr, tc, tr = Soroban::getRange(range)
i = walk("#{fc}#{fr}:#{fc}#{tr}").find_index(value)
return nil if i.nil?
(0...i).each { fr.next! }
(1...col).each { fc.next! }
eval("@#{fc}#{fr}.get")
if i.nil?
nil
else
(0...i).each { fr.next! }
(1...col).each { fc.next! }
eval("@#{fc}#{fr}.get")
end
}
2 changes: 1 addition & 1 deletion lib/soroban/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.getRange(range)
def self.getPos(label)
# TODO: fix for labels such as "BC42"
match = /^([a-zA-Z]+)([\d]+)$/.match(label.to_s)
return match[2].to_i - 1, match[1].upcase[0]-"A"[0]
return match[2].to_i - 1, match[1].upcase[0].ord-"A"[0].ord
end

# Return an array of values for the supplied arguments (which may be numbers, labels and ranges).
Expand Down
1 change: 0 additions & 1 deletion lib/soroban/import/ruby_xl_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def _addCell(label)
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

Expand Down
12 changes: 9 additions & 3 deletions lib/soroban/label_walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ def initialize(range)

# Yield the label of each cell referenced by the supplied range.
def each
(@fc..@tc).each do |col|
(@fr..@tr).each do |row|
yield "#{col}#{row}"
col, row = @fc, @fr
while true do
yield "#{col}#{row}"
break if row == @tr && col == @tc
if row == @tr
row = @fr
col = col.next
else
row = row.next
end
end
end
Expand Down
9 changes: 1 addition & 8 deletions lib/soroban/parser/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ def rewrite(value)
"'#{value}'"
end
def extract(value)
fc, fr, tc, tr = Soroban::getRange(value)
retval = []
(fc..tc).each do |cc|
(fr..tr).each do |cr|
retval << "#{cc}#{cr}".to_sym
end
end
retval
LabelWalker.new(value).map { |label| "#{label}".to_sym }
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/import_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "Documentation", :if => Gem.available?("rubyXL") do

it "can import xlsx files using RubyXL" do

BINDINGS = {
:planet => :B1,
:mass => :B2,
:force => :B3
}

s = Soroban::Import::rubyXL("files/Physics.xlsx", 0, BINDINGS )

s.planet = 'Earth'
s.mass = 80
puts s.force # => 783.459251241996
s.force.should be_within(0.01).of(783.46)

s.planet = 'Venus'
s.mass = 80
puts s.force # => 710.044826106394
s.force.should be_within(0.01).of(710.04)

end

end

0 comments on commit c3fbdd4

Please sign in to comment.