From 31a4e6cb13d139c7d54038ecb204b4ae1b152c61 Mon Sep 17 00:00:00 2001 From: Evan Hallmark Date: Mon, 7 Nov 2016 11:19:12 -0800 Subject: [PATCH 1/4] Override NumFmt serialized_attributes to ovid removing underscores --- lib/axlsx/stylesheet/num_fmt.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb index 8276ba18..1072d18a 100644 --- a/lib/axlsx/stylesheet/num_fmt.rb +++ b/lib/axlsx/stylesheet/num_fmt.rb @@ -73,5 +73,14 @@ def to_xml_string(str = '') serialized_tag('numFmt', str) end + # Override to avoid removing underscores + def serialized_attributes(str = '', additional_attributes = {}) + attributes = declared_attributes.merge! additional_attributes + attributes.each do |key, value| + str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.booleanize(value)}\" " + end + str + end + end end From 5d42f7708f7d3f1c25e9f31ff13db58679c1dcb7 Mon Sep 17 00:00:00 2001 From: Evan Hallmark Date: Mon, 7 Nov 2016 11:49:58 -0800 Subject: [PATCH 2/4] Added pivot table option to pass in list of row fields to omit subtotals from --- lib/axlsx/workbook/worksheet/pivot_table.rb | 20 +++++++++++++++----- test/workbook/worksheet/tc_pivot_table.rb | 10 +++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb index 0d5f34e8..71bcde90 100644 --- a/lib/axlsx/workbook/worksheet/pivot_table.rb +++ b/lib/axlsx/workbook/worksheet/pivot_table.rb @@ -25,10 +25,15 @@ def initialize(ref, range, sheet, options={}) @data = [] @pages = [] @subtotal = nil + @no_subtotals_on_headers = [] parse_options options yield self if block_given? end + # Defines the headers in which subtotals are not to be included + # @return[Array] + attr_accessor :no_subtotals_on_headers + # The reference to the table data # @return [String] attr_reader :ref @@ -163,7 +168,7 @@ def to_xml_string(str = '') str << ( '') str << ( '') header_cell_values.each do |cell_value| - str << pivot_field_for(cell_value) + str << pivot_field_for(cell_value,!no_subtotals_on_headers.include?(cell_value)) end str << '' if rows.empty? @@ -200,8 +205,9 @@ def to_xml_string(str = '') unless data.empty? str << "" data.each do |datum_value| - str << "" end str << '' @@ -241,9 +247,13 @@ def header_index_of(value) private - def pivot_field_for(cell_ref) + def pivot_field_for(cell_ref, subtotal=true) if rows.include? cell_ref - '' + '' + '' + if subtotal + '' + '' + '' + else + '' + '' + end elsif columns.include? cell_ref '' + '' + '' elsif pages.include? cell_ref diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb index 3c42d605..a62e6f40 100644 --- a/test/workbook/worksheet/tc_pivot_table.rb +++ b/test/workbook/worksheet/tc_pivot_table.rb @@ -67,7 +67,15 @@ def test_add_pivot_table_with_options_on_data_field assert_equal([{:ref=>"Sales", :subtotal => 'average'}], pivot_table.data) end - def test_header_indices + def test_add_pivot_table_with_row_without_subtotals + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5', {:no_subtotals_on_headers=>['Year']}) do |pt| + pt.data = ['Sales'] + pt.rows = ['Year','Month'] + end + assert_equal(['Year'], pivot_table.no_subtotals_on_headers) + end + + def test_header_indices pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') assert_equal(0, pivot_table.header_index_of('Year' )) assert_equal(1, pivot_table.header_index_of('Month' )) From 90b69543ccad773ffff581dd929d94502e45cae9 Mon Sep 17 00:00:00 2001 From: Evan Hallmark Date: Mon, 7 Nov 2016 11:53:17 -0800 Subject: [PATCH 3/4] Updated pivot_table#pivot_field_for to use the axisPage attr for page fields instead of axisCol --- lib/axlsx/workbook/worksheet/pivot_table.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb index 71bcde90..7dac7aa3 100644 --- a/lib/axlsx/workbook/worksheet/pivot_table.rb +++ b/lib/axlsx/workbook/worksheet/pivot_table.rb @@ -257,7 +257,7 @@ def pivot_field_for(cell_ref, subtotal=true) elsif columns.include? cell_ref '' + '' + '' elsif pages.include? cell_ref - '' + '' + '' + '' + '' + '' elsif data_refs.include? cell_ref '' + '' else From cbfabd0dca2eb4f1b3af81bbaa7433246c5b198d Mon Sep 17 00:00:00 2001 From: Evan Hallmark Date: Mon, 7 Nov 2016 13:13:58 -0800 Subject: [PATCH 4/4] Added style_info field to pivot_table.rb --- lib/axlsx/workbook/worksheet/pivot_table.rb | 17 +++++++++++++++-- test/workbook/worksheet/tc_pivot_table.rb | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb index 7dac7aa3..fffced0b 100644 --- a/lib/axlsx/workbook/worksheet/pivot_table.rb +++ b/lib/axlsx/workbook/worksheet/pivot_table.rb @@ -26,6 +26,7 @@ def initialize(ref, range, sheet, options={}) @pages = [] @subtotal = nil @no_subtotals_on_headers = [] + @style_info = {} parse_options options yield self if block_given? end @@ -34,6 +35,10 @@ def initialize(ref, range, sheet, options={}) # @return[Array] attr_accessor :no_subtotals_on_headers + # Style info for the pivot table + # @return[Hash] + attr_accessor :style_info + # The reference to the table data # @return [String] attr_reader :ref @@ -206,12 +211,20 @@ def to_xml_string(str = '') str << "" data.each do |datum_value| # The correct name prefix in ["Sum","Average", etc...] - str << "" end str << '' end + # custom pivot table style + unless style_info.empty? + str << '' + end str << '' end diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb index a62e6f40..08ad49e6 100644 --- a/test/workbook/worksheet/tc_pivot_table.rb +++ b/test/workbook/worksheet/tc_pivot_table.rb @@ -67,6 +67,18 @@ def test_add_pivot_table_with_options_on_data_field assert_equal([{:ref=>"Sales", :subtotal => 'average'}], pivot_table.data) end + def test_add_pivot_table_with_style_info + style_info_data = { :name=>"PivotStyleMedium9", :showRowHeaders=>"1", :showLastColumn=>"0"} + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5', {:style_info=>style_info_data}) do |pt| + pt.rows = ['Year', 'Month'] + pt.columns = ['Type'] + pt.data = ['Sales'] + pt.pages = ['Region'] + end + assert_equal(style_info_data, pivot_table.style_info) + shared_test_pivot_table_xml_validity(pivot_table) + end + def test_add_pivot_table_with_row_without_subtotals pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5', {:no_subtotals_on_headers=>['Year']}) do |pt| pt.data = ['Sales']