summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-06-20 19:26:25 +0900
committerRandy Morgan <[email protected]>2012-06-20 19:26:25 +0900
commit3d3d8e3aba0c49fe5adef5cf1058c3157314b2b2 (patch)
treed22cda14d33f15646a54195abb7b709741650a47 /lib
parenteb9e1495c31b74ade95cce3b9e5e4c420d66eb02 (diff)
downloadcaxlsx-3d3d8e3aba0c49fe5adef5cf1058c3157314b2b2.tar.gz
caxlsx-3d3d8e3aba0c49fe5adef5cf1058c3157314b2b2.zip
update examples and the beginings of protected_range
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/workbook/workbook.rb3
-rw-r--r--lib/axlsx/workbook/worksheet/protected_range.rb40
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb26
3 files changed, 64 insertions, 5 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index 08cbbc20..b9a46c06 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -2,6 +2,7 @@
module Axlsx
require 'axlsx/workbook/worksheet/date_time_converter.rb'
+require 'axlsx/workbook/worksheet/protected_range.rb'
require 'axlsx/workbook/worksheet/cell.rb'
require 'axlsx/workbook/worksheet/page_margins.rb'
require 'axlsx/workbook/worksheet/page_setup.rb'
@@ -100,7 +101,6 @@ require 'axlsx/workbook/worksheet/selection.rb'
# @return [SimpleTypedList]
attr_reader :tables
-
# A colllection of comments associated with this workbook
# @note The recommended way to manage comments is Worksheet#add_comment
# @see Worksheet#add_comment
@@ -143,7 +143,6 @@ require 'axlsx/workbook/worksheet/selection.rb'
@drawings = SimpleTypedList.new Drawing
@charts = SimpleTypedList.new Chart
@images = SimpleTypedList.new Pic
-
# Are these even used????? Check package serialization parts
@tables = SimpleTypedList.new Table
@comments = SimpleTypedList.new Comments
diff --git a/lib/axlsx/workbook/worksheet/protected_range.rb b/lib/axlsx/workbook/worksheet/protected_range.rb
new file mode 100644
index 00000000..feb5eb48
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/protected_range.rb
@@ -0,0 +1,40 @@
+module Axlsx
+ # The Protected Range class represents a set of cells in the worksheet
+ # @note the recommended way to manage protected ranges with via Worksheet#protect_range
+ # @see Worksheet#protect_range
+ class ProtectedRange
+
+ # The reference for the protected range
+ # @return [String]
+ attr_reader :sqref
+
+ # The name of the protected range
+ # @return [String]
+ attr_reader :name
+
+ # Initializes a new protected range object
+ # @option [String] sqref The cell range reference to protect. This can be an absolute or a relateve range however, it only applies to the current sheet.
+ # @option [String] name An optional name for the protected name.
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ yield self if block_given?
+ end
+
+ def sqref=(v)
+ Axlsx.validate_string(v)
+ @sqref = v
+ end
+
+ def name=(v)
+ Axlsx.validate_string(v)
+ @name = v
+ end
+
+ def to_xml_string(str="")
+ attrs = self.instance_values.reject{ |key, value| value == nil }
+ str << '<protectedRange ' << attrs.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ') << '/>'
+ end
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 7245b8c2..513f0453 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -17,6 +17,12 @@ module Axlsx
@sheet_protection
end
+ # A collection of protected ranges in the worksheet
+ # @note The recommended way to manage protected ranges is with Worksheet#protect_range
+ # @see Worksheet#protect_range
+ # @return [SimpleTypedList] The protected ranges for this worksheet
+ attr_reader :protected_ranges
+
# The sheet view object for this worksheet
# @return [SheetView]
# @see [SheetView]
@@ -188,7 +194,7 @@ module Axlsx
@print_options = PrintOptions.new options[:print_options] if options[:print_options]
@rows = SimpleTypedList.new Row
@column_info = SimpleTypedList.new Col
- # @cols = SimpleTypedList.new Cell
+ @protected_ranges = SimpleTypedList.new ProtectedRange
@tables = SimpleTypedList.new Table
options.each do |o|
@@ -247,7 +253,21 @@ module Axlsx
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] The string reference for the cells to protect or an array of cells.
+ # @retrun [ProtectedRange]
+ # @note When using an array of cells, a contiguous range is created from the minimum top left to the maximum top bottom of the cells provided.
+ def protect_range(cells)
+ 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}"
+ end
+ @protected_ranges << ProtectedRange.new(:sqref => sqref, :name => 'Range#{@protected_ranges.size}')
+ @protected_ranges.last
+ end
+
# The demensions of a worksheet. This is not actually a required element by the spec,
# but at least a few other document readers expect this for conversion
# @return [String] the A1:B2 style reference for the first and last row column intersection in the workbook
@@ -550,7 +570,7 @@ module Axlsx
# [#xAFFFE-#xAFFFF], [#xBFFFE-#xBFFFF], [#xCFFFE-#xCFFFF],
# [#xDFFFE-#xDFFFF], [#xEFFFE-#xEFFFF], [#xFFFFE-#xFFFFF],
# [#x10FFFE-#x10FFFF].
- # str.tr("", '')
+ str.tr("\u0001-\u0008", '')
end
# The worksheet relationships. This is managed automatically by the worksheet