diff options
| author | Randy Morgan <[email protected]> | 2012-10-14 12:25:09 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-14 12:25:09 +0900 |
| commit | 93b70a39999ac4d06e43e495f3fd283e9630d9d2 (patch) | |
| tree | bb2846edfc328ff9143c548c2a25eab23b8c9179 /lib/axlsx/workbook/worksheet/data_validation.rb | |
| parent | 5b5410845447772f4ba01b2ee5d03907f5897e7a (diff) | |
| download | caxlsx-93b70a39999ac4d06e43e495f3fd283e9630d9d2.tar.gz caxlsx-93b70a39999ac4d06e43e495f3fd283e9630d9d2.zip | |
Refactored to use options parser and serialized attributes.
Diffstat (limited to 'lib/axlsx/workbook/worksheet/data_validation.rb')
| -rw-r--r-- | lib/axlsx/workbook/worksheet/data_validation.rb | 123 |
1 files changed, 61 insertions, 62 deletions
diff --git a/lib/axlsx/workbook/worksheet/data_validation.rb b/lib/axlsx/workbook/worksheet/data_validation.rb index e790bc77..92a42a11 100644 --- a/lib/axlsx/workbook/worksheet/data_validation.rb +++ b/lib/axlsx/workbook/worksheet/data_validation.rb @@ -5,24 +5,50 @@ module Axlsx # @note The recommended way to manage data validations is via Worksheet#add_data_validation # @see Worksheet#add_data_validation class DataValidation - + include Axlsx::OptionsParser + + # Creates a new {DataValidation} object + # @option options [String] formula1 + # @option options [String] formula2 + # @option options [Boolean] allowBlank - A boolean value indicating whether the data validation allows the use of empty or blank entries. + # @option options [String] error - Message text of error alert. + # @option options [Symbol] errorStyle - The style of error alert used for this data validation. + # @option options [String] errorTitle - itle bar text of error alert. + # @option options [Symbol] operator - The relational operator used with this data validation. + # @option options [String] prompt - Message text of input prompt. + # @option options [String] promptTitle - Title bar text of input prompt. + # @option options [Boolean] showDropDown - A boolean value indicating whether to display a dropdown combo box for a list type data validation + # @option options [Boolean] showErrorMessage - A boolean value indicating whether to display the error alert message when an invalid value has been entered, according to the criteria specified. + # @option options [Boolean] showInputMessage - A boolean value indicating whether to display the input prompt message. + # @option options [String] sqref - Range over which data validation is applied, in "A1:B2" format. + # @option options [Symbol] type - The type of data validation. + def initialize(options={}) + # defaults + @formula1 = @formula2 = @error = @errorTitle = @operator = @prompt = @promptTitle = @sqref = nil + @allowBlank = @showErrorMessage = true + @showDropDown = @showInputMessage = false + @type = :none + @errorStyle = :stop + parse_options options + end + # instance values that must be serialized as their own elements - e.g. not attributes. CHILD_ELEMENTS = [:formula1, :formula2] - + # Formula1 # Available for type whole, decimal, date, time, textLength, list, custom # @see type # @return [String] # default nil attr_reader :formula1 - + # Formula2 # Available for type whole, decimal, date, time, textLength # @see type # @return [String] # default nil attr_reader :formula2 - + # Allow Blank # A boolean value indicating whether the data validation allows the use of empty or blank # entries. 1 means empty entries are OK and do not violate the validation constraints. @@ -31,7 +57,7 @@ module Axlsx # @return [Boolean] # default true attr_reader :allowBlank - + # Error Message # Message text of error alert. # Available for type whole, decimal, date, time, textLength, list, custom @@ -39,7 +65,7 @@ module Axlsx # @return [String] # default nil attr_reader :error - + # Error Style (ST_DataValidationErrorStyle) # The style of error alert used for this data validation. # Options are: @@ -51,7 +77,7 @@ module Axlsx # @return [Symbol] # default :stop attr_reader :errorStyle - + # Error Title # Title bar text of error alert. # Available for type whole, decimal, date, time, textLength, list, custom @@ -59,7 +85,7 @@ module Axlsx # @return [String] # default nil attr_reader :errorTitle - + # Operator (ST_DataValidationOperator) # The relational operator used with this data validation. # Options are: @@ -76,7 +102,7 @@ module Axlsx # @return [Symbol] # default nil attr_reader :operator - + # Input prompt # Message text of input prompt. # Available for type whole, decimal, date, time, textLength, list, custom @@ -84,7 +110,7 @@ module Axlsx # @return [String] # default nil attr_reader :prompt - + # Prompt title # Title bar text of input prompt. # Available for type whole, decimal, date, time, textLength, list, custom @@ -92,7 +118,7 @@ module Axlsx # @return [String] # default nil attr_reader :promptTitle - + # Show drop down # A boolean value indicating whether to display a dropdown combo box for a list type data # validation. Be careful: false shows the dropdown list! @@ -101,7 +127,7 @@ module Axlsx # @return [Boolean] # default false attr_reader :showDropDown - + # Show error message # A boolean value indicating whether to display the error alert message when an invalid # value has been entered, according to the criteria specified. @@ -110,7 +136,7 @@ module Axlsx # @return [Boolean] # default false attr_reader :showErrorMessage - + # Show input message # A boolean value indicating whether to display the input prompt message. # Available for type whole, decimal, date, time, textLength, list, custom @@ -118,14 +144,14 @@ module Axlsx # @return [Boolean] # default false attr_reader :showInputMessage - + # Range over which data validation is applied, in "A1:B2" format # Available for type whole, decimal, date, time, textLength, list, custom # @see type # @return [String] # default nil attr_reader :sqref - + # The type (ST_DataValidationType) of data validation. # Options are: # * custom: Data validation which uses a custom formula to check the cell value. @@ -139,83 +165,56 @@ module Axlsx # @return [Symbol] # default none attr_reader :type - - # Creates a new {DataValidation} object - # @option options [String] formula1 - # @option options [String] formula2 - # @option options [Boolean] allowBlank - A boolean value indicating whether the data validation allows the use of empty or blank entries. - # @option options [String] error - Message text of error alert. - # @option options [Symbol] errorStyle - The style of error alert used for this data validation. - # @option options [String] errorTitle - itle bar text of error alert. - # @option options [Symbol] operator - The relational operator used with this data validation. - # @option options [String] prompt - Message text of input prompt. - # @option options [String] promptTitle - Title bar text of input prompt. - # @option options [Boolean] showDropDown - A boolean value indicating whether to display a dropdown combo box for a list type data validation - # @option options [Boolean] showErrorMessage - A boolean value indicating whether to display the error alert message when an invalid value has been entered, according to the criteria specified. - # @option options [Boolean] showInputMessage - A boolean value indicating whether to display the input prompt message. - # @option options [String] sqref - Range over which data validation is applied, in "A1:B2" format. - # @option options [Symbol] type - The type of data validation. - def initialize(options={}) - # defaults - @formula1 = @formula2 = @error = @errorTitle = @operator = @prompt = @promptTitle = @sqref = nil - @allowBlank = @showErrorMessage = true - @showDropDown = @showInputMessage = false - @type = :none - @errorStyle = :stop - - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - + + # @see formula1 def formula1=(v); Axlsx::validate_string(v); @formula1 = v end - + # @see formula2 def formula2=(v); Axlsx::validate_string(v); @formula2 = v end - + # @see allowBlank def allowBlank=(v); Axlsx::validate_boolean(v); @allowBlank = v end - + # @see error def error=(v); Axlsx::validate_string(v); @error = v end - + # @see errorStyle def errorStyle=(v); Axlsx::validate_data_validation_error_style(v); @errorStyle = v end - + # @see errorTitle def errorTitle=(v); Axlsx::validate_string(v); @errorTitle = v end - + # @see operator def operator=(v); Axlsx::validate_data_validation_operator(v); @operator = v end - + # @see prompt def prompt=(v); Axlsx::validate_string(v); @prompt = v end - + # @see promptTitle def promptTitle=(v); Axlsx::validate_string(v); @promptTitle = v end - + # @see showDropDown def showDropDown=(v); Axlsx::validate_boolean(v); @showDropDown = v end - + # @see showErrorMessage def showErrorMessage=(v); Axlsx::validate_boolean(v); @showErrorMessage = v end - + # @see showInputMessage def showInputMessage=(v); Axlsx::validate_boolean(v); @showInputMessage = v end - + # @see sqref def sqref=(v); Axlsx::validate_string(v); @sqref = v end - + # @see type def type=(v); Axlsx::validate_data_validation_type(v); @type = v end - + # Serializes the data validation # @param [String] str # @return [String] def to_xml_string(str = '') valid_attributes = get_valid_attributes - + str << '<dataValidation ' str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' if (valid_attributes.include?(key.to_sym) and not CHILD_ELEMENTS.include?(key.to_sym)) }.join(' ') str << '>' @@ -223,11 +222,11 @@ module Axlsx str << '<formula2>' << self.formula2 << '</formula2>' if @formula2 and valid_attributes.include?(:formula2) str << '</dataValidation>' end - + private def get_valid_attributes attributes = [:allowBlank, :error, :errorStyle, :errorTitle, :prompt, :promptTitle, :showErrorMessage, :showInputMessage, :sqref, :type ] - + if [:whole, :decimal, :data, :time, :textLength].include?(@type) attributes << [:operator, :formula1] attributes << [:formula2] if [:between, :notBetween].include?(@operator) @@ -238,7 +237,7 @@ module Axlsx else attributes = [] end - + attributes.flatten! end end |
