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 | |
| parent | 5b5410845447772f4ba01b2ee5d03907f5897e7a (diff) | |
| download | caxlsx-93b70a39999ac4d06e43e495f3fd283e9630d9d2.tar.gz caxlsx-93b70a39999ac4d06e43e495f3fd283e9630d9d2.zip | |
Refactored to use options parser and serialized attributes.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb | 30 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/auto_filter/filters.rb | 47 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 77 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/col.rb | 7 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/comment.rb | 25 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/conditional_formatting.rb | 22 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/data_validation.rb | 123 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/dimension.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/page_margins.rb | 40 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/page_set_up_pr.rb | 15 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/page_setup.rb | 39 |
11 files changed, 205 insertions, 221 deletions
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb index 34ee2507..71f08a2f 100644 --- a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +++ b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb @@ -5,8 +5,8 @@ module Axlsx # then there is no corresponding filterColumn collection expressed for that column. class FilterColumn - # Allowed filters - FILTERS = [:filters] #, :top10, :custom_filters, :dynamic_filters, :color_filters, :icon_filters] + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes # Creates a new FilterColumn object # @note This class yeilds its filter object as that is where the vast majority of processing will be done @@ -19,17 +19,20 @@ module Axlsx RestrictionValidator.validate 'FilterColumn.filter', FILTERS, filter_type #Axlsx::validate_unsigned_int(col_id) self.col_id = col_id - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options @filter = Axlsx.const_get(Axlsx.camel(filter_type)).new(options) yield @filter if block_given? end + serializable_attributes :col_id, :hidden_button, :show_button + + # Allowed filters + FILTERS = [:filters] #, :top10, :custom_filters, :dynamic_filters, :color_filters, :icon_filters] + # Zero-based index indicating the AutoFilter column to which this filter information applies. # @return [Integer] attr_reader :col_id - + # The actual filter being dealt with here # This could be any one of the allowed filter types attr_reader :filter @@ -42,7 +45,7 @@ module Axlsx @show_button ||= true end - # Flag indicating whether the AutoFilter button for this column is hidden. + # Flag indicating whether the AutoFilter button for this column is hidden. # @return [Boolean] def hidden_button @hidden_button ||= false @@ -62,7 +65,7 @@ module Axlsx # @param [Array] row A row from a worksheet that needs to be # filtered. def apply(row, offset) - row.hidden = @filter.apply(row.cells[offset+col_id.to_i]) + row.hidden = @filter.apply(row.cells[offset+col_id.to_i]) end # @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden. # @return [Boolean] @@ -87,16 +90,5 @@ module Axlsx @filter.to_xml_string(str) str << "</filterColumn>" end - - private - - def serialized_attributes(str='') - instance_values.each do |key, value| - if %(show_button hidden_button col_id).include? key.to_s - str << "#{Axlsx.camel(key, false)}='#{value}' " - end - end - str - end end end diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb index 76ccf0de..f26a8442 100644 --- a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +++ b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb @@ -3,9 +3,8 @@ module Axlsx # When multiple values are chosen to filter by, or when a group of date values are chosen to filter by, # this object groups those criteria together. class Filters - - # Allowed calendar types - CALENDAR_TYPES = %w(gregorian gregorianUs gregorianMeFrench gregorianArabic hijri hebrew taiwan japan thai korea saka gregorianXlitEnglish gregorianXlitFrench none) + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes # Creates a new Filters object # @param [Hash] options Options used to set this objects attributes and @@ -18,11 +17,14 @@ module Axlsx # @example # ws.auto_filter.add_column(0, :filters, :blank => true, :calendar_type => 'japan', :filter_items => [100, 'a']) def initialize(options={}) - options.each do |key, value| - self.send("#{key}=", value) if self.respond_to? "#{key}=" - end + parse_options options end + serializable_attributes :blank, :calendar_type + + # Allowed calendar types + CALENDAR_TYPES = %w(gregorian gregorianUs gregorianMeFrench gregorianArabic hijri hebrew taiwan japan thai korea saka gregorianXlitEnglish gregorianXlitFrench none) + # Flag indicating whether to filter by blank. # @return [Boolean] attr_reader :blank @@ -45,7 +47,7 @@ module Axlsx end true end - + # The filter values in this filters object def filter_items @filter_items ||= [] @@ -100,17 +102,6 @@ module Axlsx end end - private - - def serialized_attributes(str='') - instance_values.each do |key, value| - if %(blank claendar_type).include? key.to_s - str << "#{Axlsx.camel(key, false)}='#{value}' " - end - end - str - end - # This class expresses a filter criteria value. class Filter @@ -140,9 +131,8 @@ module Axlsx # subsequent dates, even when formatted or represented by other calendar # types, can be correctly compared for the purposes of filtering. class DateGroupItem - - # Allowed date time groupings - DATE_TIME_GROUPING = %w(year month day hour minute second) + include Axlsx::OptionsParser +include Axlsx::SerializedAttributes # Creates a new DateGroupItem # @param [Hash] options A hash of options to use when @@ -158,11 +148,14 @@ module Axlsx def initialize(options={}) raise ArgumentError, "You must specify a year for date time grouping" unless options[:year] raise ArgumentError, "You must specify a date_time_grouping when creating a DateGroupItem for auto filter" unless options[:date_time_grouping] - options.each do |key, value| - self.send("#{key}=", value) if self.respond_to?("#{key}=") - end + parse_options options end + serializable_attributes :date_time_grouping, :year, :month, :day, :hour, :minute, :second + + # Allowed date time groupings + DATE_TIME_GROUPING = %w(year month day hour minute second) + # Grouping level # This must be one of year, month, day, hour, minute or second. # @return [String] @@ -224,8 +217,8 @@ module Axlsx # The minute value for the date group item # This must be between 0 and 59 def minute=(value) - RangeValidator.validate "DateGroupItem.minute", 0, 59, value - @minute = value + RangeValidator.validate "DateGroupItem.minute", 0, 59, value + @minute = value end # The second value for the date group item @@ -245,7 +238,7 @@ module Axlsx # @param [String] str The string object this serialization will be concatenated to. def to_xml_string(str = '') str << '<dateGroupItem ' - instance_values.each { |key, value| str << "#{key}='#{value.to_s}' " } + serialized_attributes str str << '/>' end end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 621a51f6..86e61d9d 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -8,6 +8,38 @@ module Axlsx # @see Worksheet#add_row class Cell + include Axlsx::OptionsParser + + # @param [Row] row The row this cell belongs to. + # @param [Any] value The value associated with this cell. + # @option options [Symbol] type The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided. + # @option options [Integer] style The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied. + # @option options [String] font_name + # @option options [Integer] charset + # @option options [String] family + # @option options [Boolean] b + # @option options [Boolean] i + # @option options [Boolean] strike + # @option options [Boolean] outline + # @option options [Boolean] shadow + # @option options [Boolean] condense + # @option options [Boolean] extend + # @option options [Boolean] u + # @option options [Symbol] vertAlign must be one of :baseline, :subscript, :superscript + # @option options [Integer] sz + # @option options [String] color an 8 letter rgb specification + # @option options [Symbol] scheme must be one of :none, major, :minor + def initialize(row, value="", options={}) + self.row=row + @value = @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil + @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil + @styles = row.worksheet.workbook.styles + @row.cells << self + parse_options options + @style ||= 0 + @type ||= cell_type_from_value(value) + @value = cast_value(value) + end # An array of available inline styes. # TODO change this to a hash where each key defines attr name and validator (and any info the validator requires) @@ -16,9 +48,9 @@ module Axlsx # set_attr method that kicks the suplied validator and updates the instance_variable # for the key INLINE_STYLES = ['value', 'type', 'font_name', 'charset', - 'family', 'b', 'i', 'strike','outline', - 'shadow', 'condense', 'extend', 'u', - 'vertAlign', 'sz', 'color', 'scheme'] + 'family', 'b', 'i', 'strike','outline', + 'shadow', 'condense', 'extend', 'u', + 'vertAlign', 'sz', 'color', 'scheme'] # The index of the cellXfs item to be applied to this cell. # @return [Integer] @@ -174,39 +206,6 @@ module Axlsx set_run_style nil, :scheme, v end - # @param [Row] row The row this cell belongs to. - # @param [Any] value The value associated with this cell. - # @option options [Symbol] type The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided. - # @option options [Integer] style The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied. - # @option options [String] font_name - # @option options [Integer] charset - # @option options [String] family - # @option options [Boolean] b - # @option options [Boolean] i - # @option options [Boolean] strike - # @option options [Boolean] outline - # @option options [Boolean] shadow - # @option options [Boolean] condense - # @option options [Boolean] extend - # @option options [Boolean] u - # @option options [Symbol] vertAlign must be one of :baseline, :subscript, :superscript - # @option options [Integer] sz - # @option options [String] color an 8 letter rgb specification - # @option options [Symbol] scheme must be one of :none, major, :minor - def initialize(row, value="", options={}) - self.row=row - @value = @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil - @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil - @styles = row.worksheet.workbook.styles - @row.cells << self - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - @style ||= 0 - @type ||= cell_type_from_value(value) - @value = cast_value(value) - end - # The Shared Strings Table index for this cell # @return [Integer] attr_reader :ssti @@ -346,9 +345,9 @@ module Axlsx # the cell itself. Yes, it is a bit of a hack, but it is much better than using # imagemagick and loading metrics for every character. def font_size - font = @styles.fonts[@styles.cellXfs[style].fontId] || @styles.fonts[0] - size_from_styles = (font.b || b) ? font.sz * 1.5 : font.sz - sz || size_from_styles + font = @styles.fonts[@styles.cellXfs[style].fontId] || @styles.fonts[0] + size_from_styles = (font.b || b) ? font.sz * 1.5 : font.sz + sz || size_from_styles end # Utility method for setting inline style attributes diff --git a/lib/axlsx/workbook/worksheet/col.rb b/lib/axlsx/workbook/worksheet/col.rb index 6c9271a2..596531ab 100644 --- a/lib/axlsx/workbook/worksheet/col.rb +++ b/lib/axlsx/workbook/worksheet/col.rb @@ -4,6 +4,7 @@ module Axlsx # The Col class defines column attributes for columns in sheets. class Col + include Axlsx::OptionsParser include Axlsx::SerializedAttributes # Create a new Col objects # @param min First column affected by this 'column info' record. @@ -19,12 +20,11 @@ module Axlsx Axlsx.validate_unsigned_int(min) @min = min @max = max - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end serializable_attributes :collapsed, :hidden, :outline_level, :phonetic, :style, :width, :min, :max + # First column affected by this 'column info' record. # @return [Integer] attr_reader :min @@ -107,7 +107,6 @@ module Axlsx @width = v end - # updates the width for this col based on the cells autowidth and # an optionally specified fixed width # @param [Cell] cell The cell to use in updating this col's width diff --git a/lib/axlsx/workbook/worksheet/comment.rb b/lib/axlsx/workbook/worksheet/comment.rb index bd7a88aa..3e54d2b3 100644 --- a/lib/axlsx/workbook/worksheet/comment.rb +++ b/lib/axlsx/workbook/worksheet/comment.rb @@ -3,6 +3,20 @@ module Axlsx # A comment is the text data for a comment class Comment + include Axlsx::OptionsParser + + # Creates a new comment object + # @param [Comments] comments + # @param [Hash] options + # @option [String] author the author of the comment + # @option [String] text The text for the comment + def initialize(comments, options={}) + raise ArgumentError, "A comment needs a parent comments object" unless comments.is_a?(Comments) + @comments = comments + parse_options options + yield self if block_given? + end + # The text to render # @return [String] attr_reader :text @@ -26,22 +40,12 @@ module Axlsx # rPh (Phonetic Text Run) # phoneticPr (Phonetic Properties) - def initialize(comments, options={}) - raise ArgumentError, "A comment needs a parent comments object" unless comments.is_a?(Comments) - @comments = comments - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - yield self if block_given? - end - # The vml shape that will render this comment # @return [VmlShape] def vml_shape @vml_shape ||= initialize_vml_shape end - # # The index of this author in a unique sorted list of all authors in # the comment. # @return [Integer] @@ -96,6 +100,5 @@ module Axlsx vml.bottom_row = vml.row + 4 end end - end end diff --git a/lib/axlsx/workbook/worksheet/conditional_formatting.rb b/lib/axlsx/workbook/worksheet/conditional_formatting.rb index 7ca07be6..eabf570e 100644 --- a/lib/axlsx/workbook/worksheet/conditional_formatting.rb +++ b/lib/axlsx/workbook/worksheet/conditional_formatting.rb @@ -6,6 +6,16 @@ module Axlsx # @see ConditionalFormattingRule class ConditionalFormatting + include Axlsx::OptionsParser + + # Creates a new {ConditionalFormatting} object + # @option options [Array] rules The rules to apply + # @option options [String] sqref The range to apply the rules to + def initialize(options={}) + @rules = [] + parse_options options + end + # Range over which the formatting is applied, in "A1:B2" format # @return [String] attr_reader :sqref @@ -18,17 +28,7 @@ module Axlsx # @return [Array] attr_reader :rules - # Creates a new {ConditionalFormatting} object - # @option options [Array] rules The rules to apply - # @option options [String] sqref The range to apply the rules to - def initialize(options={}) - @rules = [] - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - - # Add Conditional Formatting Rules to this object. Rules can either + # Add Conditional Formatting Rules to this object. Rules can either # be already created {ConditionalFormattingRule} elements or # hashes of options for automatic creation. If rules is a hash # instead of an array, assume only one rule being added. 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 diff --git a/lib/axlsx/workbook/worksheet/dimension.rb b/lib/axlsx/workbook/worksheet/dimension.rb index 4314b909..552fc001 100644 --- a/lib/axlsx/workbook/worksheet/dimension.rb +++ b/lib/axlsx/workbook/worksheet/dimension.rb @@ -17,7 +17,6 @@ module Axlsx @@default_last ||= 'AA200' end - # Creates a new dimension object # @param[Worksheet] worksheet - the worksheet this dimension applies # to. diff --git a/lib/axlsx/workbook/worksheet/page_margins.rb b/lib/axlsx/workbook/worksheet/page_margins.rb index d2349f9e..6b1b3a7b 100644 --- a/lib/axlsx/workbook/worksheet/page_margins.rb +++ b/lib/axlsx/workbook/worksheet/page_margins.rb @@ -10,6 +10,26 @@ module Axlsx # @see Worksheet#initialize class PageMargins + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # Creates a new PageMargins object + # @option options [Numeric] left The left margin in inches + # @option options [Numeric] right The right margin in inches + # @option options [Numeric] bottom The bottom margin in inches + # @option options [Numeric] top The top margin in inches + # @option options [Numeric] header The header margin in inches + # @option options [Numeric] footer The footer margin in inches + def initialize(options={}) + # Default values taken from MS Excel for Mac 2011 + @left = @right = DEFAULT_LEFT_RIGHT + @top = @bottom = DEFAULT_TOP_BOTTOM + @header = @footer = DEFAULT_HEADER_FOOTER + parse_options options + end + + serializable_attributes :left, :right, :bottom, :top, :header, :footer + # Default left and right margin (in inches) DEFAULT_LEFT_RIGHT = 0.75 @@ -43,24 +63,6 @@ module Axlsx # @return [Float] attr_reader :footer - # Creates a new PageMargins object - # @option options [Numeric] left The left margin in inches - # @option options [Numeric] right The right margin in inches - # @option options [Numeric] bottom The bottom margin in inches - # @option options [Numeric] top The top margin in inches - # @option options [Numeric] header The header margin in inches - # @option options [Numeric] footer The footer margin in inches - def initialize(options={}) - # Default values taken from MS Excel for Mac 2011 - @left = @right = DEFAULT_LEFT_RIGHT - @top = @bottom = DEFAULT_TOP_BOTTOM - @header = @footer = DEFAULT_HEADER_FOOTER - - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - # Set some or all margins at once. # @param [Hash] margins the margins to set (possible keys are :left, :right, :top, :bottom, :header and :footer). def set(margins) @@ -90,7 +92,7 @@ module Axlsx # @see #custom_margins_specified? def to_xml_string(str = '') str << '<pageMargins ' - str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str str << '/>' end end diff --git a/lib/axlsx/workbook/worksheet/page_set_up_pr.rb b/lib/axlsx/workbook/worksheet/page_set_up_pr.rb index 52ff0c7d..f52156c7 100644 --- a/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +++ b/lib/axlsx/workbook/worksheet/page_set_up_pr.rb @@ -4,16 +4,19 @@ module Axlsx # This class name is not a typo, its spec. class PageSetUpPr + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + # creates a new page setup properties object # @param [Hash] options # @option [Boolean] auto_page_breaks Flag indicating whether the sheet displays Automatic Page Breaks. # @option [Boolean] fit_to_page Flag indicating whether the Fit to Page print option is enabled. def initialize(options = {}) - options.each do |key, value| - self.send("#{key}=",value) if self.respond_to?("#{key}=") - end + parse_options options end + serializable_attributes :auto_page_breaks, :fit_to_page + attr_reader :auto_page_breaks attr_reader :fit_to_page @@ -37,11 +40,5 @@ module Axlsx def to_xml_string(str='') str << '<pageSetUpPr ' << serialized_attributes << '/>' end - - private - - def serialized_attributes - instance_values.map { |key, value| "#{Axlsx.camel(key, false)}='#{value}'" }.join(' ') - end end end diff --git a/lib/axlsx/workbook/worksheet/page_setup.rb b/lib/axlsx/workbook/worksheet/page_setup.rb index b3e1051e..ec76cd61 100644 --- a/lib/axlsx/workbook/worksheet/page_setup.rb +++ b/lib/axlsx/workbook/worksheet/page_setup.rb @@ -6,8 +6,24 @@ module Axlsx # @see Worksheet#initialize class PageSetup + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # Creates a new PageSetup object + # @option options [Integer] fit_to_height Number of vertical pages to fit on + # @option options [Integer] fit_to_width Number of horizontal pages to fit on + # @option options [Symbol] orientation Orientation of the page (:default, :landscape, :portrait) + # @option options [String] paper_height Height of paper (number followed by unit identifier: "297mm", "11in") + # @option options [String] paper_width Width of paper (number followed by unit identifier: "210mm", "8.5in") + # @option options [Integer] scale Print scaling (percent value, integer ranging from 10 to 400) + # @option options [Integer] paper_size - the size of paper to use + def initialize(options = {}) + parse_options options + end + + serializable_attributes :fit_to_height, :fit_to_width, :orientation, :paper_height, :paper_width, :scale, :paper_size + # TODO: Attributes defined by Open XML spec that are not implemented yet: - # # * blackAndWhite # * cellComments # * copies @@ -16,7 +32,6 @@ module Axlsx # * firstPageNumber # * horizontalDpi # * pageOrder - # * paperSize # * useFirstPageNumber # * usePrinterDefaults # * verticalDpi @@ -176,24 +191,10 @@ module Axlsx @paper_size = size end - # Creates a new PageSetup object - # @option options [Integer] fit_to_height Number of vertical pages to fit on - # @option options [Integer] fit_to_width Number of horizontal pages to fit on - # @option options [Symbol] orientation Orientation of the page (:default, :landscape, :portrait) - # @option options [String] paper_height Height of paper (number followed by unit identifier: "297mm", "11in") - # @option options [String] paper_width Width of paper (number followed by unit identifier: "210mm", "8.5in") - # @option options [Integer] scale Print scaling (percent value, integer ranging from 10 to 400) - # @option options [Integer] paper_size - the size of paper to use - def initialize(options = {}) - set(options) - end - - # Set some or all page settings at once. + # Set some or all page settings at once. # @param [Hash] options The page settings to set (possible keys are :fit_to_height, :fit_to_width, :orientation, :paper_height, :paper_width, and :scale). def set(options) - options.each do |k, v| - send("#{k}=", v) if respond_to? "#{k}=" - end + parse_options options end # @see fit_to_height @@ -234,7 +235,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') str << '<pageSetup ' - str << instance_values.reject{ |k, v| k == 'worksheet' }.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v}"} }.join(' ') + serialized_attributes str str << '/>' end end |
