summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-09-23 11:20:37 +0900
committerRandy Morgan <[email protected]>2012-09-23 11:20:37 +0900
commite73bd501eb15106daeb4cf9830991a8181835b06 (patch)
tree441057859986aafb27d4cc21171eca52a18153c8 /lib
parent54e3e2f157df97e6edebaca3510fb0603be432a9 (diff)
downloadcaxlsx-e73bd501eb15106daeb4cf9830991a8181835b06.tar.gz
caxlsx-e73bd501eb15106daeb4cf9830991a8181835b06.zip
Autofilter extended implementation
Beginnings of 18.3.2 AutoFilter Settings implementation. I've moved auto_filter into its own directory as there will be a minimum of 15 or 20 classes required to implement this in full.
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx.rb1
-rw-r--r--lib/axlsx/workbook/workbook.rb2
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter.rb35
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb58
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb70
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filters.rb14
-rw-r--r--lib/axlsx/workbook/worksheet/table_style_info.rb2
7 files changed, 145 insertions, 37 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index 50f34c70..85a7b7c9 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -95,6 +95,7 @@ module Axlsx
# @param [String] s The snake case string to camelize
# @return [String]
def self.camel(s="", all_caps = true)
+ s = s.to_s
s = s.capitalize if all_caps
s.gsub(/_(.)/){ $1.upcase }
end
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index 3ff61c07..cdc73360 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
module Axlsx
-require 'axlsx/workbook/worksheet/auto_filter.rb'
+require 'axlsx/workbook/worksheet/auto_filter/auto_filter.rb'
require 'axlsx/workbook/worksheet/date_time_converter.rb'
require 'axlsx/workbook/worksheet/protected_range.rb'
require 'axlsx/workbook/worksheet/protected_ranges.rb'
diff --git a/lib/axlsx/workbook/worksheet/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter.rb
deleted file mode 100644
index 7d9fbcfc..00000000
--- a/lib/axlsx/workbook/worksheet/auto_filter.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-module Axlsx
-
- #This class represents an auto filter range in a worksheet
- class AutoFilter
-
- # creates a new Autofilter object
- # @param [Worksheet] worksheet
- def initialize(worksheet)
- raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
- @worksheet = worksheet
- end
-
- attr_reader :worksheet
-
- # The range the autofilter should be applied to.
- # This should be a string like 'A1:B8'
- # @return [String]
- attr_accessor :range
-
- # the formula for the defined name required for this auto filter
- # @return [String]
- def defined_name
- return unless range
- Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
- end
-
- # serialize the object
- # @return [String]
- def to_xml_string(str='')
- return unless range
- str << "<autoFilter ref='#{range}'></autoFilter>"
- end
-
- end
-end
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
new file mode 100644
index 00000000..a2e82835
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
@@ -0,0 +1,58 @@
+
+require 'axlsx/workbook/worksheet/auto_filter/filter_column.rb'
+require 'axlsx/workbook/worksheet/auto_filter/filters.rb'
+
+module Axlsx
+
+ #This class represents an auto filter range in a worksheet
+ class AutoFilter
+
+ # creates a new Autofilter object
+ # @param [Worksheet] worksheet
+ def initialize(worksheet)
+ raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
+ @worksheet = worksheet
+ end
+
+ attr_reader :worksheet
+
+ # The range the autofilter should be applied to.
+ # This should be a string like 'A1:B8'
+ # @return [String]
+ attr_accessor :range
+
+ # the formula for the defined name required for this auto filter
+ # This prepends the worksheet name to the absolute cell reference
+ # e.g. A1:B2 -> 'Sheet1'!$A$1:$B$2
+ # @return [String]
+ def defined_name
+ return unless range
+ Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
+ end
+
+ # A collection of filterColumns for this auto_filter
+ # @return [SimpleTypedList]
+ def columns
+ @columns ||= SimpleTypedList.new FilterColumn
+ end
+
+ # Adds a filter column. This is the recommended way to create and manage filter columns for your autofilter.
+ # In addition to the require id and type parameters, options will be passed to the filter column during instantiation.
+ # @param [String] col_id Zero-based index indicating the AutoFilter column to which this filter information applies.
+ # @param [Symbol] filter_type A symbol representing one of the supported filter types.
+ # @param [Hash] options a hash of options to pass into the generated filter
+ # @return [FilterColumn]
+ def add_column(col_id, filter_type, options = {})
+ columns << FilterColumn.new(col_id, filter_type, options)
+ columns.last
+ end
+
+ # serialize the object
+ # @return [String]
+ def to_xml_string(str='')
+ return unless range
+ str << "<autoFilter ref='#{range}'></autoFilter>"
+ end
+
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
new file mode 100644
index 00000000..57f7e23b
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
@@ -0,0 +1,70 @@
+module Axlsx
+ # The filterColumn collection identifies a particular column in the AutoFilter
+ # range and specifies filter information that has been applied to this column.
+ # If a column in the AutoFilter range has no criteria specified,
+ # then there is no corresponding filterColumn collection expressed for that column.
+ class FilterColumn
+
+ 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
+
+ # Flag indicating whether the AutoFilter button for this column is hidden.
+ # @return [Boolean]
+ attr_reader :hidden_button
+
+ # Flag indicating whether the filter button is visible.
+ # When the cell containing the filter button is merged with another cell,
+ # the filter button can be hidden, and not drawn.
+ # @return [Boolean]
+ attr_reader :show_button
+
+ # Filter information applied to the column
+ # @return [Filters|Top10|CustomFilters|dynamicFilters|colorFilters|iconFilters] The filter
+ # information for this filter columns.
+ attr_reader :filter
+
+ # Creates a new FilterColumn object
+ # @note This class yeilds its filter object as that is where the vast majority of processing will be done
+ # @param [Integer|Cell] col_id The zero based index for the column to which this filter will be applied
+ # @param [Symbol] filter_type The symbolized class name of the filter to apply to this column.
+ # @param [Hash] options options for this object and the filter
+ # @option [Boolean] hidden_button @see hidden_button
+ # @option [Boolean] show_button @see show_button
+
+ def initialize(col_id, filter_type, options = {})
+ RestrictionValidator.validate 'FilterColumn.filter', FILTERS, filter_type
+ self.col_id = col_id
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ @filter = Axlsx.const_get(Axlsx.camel(filter_type))
+ yield @filter if block_given?
+ end
+
+ # Sets the col_id attribute for this filter column.
+ # @param [Integer | Cell] column_index The zero based index of the column to which this filter applies.
+ # When you specify a cell, the column index will be read off the cell
+ # @return [Integer]
+ def col_id=(column_index)
+ column_index = column_index.col if column_index.is_a?(Cell)
+ Axlsx.validate_unsigned_int column_index
+ @col_id = column_index
+ end
+
+ # Sets the button_hidden attribute for this filter column
+ # @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden.
+ # @return [Boolean]
+ def button_hidden=(hidden)
+ DataValidater.validate_boolean hidden
+ @button_hidden = hidden
+ end
+
+ # Serialize the object to xml
+ def to_xml_string(str='')
+
+ end
+
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
new file mode 100644
index 00000000..f37e2f29
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
@@ -0,0 +1,14 @@
+module Axlsx
+
+ class Filters
+
+ def initialize(options={})
+ options[:filter_items].each do |filter|
+ @filters << Filter.new(filter)
+ end
+ options[:date_group_items].each do |date_group|
+ @date_group_items << DateGroupItem.new(date_group)
+ end
+ end
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/table_style_info.rb b/lib/axlsx/workbook/worksheet/table_style_info.rb
index f0d08d8b..778546e0 100644
--- a/lib/axlsx/workbook/worksheet/table_style_info.rb
+++ b/lib/axlsx/workbook/worksheet/table_style_info.rb
@@ -20,7 +20,7 @@ module Axlsx
# @see Annex G. (normative) Predefined SpreadsheetML Style Definitions in part 1 of the specification.
def initialize(options = {})
initialize_defaults
- name= 'TableStyleMedium9'
+ @name = 'TableStyleMedium9'
options.each do |k, v|
send("#{k}=", v) if respond_to? "#{k}="
end