summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorWeston Ganger <[email protected]>2021-12-29 12:00:26 -0800
committerWeston Ganger <[email protected]>2021-12-29 13:11:55 -0800
commit6bc6e6303ac27ac11571760c036c244b1448aeaa (patch)
treeb0aa4f90802b49f5fcb6eb7dc53931b9364889f1 /lib
parent7aa995d58f072d2b49166d43ef44232a5086042a (diff)
downloadcaxlsx-6bc6e6303ac27ac11571760c036c244b1448aeaa.tar.gz
caxlsx-6bc6e6303ac27ac11571760c036c244b1448aeaa.zip
Fix Worksheet#name_to_cell bug which returned reversed row/col indexes
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx.rb20
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb18
2 files changed, 34 insertions, 4 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index 0c8c3850..4e804513 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -79,11 +79,25 @@ module Axlsx
# returns the x, y position of a cell
def self.name_to_indices(name)
raise ArgumentError, 'invalid cell name' unless name.size > 1
+
+ letters_str = name[/[A-Z]+/]
+
# capitalization?!?
- v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c|
- val[:i] += ((c.bytes.first - 64) * val[:base]); val[:base] *= 26; val
+ v = letters_str.reverse.chars.reduce({:base=>1, :i=>0}) do |val, c|
+ val[:i] += ((c.bytes.first - 64) * val[:base])
+
+ val[:base] *= 26
+
+ next val
end
- [v[:i]-1, ((name[/[1-9][0-9]*/]).to_i)-1]
+
+ col_index = (v[:i] - 1)
+
+ numbers_str = name[/[1-9][0-9]*/]
+
+ row_index = (numbers_str.to_i - 1)
+
+ return [col_index, row_index]
end
# converts the column index into alphabetical values.
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 06a5e7f1..f1ec815c 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -599,10 +599,20 @@ module Axlsx
# @return [Cell, Array]
def [](cell_def)
return rows[cell_def] if cell_def.is_a?(Integer)
+
parts = cell_def.split(':').map{ |part| name_to_cell part }
+
if parts.size == 1
parts.first
else
+ if parts.size > 2
+ raise ArgumentError.new("Invalid cell definition `#{cell_def}`")
+ elsif parts.first.nil?
+ raise ArgumentError.new("Missing cell `#{cell_def.split(":").first}` for the specified range `#{cell_def}`")
+ elsif parts.last.nil?
+ raise ArgumentError.new("Missing cell `#{cell_def.split(":").last}` for the specified range `#{cell_def}`")
+ end
+
range(*parts)
end
end
@@ -612,8 +622,12 @@ module Axlsx
# @return [Cell]
def name_to_cell(name)
col_index, row_index = *Axlsx::name_to_indices(name)
+
r = rows[row_index]
- r[col_index] if r
+
+ if r
+ return r[col_index]
+ end
end
# shortcut method to access styles direclty from the worksheet
@@ -688,11 +702,13 @@ module Axlsx
def range(*cell_def)
first, last = cell_def
cells = []
+
rows[(first.row.row_index..last.row.row_index)].each do |r|
r[(first.index..last.index)].each do |c|
cells << c
end
end
+
cells
end