diff options
| author | Knut I. Stenmark <[email protected]> | 2021-03-19 11:47:49 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-03-19 11:47:49 +0100 |
| commit | 33e2771710a1f0a7740d15cf0b9925f9079d486b (patch) | |
| tree | 87cb7e89e0da6000ddaadbad445e5cde23e56e7f | |
| parent | 927849bb091f9858fe54d2b48742aa531f21dfb3 (diff) | |
| download | caxlsx-33e2771710a1f0a7740d15cf0b9925f9079d486b.tar.gz caxlsx-33e2771710a1f0a7740d15cf0b9925f9079d486b.zip | |
Add support for format in pivot tables (#79)
| -rw-r--r-- | lib/axlsx/workbook/worksheet/pivot_table.rb | 9 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_pivot_table.rb | 8 |
2 files changed, 15 insertions, 2 deletions
diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb index 55174968..d8c630ec 100644 --- a/lib/axlsx/workbook/worksheet/pivot_table.rb +++ b/lib/axlsx/workbook/worksheet/pivot_table.rb @@ -111,8 +111,12 @@ module Axlsx if data_field.is_a? String data_field = {:ref => data_field} end - data_field.values.each do |value| - DataTypeValidator.validate "#{self.class}.data[]", [String], value + data_field.each do |key, value| + if key == :num_fmt + DataTypeValidator.validate "#{self.class}.data[]", [Integer], value + else + DataTypeValidator.validate "#{self.class}.data[]", [String], value + end end @data << data_field end @@ -212,6 +216,7 @@ module Axlsx data.each do |datum_value| # The correct name prefix in ["Sum","Average", etc...] str << "<dataField name='#{(datum_value[:subtotal]||'')} of #{datum_value[:ref]}' fld='#{header_index_of(datum_value[:ref])}' baseField='0' baseItem='0'" + str << " numFmtId='#{datum_value[:num_fmt]}'" if datum_value[:num_fmt] str << " subtotal='#{datum_value[:subtotal]}' " if datum_value[:subtotal] str << "/>" end diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb index 6d7f4d1c..a494dcfd 100644 --- a/test/workbook/worksheet/tc_pivot_table.rb +++ b/test/workbook/worksheet/tc_pivot_table.rb @@ -132,4 +132,12 @@ class TestPivotTable < Test::Unit::TestCase end shared_test_pivot_table_xml_validity(pivot_table) end + + def test_add_pivot_table_with_format_options_on_data_field + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt| + pt.data = [{:ref=>"Sales", :subtotal => 'sum', num_fmt: 4}] + end + doc = Nokogiri::XML(pivot_table.to_xml_string) + assert_equal('4', doc.at_css('dataFields dataField')['numFmtId'], 'adding format options to pivot_table') + end end |
