summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-09-26 14:42:00 +0900
committerRandy Morgan <[email protected]>2012-09-26 14:42:00 +0900
commit7ae8e76838c5ccf921de032a8d7bfa6e1017d007 (patch)
tree5e16ff075e7b261b60c891097f25215fb24906ea /lib
parent178335010128df34a7d0a0a6492e3e9bf1e94b99 (diff)
downloadcaxlsx-7ae8e76838c5ccf921de032a8d7bfa6e1017d007.tar.gz
caxlsx-7ae8e76838c5ccf921de032a8d7bfa6e1017d007.zip
pre-release readme updates and more work on auto_filter preset values
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/util/validators.rb2
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb21
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/filters.rb10
-rw-r--r--lib/axlsx/workbook/worksheet/sheet_pr.rb135
4 files changed, 158 insertions, 10 deletions
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb
index 142f6dde..417de908 100644
--- a/lib/axlsx/util/validators.rb
+++ b/lib/axlsx/util/validators.rb
@@ -91,7 +91,7 @@ module Axlsx
# @raise [ArgumentError] raised if the value is not a Fixnun, Integer, Float value greater or equal to 0
# @return [Boolean] true if the data is valid
def self.validate_unsigned_numeric(v)
- DataTypeValidator.validate("Invalid column width", [Fixnum, Integer, Float], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 })
+ DataTypeValidator.validate("Invalid column width", [Fixnum, Integer, Float], v, lambda { |arg| arg.respond_to?(:>=) && arg.to_i >= 0 })
end
# Requires that the value is a Fixnum or Integer
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
index 3de6d6a6..b55ee884 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb
@@ -17,17 +17,22 @@ module Axlsx
# @option [Boolean] show_button @see show_button
def initialize(col_id, filter_type, options = {})
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
- @filter = Axlsx.const_get(Axlsx.camel(filter_type))
+ @filter = Axlsx.const_get(Axlsx.camel(filter_type)).new(options)
yield @filter if block_given?
end
# 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
# Flag indicating whether the filter button is visible.
# When the cell containing the filter button is merged with another cell,
@@ -56,13 +61,23 @@ module Axlsx
# @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden.
# @return [Boolean]
def hidden_button=(hidden)
- DataValidater.validate_boolean hidden
+ Axlsx.validate_boolean hidden
@hidden_button = hidden
end
+ # Flag indicating whether the AutoFilter button is show. This is
+ # undocumented in the spec, but exists in the schema file as an
+ # optional attribute.
+ # @param [Boolean] show Show or hide the button
+ # @return [Boolean]
+ def show_button=(show)
+ Axlsx.validate_boolean show
+ @show_botton = show
+ end
+
# Serialize the object to xml
def to_xml_string(str='')
- str << "<filterColumn colId='#{@col_id}' hiddenButton='#{@hidden_button}' showButton='#{@show_button}'>"
+ str << "<filterColumn colId='#{@col_id}' hiddenButton='#{hidden_button}' showButton='#{show_button}'>"
@filter.to_xml_string(str)
str << "</filterColumn>"
end
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
index d754f433..a71e999c 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/filters.rb
@@ -62,12 +62,16 @@ module Axlsx
# Serialize the object to xml
def to_xml_string(str = '')
- str << "<filters blank='#{@blank}' calendarType='#{@calendar_type}'>"
- @filters.each { |filter| filter.to_xml_string(str) }
- @date_group_items.each { |date_group_item| date_group_item.to_xml_string(str) }
+ str << "<filters blank='#{blank}' calendarType='#{calendar_type}'>"
+ filter_items.each { |filter| filter.to_xml_string(str) }
+ date_group_items.each { |date_group_item| date_group_item.to_xml_string(str) }
str << '</filters>'
end
+ # not entirely happy with this.
+ # filter_items should be a simple typed list that overrides << etc
+ # to create Filter objects from the inserted values. However this
+ # is most likely so rarely used...(really? do you know that?)
def filter_items=(values)
values.each do |value|
filter_items << Filter.new(value)
diff --git a/lib/axlsx/workbook/worksheet/sheet_pr.rb b/lib/axlsx/workbook/worksheet/sheet_pr.rb
index 6f03bf5b..710d3cc1 100644
--- a/lib/axlsx/workbook/worksheet/sheet_pr.rb
+++ b/lib/axlsx/workbook/worksheet/sheet_pr.rb
@@ -1,9 +1,37 @@
module Axlsx
-
+#<xsd:complexType name="CT_SheetPr">
+#<xsd:sequence>
+#<xsd:element name="tabColor" type="CT_Color" minOccurs="0" maxOccurs="1"/>
+#<xsd:element name="outlinePr" type="CT_OutlinePr" minOccurs="0" maxOccurs="1"/>
+#<xsd:element name="pageSetUpPr" type="CT_PageSetUpPr" minOccurs="0" maxOccurs="1"/>
+#</xsd:sequence>
+#<xsd:attribute name="syncHorizontal" type="xsd:boolean" use="optional" default="false"/>
+#<xsd:attribute name="syncVertical" type="xsd:boolean" use="optional" default="false"/>
+#<xsd:attribute name="syncRef" type="ST_Ref" use="optional"/>
+#<xsd:attribute name="transitionEvaluation" type="xsd:boolean" use="optional" default="false"/>
+#<xsd:attribute name="transitionEntry" type="xsd:boolean" use="optional" default="false"/>
+#<xsd:attribute name="published" type="xsd:boolean" use="optional" default="true"/>
+#<xsd:attribute name="codeName" type="xsd:string" use="optional"/>
+#<xsd:attribute name="filterMode" type="xsd:boolean" use="optional" default="false"/>
+#<xsd:attribute name="enableFormatConditionsCalculation" type="xsd:boolean" use="optional" default="true"/>
+#</xsd:complexType>
# The SheetPr class manages serialization fo a worksheet's sheetPr element.
# Only fit_to_page is implemented
class SheetPr
+
+ # These attributes are all boolean so I'm doing a bit of a hand
+ # waving magic show to set up the attriubte accessors
+ #
+ BOOLEAN_ATTRIBUTES = [:sync_horizontal,
+ :sync_vertical,
+ :transtion_evaluation,
+ :transition_entry,
+ :published,
+ :filter_mode,
+ :enable_format_conditions_calculation]
+
+
# Creates a new SheetPr object
# @param [Worksheet] worksheet The worksheet that owns this SheetPr object
def initialize(worksheet)
@@ -11,14 +39,115 @@ module Axlsx
@worksheet = worksheet
end
+ # Dynamically create accessors for boolean attriubtes
+ BOOLEAN_ATTRIBUTES.each do |attr|
+ class_eval %{
+ # The #{attr} attribute reader
+ # @return [Boolean]
+ attr_reader :#{attr}
+
+ # The #{attr} writer
+ # @param [Boolean] value The value to assign to #{attr}
+ # @return [Boolean]
+ def #{attr}=(value)
+ Axlsx::validate_boolean(value)
+ @#{attr} = value
+ end
+ }
+ end
+
+ # Anchor point for worksheet's window.
+ # @return [String]
+ attr_reader :code_name
+
+ # Specifies a stable name of the sheet, which should not change over time,
+ # and does not change from user input. This name should be used by code
+ # to reference a particular sheet.
+ # @return [String]
+ attr_reader :sync_ref
+
+ # The worksheet these properties apply to!
+ # @return [Worksheet]
attr_reader :worksheet
+ # @see code_name
+ # @param [String] name
+ def code_name=(name)
+ @code_name = name
+ end
+
+ # @see sync_ref
+ # @param [String] ref A cell reference (e.g. "A1")
+ def sync_ref=(ref)
+ @sync_ref = ref
+ end
+
# Serialize the object
# @param [String] str serialized output will be appended to this object if provided.
# @return [String]
def to_xml_string(str = '')
- return unless worksheet.fit_to_page?
- str << "<sheetPr><pageSetUpPr fitToPage=\"%s\"></pageSetUpPr></sheetPr>" % worksheet.fit_to_page?
+ update_properties
+ str << "<sheetPr #{serialized_attributes}>"
+ page_setup_pr.to_xml_string(str)
+ str << "</sheetPr>"
+ end
+
+ def page_setup_pr
+ @page_setup_pr ||= PageSetUpPr.new
+ end
+
+ private
+
+ def serialized_attributes(str = '')
+ instance_values.each do |key, value|
+ unless %(worksheet page_setup_pr).include? key
+ str << "#{Axlsx.camel(key, false)}='#{value}' "
+ end
+ end
+ str
+ end
+
+
+ def update_properties
+ page_setup_pr.fit_to_page = worksheet.fit_to_page?
+ filter_mode = worksheet.auto_filter.columns.size > 0
+ end
+ end
+
+
+ class PageSetUpPr
+
+ # creates a new page setup properties object
+ # @param [Hash] options
+ # @option [Boolean] fit_to_page Flag indicating whether the sheet displays Automatic Page Breaks.
+ # @option [Boolean] auto_page_breaks 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
+ end
+
+ # Flag indicating whether the sheet displays Automatic Page Breaks.
+ # @param [Boolean] value
+ # @return [Boolean]
+ def fit_to_page=(value)
+ Axlsx.validate_boolean value
+ @fit_to_page = value
+ end
+
+ # Flag indicating whether the Fit to Page print option is enabled.
+ # @param [Boolean] value
+ # @return [Boolean]
+ def auto_page_breaks=(value)
+ Alxsx.validate_boolean value
+ @auto_page_breaks = value
+ end
+
+ # serialize to xml
+ def to_xml_string(str='')
+ str << '<pageSetUpPr '
+ instance_values.each { |key, value| str << "#{Axlsx.camel(key, false)}='#{value}' " }
+ str << '></pageSetUpPr>'
end
end
end