1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# frozen_string_literal: true
require 'tc_helper'
def shared_test_pivot_table_xml_validity(pivot_table)
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_empty(errors, "error free validation")
end
class TestPivotTable < Test::Unit::TestCase
def setup
p = Axlsx::Package.new
@ws = p.workbook.add_worksheet
@ws << ["Year", "Month", "Region", "Type", "Sales"]
@ws << [2012, "Nov", "East", "Soda", "12345"]
end
def test_initialization
assert_empty(@ws.workbook.pivot_tables)
assert_empty(@ws.pivot_tables)
end
def test_add_pivot_table
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal('G5:G6', pivot_table.ref, 'ref assigned from first parameter')
assert_equal('A1:D5', pivot_table.range, 'range assigned from second parameter')
assert_equal('PivotTable1', pivot_table.name, 'name automatically generated')
assert(pivot_table.is_a?(Axlsx::PivotTable), "must create a pivot table")
assert_equal(@ws.workbook.pivot_tables.last, pivot_table, "must be added to workbook pivot tables collection")
assert_equal(@ws.pivot_tables.last, pivot_table, "must be added to worksheet pivot tables collection")
end
def test_set_pivot_table_data_sheet
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5')
data_sheet = @ws.clone
data_sheet.name = "Pivot Table Data Source"
assert_equal(pivot_table.data_sheet.name, @ws.name, "must default to the same sheet the pivot table is added to")
pivot_table.data_sheet = data_sheet
assert_equal(pivot_table.data_sheet.name, data_sheet.name, "data sheet assigned to pivot table")
end
def test_add_pivot_table_with_config
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
assert_equal(['Year', 'Month'], pivot_table.rows)
assert_equal(['Type'], pivot_table.columns)
assert_equal([{ ref: "Sales" }], pivot_table.data)
assert_equal(['Region'], pivot_table.pages)
shared_test_pivot_table_xml_validity(pivot_table)
end
def test_add_pivot_table_with_options_on_data_field
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') do |pt|
pt.data = [{ ref: "Sales", subtotal: 'average' }]
end
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']
pt.rows = ['Year', 'Month']
end
assert_equal(['Year'], pivot_table.no_subtotals_on_headers)
end
def test_add_pivot_table_with_months_sorted
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5', { sort_on_headers: ['Month'] }) do |pt|
pt.data = ['Sales']
pt.rows = ['Year', 'Month']
end
assert_equal({ 'Month' => :ascending }, pivot_table.sort_on_headers)
pivot_table.sort_on_headers = { 'Month' => :descending }
assert_equal({ 'Month' => :descending }, pivot_table.sort_on_headers)
shared_test_pivot_table_xml_validity(pivot_table)
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_nil(pivot_table.header_index_of('Missing'))
assert_equal(%w(A1 B1 C1 D1 E1), pivot_table.header_cell_refs)
end
def test_pn
@ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal("pivotTables/pivotTable1.xml", @ws.pivot_tables.first.pn)
end
def test_index
@ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal(@ws.pivot_tables.first.index, @ws.workbook.pivot_tables.index(@ws.pivot_tables.first))
end
def test_relationships
assert_empty(@ws.relationships)
@ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal(1, @ws.relationships.size, "adding a pivot table adds a relationship")
@ws.add_pivot_table('G10:G11', 'A1:D5')
assert_equal(2, @ws.relationships.size, "adding a pivot table adds a relationship")
end
def test_rels_pn
@ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal("pivotTables/_rels/pivotTable1.xml.rels", @ws.pivot_tables.first.rels_pn)
end
def test_to_xml_string
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5', { no_subtotals_on_headers: ['Year'] }) do |pt|
pt.rows = ['Year', 'Month']
pt.columns = ['Type']
pt.data = ['Sales']
pt.pages = ['Region']
end
shared_test_pivot_table_xml_validity(pivot_table)
end
def test_to_xml_string_with_options_on_data_field
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
pt.data = [{ ref: "Sales", subtotal: 'average' }]
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
def test_pivot_table_with_more_than_one_data_row
### https://github.com/caxlsx/caxlsx/issues/110
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
pt.rows = ["Date", "Name"]
pt.data = [
{ ref: "Gross amount", num_fmt: 2 },
{ ref: "Net amount", num_fmt: 2 },
{ ref: "Margin", num_fmt: 2 }
]
end
xml = pivot_table.to_xml_string
refute_includes(xml, 'dataOnRows')
assert_includes(xml, 'colFields')
assert_includes(xml, 'colItems')
doc = Nokogiri::XML(pivot_table.to_xml_string)
assert_equal('1', doc.at_css('colFields')['count'])
assert_equal('-2', doc.at_css('colFields field')['x'])
assert_equal('3', doc.at_css('colItems')['count'])
assert_equal(3, doc.at_css('colItems').children.size)
assert_nil(doc.at_css('colItems i')['x'])
assert_equal('1', doc.at_css('colItems i[i=1] x')['v'])
assert_equal('2', doc.at_css('colItems i[i=2] x')['v'])
end
def test_pivot_table_with_only_one_data_row
### https://github.com/caxlsx/caxlsx/issues/110
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
pt.rows = ["Date", "Name"]
pt.data = [
{ ref: "Gross amount", num_fmt: 2 }
]
end
xml = pivot_table.to_xml_string
assert_includes(xml, 'dataOnRows')
assert_includes(xml, 'colItems')
refute_includes(xml, 'colFields')
end
end
|