summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-04-01 17:00:06 -0700
committerRandy Morgan <[email protected]>2012-04-01 17:00:06 -0700
commitbcc7c8c4e85a6ed53f134d633e8f81fd313d6c8d (patch)
tree9a474295ec099e303b3711338962d84cb7a43596
parent225edfc837e99167072fdf15e4ee3a5af9cfe96c (diff)
parent44c77ed27b20c4ebe1c9c8faf6ad80701d7f567e (diff)
downloadcaxlsx-bcc7c8c4e85a6ed53f134d633e8f81fd313d6c8d.tar.gz
caxlsx-bcc7c8c4e85a6ed53f134d633e8f81fd313d6c8d.zip
Merge pull request #74 from joekain/wide
Wide
-rw-r--r--lib/axlsx.rb6
-rw-r--r--test/workbook/worksheet/tc_cell.rb40
2 files changed, 43 insertions, 3 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index dd628562..46d1c9bf 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -57,7 +57,7 @@ module Axlsx
def self.name_to_indices(name)
raise ArgumentError, 'invalid cell name' unless name.size > 1
v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c|
- val[:i] += ((c.bytes.first - 65) + val[:base]); val[:base] *= 26; val
+ val[:i] += ((c.bytes.first - 64) * val[:base]); val[:base] *= 26; val
end
[v[:i]-1, ((name[/[1-9][0-9]*/]).to_i)-1]
@@ -71,9 +71,9 @@ module Axlsx
chars = []
while index >= 26 do
chars << ((index % 26) + 65).chr
- index /= 26
+ index = index / 26 - 1
end
- chars << ((chars.empty? ? index : index-1) + 65).chr
+ chars << (index + 65).chr
chars.reverse.join
end
diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb
index 4db0c7be..c35e62bc 100644
--- a/test/workbook/worksheet/tc_cell.rb
+++ b/test/workbook/worksheet/tc_cell.rb
@@ -13,6 +13,30 @@ class TestCell < Test::Unit::TestCase
@cAA = @ws["AA2"]
end
+ def setup_wide
+ # The wide row makes the test take a long time. Add it only
+ # in test_large as adding it in setup make all the tests take
+ # longer.
+ #
+ # For even more, but slower, testing use the 20000 cell row and
+ # uncomment the ABCD3 element below.
+ data = (0..1000).map { |index| index }
+ #data = (0..20000).map { |index| index }
+ @ws.add_row data
+
+ @wide_test_points = { "A3" => 0,
+ "Z3" => 25,
+ "B3" => 1,
+ "AA3" => 1 * 26 + 0,
+ "AAA3" => 1 * 26**2 + 1 * 26 + 0,
+ "AAZ3" => 1 * 26**2 + 1 * 26 + 25,
+ "ABA3" => 1 * 26**2 + 2 * 26 + 0,
+
+ # For additional testing, uncomment this line and uncomment the 20000 cell row above
+ #"ABCD3" => 1 * 26**3 + 2 * 26**2 + 3 * 26 + 3
+ }
+ end
+
def test_initialize
assert_equal(@row.cells.last, @c, "the cell was added to the row")
assert_equal(@c.type, :float, "type option is applied")
@@ -41,6 +65,22 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.r, "A1", "calculate cell reference")
end
+ def test_wide_index
+ setup_wide
+ @wide_test_points.each_pair do |ref, index|
+ c = @ws[ref]
+ assert_equal(c.index, index, "calculate cell index for cell #{ref}")
+ end
+ end
+
+ def test_wide_r
+ setup_wide
+ @wide_test_points.each_pair do |ref, index|
+ c = @ws[ref]
+ assert_equal(c.r, ref, "calculate cell reference for cell at index #{index}")
+ end
+ end
+
def test_r_abs
assert_equal(@c.r_abs,"$A$1", "calculate absolute cell reference")
assert_equal(@cAA.r_abs,"$AA$2", "needs to accept multi-digit columns")