diff options
| author | Randy Morgan <[email protected]> | 2012-09-14 17:53:00 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-09-14 17:53:00 +0900 |
| commit | 3e4ee23a46c12f1a634f2cadee1798b0cb1000ff (patch) | |
| tree | 49c8d888a2131740c2765d95b96c391fd90078b8 /lib | |
| parent | 46a9ee40524b2485155361a728fde7d35d687066 (diff) | |
| download | caxlsx-3e4ee23a46c12f1a634f2cadee1798b0cb1000ff.tar.gz caxlsx-3e4ee23a46c12f1a634f2cadee1798b0cb1000ff.zip | |
Implement table style info for named tables.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/table.rb | 9 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/table_style_info.rb | 65 |
3 files changed, 74 insertions, 1 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 17e11ced..3ff61c07 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -33,6 +33,7 @@ require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' require 'axlsx/workbook/defined_name.rb' require 'axlsx/workbook/defined_names.rb' +require 'axlsx/workbook/worksheet/table_style_info.rb' require 'axlsx/workbook/worksheet/table.rb' require 'axlsx/workbook/worksheet/tables.rb' require 'axlsx/workbook/worksheet/data_validation.rb' diff --git a/lib/axlsx/workbook/worksheet/table.rb b/lib/axlsx/workbook/worksheet/table.rb index b848180b..424bb73b 100644 --- a/lib/axlsx/workbook/worksheet/table.rb +++ b/lib/axlsx/workbook/worksheet/table.rb @@ -28,6 +28,7 @@ module Axlsx @sheet = sheet @style = nil @sheet.workbook.tables << self + @table_style_info = TableStyleInfo.new(options[:style_info]) if options[:style_info] @name = "Table#{index+1}" options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" @@ -62,6 +63,12 @@ module Axlsx @name = v end end + + # TableStyleInfo for the table. + # initialization can be fed via the :style_info option + def table_style_info + @table_style_info ||= TableStyleInfo.new + end # Serializes the object # @param [String] str @@ -77,7 +84,7 @@ module Axlsx end str << '</tableColumns>' #TODO implement tableStyleInfo - str << '<tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0" name="TableStyleMedium9" />' + table_style_info.to_xml_string(str) # '<tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0" name="TableStyleMedium9" />' str << '</table>' end diff --git a/lib/axlsx/workbook/worksheet/table_style_info.rb b/lib/axlsx/workbook/worksheet/table_style_info.rb new file mode 100644 index 00000000..f0d08d8b --- /dev/null +++ b/lib/axlsx/workbook/worksheet/table_style_info.rb @@ -0,0 +1,65 @@ +module Axlsx + + # The table style info class manages style attributes for defined tables in + # a worksheet + class TableStyleInfo + + BOOLEAN_ATTRIBUTES = %w(show_first_column show_last_column show_row_stripes show_column_stripes) + + # creates a new TableStyleInfo instance + # @param [Hash] options + # @option [Boolean] show_first_column indicates if the first column should + # be shown + # @option [Boolean] show_last_column indicates if the last column should + # be shown + # @option [Boolean] show_column_stripes indicates if column stripes should + # be shown + # @option [Boolean] show_row_stripes indicates if row stripes should be shown + # @option [String] name The name of the style to apply to your table. + # Only predefined styles are currently supported. + # @see Annex G. (normative) Predefined SpreadsheetML Style Definitions in part 1 of the specification. + def initialize(options = {}) + initialize_defaults + name= 'TableStyleMedium9' + options.each do |k, v| + send("#{k}=", v) if respond_to? "#{k}=" + end + 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 + + # Initialize all the values to false as Excel requires them to + # explicitly be disabled or all will show. + def initialize_defaults + BOOLEAN_ATTRIBUTES.each do |attr| + self.send("#{attr}=", 0) + end + end + + # The name of the table style. + attr_accessor :name + + def to_xml_string(str = '') + str << '<tableStyleInfo ' + instance_values.each do |key, value| + str << Axlsx::camel(key, false) << "='#{value}' " + end + str << '/>' + end + end +end |
