summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-07-15 13:54:12 +0900
committerRandy Morgan <[email protected]>2012-07-15 13:54:12 +0900
commit2b6c89d6410df098c6319649f871d0ed8b950132 (patch)
tree23773199aef3827a4e66808a0567642ec97ec394
parent14beaaac13637f2153faa3b851f3e849974df556 (diff)
downloadcaxlsx-2b6c89d6410df098c6319649f871d0ed8b950132.tar.gz
caxlsx-2b6c89d6410df098c6319649f871d0ed8b950132.zip
refactor cell_range helper
This lets us specify if the range should be returned relative or absolute and ensures that cells are sorted prior to parsing.
-rw-r--r--lib/axlsx.rb16
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb7
2 files changed, 13 insertions, 10 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index b3ba0b52..b14ce35b 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -39,12 +39,18 @@ end
# xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine.
module Axlsx
+
# determines the cell range for the items provided
- def self.cell_range(items)
- return "" unless items.first.is_a? Cell
- ref = "'#{items.first.row.worksheet.name}'!" +
- "#{items.first.r_abs}"
- ref += ":#{items.last.r_abs}" if items.size > 1
+ def self.cell_range(cells, absolute=true)
+ return "" unless cells.first.is_a? Cell
+ cells.sort { |x, y| [x.index, x.row.index] <=> [y.index, y.row.index] }
+ reference_method = absolute ? :r_abs : :r
+ ref = if absolute
+ "'#{cells.first.row.worksheet.name}'!#{cells.first.send(reference_method)}"
+ else
+ "#{cells.first.send(reference_method)}"
+ end
+ ref << ":#{cells.last.send(reference_method)}" if cells.size > 1
ref
end
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 41b50a4a..6dbc36ff 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -250,11 +250,9 @@ module Axlsx
@merged_cells << if cells.is_a?(String)
cells
elsif cells.is_a?(Array)
- cells = cells.sort { |x, y| [x.index, x.row.index] <=> [y.index, y.row.index] }
- "#{cells.first.r}:#{cells.last.r}"
+ Axlsx::cell_range(cells, false)
end
end
-
# Adds a new protected cell range to the worksheet. Note that protected ranges are only in effect when sheet protection is enabled.
# @param [String|Array] cells The string reference for the cells to protect or an array of cells.
# @return [ProtectedRange]
@@ -263,8 +261,7 @@ module Axlsx
sqref = if cells.is_a?(String)
cells
elsif cells.is_a?(SimpleTypedList)
- cells = cells.sort { |x, y| [x.index, x.row.index] <=> [y.index, y.row.index] }
- "#{cells.first.r}:#{cells.last.r}"
+ Axlsx::cell_range(cells, false)
end
@protected_ranges << ProtectedRange.new(:sqref => sqref, :name => 'Range#{@protected_ranges.size}')
@protected_ranges.last