diff options
| author | Alex Rothenberg <[email protected]> | 2012-11-27 13:45:31 -0500 |
|---|---|---|
| committer | Alex Rothenberg <[email protected]> | 2012-11-27 14:16:55 -0500 |
| commit | 036f5883939a91fbc3eb377d968d85500dc3098a (patch) | |
| tree | 05133197c66091590840da3ed2fe3812eb3143aa /test/workbook/worksheet/tc_pivot_table.rb | |
| parent | 4560bd0a1b8b46bf4d8c0783f9fa12e8ceee714f (diff) | |
| download | caxlsx-036f5883939a91fbc3eb377d968d85500dc3098a.tar.gz caxlsx-036f5883939a91fbc3eb377d968d85500dc3098a.zip | |
Can configure a pivot table when creating it
see examples/pivot_table.rb
wb.add_worksheet(:name => "Data Sheet") do |sheet|
sheet.add_row ['Month', 'Year', 'Type', 'Sales', 'Region']
30.times { sheet.add_row [month, year, type, sales, region] }
sheet.add_pivot_table 'G4:L17', "A1:E31" do |pivot_table|
pivot_table.rows = ['Month', 'Year']
pivot_table.columns = ['Type']
pivot_table.data = ['Sales']
pivot_table.pages = ['Region']
end
end
Diffstat (limited to 'test/workbook/worksheet/tc_pivot_table.rb')
| -rw-r--r-- | test/workbook/worksheet/tc_pivot_table.rb | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb index 6f1f3f7a..3a94cfae 100644 --- a/test/workbook/worksheet/tc_pivot_table.rb +++ b/test/workbook/worksheet/tc_pivot_table.rb @@ -4,9 +4,9 @@ class TestPivotTable < Test::Unit::TestCase def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet - 40.times do - @ws << ["aa","aa","aa","aa","aa","aa"] - end + + @ws << ["Year","Month","Region", "Type", "Sales"] + @ws << [2012, "Nov", "East", "Soda", "12345"] end def test_initialization @@ -24,6 +24,29 @@ class TestPivotTable < Test::Unit::TestCase assert_equal(@ws.pivot_tables.last, pivot_table, "must be added to worksheet pivot tables collection") end + def test_add_pivot_table_with_config + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') do |pt| + pt.rows = ['Year', 'Month'] + pt.columns = ['Type'] + pt.data = ['Sales'] + pt.pages = ['Region'] + end + assert_equal(['Year', 'Month'], pivot_table.rows) + assert_equal(['Type'], pivot_table.columns) + assert_equal(['Sales'], pivot_table.data) + assert_equal(['Region'], pivot_table.pages) + 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' )) + assert_equal(2, pivot_table.header_index_of('Region' )) + assert_equal(3, pivot_table.header_index_of('Type' )) + assert_equal(4, pivot_table.header_index_of('Sales' )) + assert_equal(nil, pivot_table.header_index_of('Missing')) + end + def test_pn @ws.add_pivot_table('G5:G6', 'A1:D5') assert_equal(@ws.pivot_tables.first.pn, "pivotTables/pivotTable1.xml") @@ -59,4 +82,20 @@ class TestPivotTable < Test::Unit::TestCase assert(errors.empty?, "error free validation") end + def test_to_xml_string_with_configuration + pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt| + pt.rows = ['Year', 'Month'] + pt.columns = ['Type'] + pt.data = ['Sales'] + pt.pages = ['Region'] + end + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) + doc = Nokogiri::XML(pivot_table.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end end |
