diff options
| -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 | ||||
| -rw-r--r-- | test/workbook/worksheet/table/tc_table.rb | 7 | ||||
| -rw-r--r-- | test/workbook/worksheet/table/tc_table_style_info.rb | 53 |
5 files changed, 133 insertions, 2 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 diff --git a/test/workbook/worksheet/table/tc_table.rb b/test/workbook/worksheet/table/tc_table.rb index 7c87c69e..de86b886 100644 --- a/test/workbook/worksheet/table/tc_table.rb +++ b/test/workbook/worksheet/table/tc_table.rb @@ -15,6 +15,12 @@ class TestTable < Test::Unit::TestCase end + def test_table_style_info + table = @ws.add_table('A1:D5', :name => 'foo', :style_info => { :show_row_stripes => true, :name => "TableStyleMedium25" }) + assert_equal('TableStyleMedium25', table.table_style_info.name) + assert_equal(true, table.table_style_info.show_row_stripes) + end + def test_add_table name = "test" table = @ws.add_table("A1:D5", :name => name) @@ -22,7 +28,6 @@ class TestTable < Test::Unit::TestCase assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection") assert_equal(@ws.tables.last, table, "must be added to worksheet table collection") assert_equal(table.name, name, "options for name are applied") - end def test_pn diff --git a/test/workbook/worksheet/table/tc_table_style_info.rb b/test/workbook/worksheet/table/tc_table_style_info.rb new file mode 100644 index 00000000..c0c452c9 --- /dev/null +++ b/test/workbook/worksheet/table/tc_table_style_info.rb @@ -0,0 +1,53 @@ +require 'tc_helper.rb' + +class TestTableStyleInfo < Test::Unit::TestCase + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet + 40.times do + @ws.add_row %w(aa bb cc dd ee ff gg hh ii jj kk) + end + @table = @ws.add_table(Axlsx::cell_range([@ws.rows.first.cells.first,@ws.rows.last.cells.last], false), :name => 'foo') + @options = { :show_first_column => 1, + :show_last_column => 1, + :show_row_stripes => 1, + :show_column_stripes => 1, + :name => "TableStyleDark4" } + + + end + + def test_initialize + table_style = Axlsx::TableStyleInfo.new @options + @options.each do |key, value| + assert_equal(value, table_style.send(key.to_sym)) + end + end + + def test_boolean_properties + table_style = Axlsx::TableStyleInfo.new + @options.keys.each do |key| + assert_nothing_raised { table_style.send("#{key.to_sym}=", true) } + assert_raises(ArgumentError) { table_style.send(key.to_sym, 'foo') } + end + end + def doc + @doc ||= Nokogiri::XML(Axlsx::TableStyleInfo.new(@options).to_xml_string) + end + + def test_to_xml_string_first_column + assert(doc.xpath('//tableStyleInfo[@showLastColumn=1]')) + end + + def test_to_xml_string_row_stripes + assert(doc.xpath('//tableStyleInfo[@showRowStripes=1]')) + end + + def test_to_xml_string_column_stripes + assert(doc.xpath('//tableStyleInfo[@showColumnStripes=1]')) + end + + def test_to_xml_string_name + assert(doc.xpath("//tableStyleInfo[@name=#{@options[:name]}]")) + end +end |
