Class: Axlsx::PivotTable

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/axlsx/workbook/worksheet/pivot_table.rb

Overview

Note:

Worksheet#add_pivot_table is the recommended way to create tables for your worksheets.

Table

See Also:

  • for examples

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(ref, range, sheet, options = {}) {|_self| ... } ⇒ PivotTable

Creates a new PivotTable object

Parameters:

  • ref (String)

    The reference to where the pivot table lives like 'G4:L17'.

  • range (String)

    The reference to the pivot table data like 'A1:D31'.

  • sheet (Worksheet)

    The sheet containing the table data.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 14

def initialize(ref, range, sheet, options = {})
  @ref = ref
  self.range = range
  @sheet = sheet
  @sheet.workbook.pivot_tables << self
  @name = "PivotTable#{index + 1}"
  @data_sheet = nil
  @rows = []
  @columns = []
  @data = []
  @pages = []
  @subtotal = nil
  @no_subtotals_on_headers = []
  @sort_on_headers = {}
  @style_info = {}
  parse_options options
  yield self if block_given?
end

Instance Attribute Details

#columnsArray

The columns

Returns:

  • (Array)


104
105
106
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 104

def columns
  @columns
end

#dataArray

The data

Returns:

  • (Array)


117
118
119
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 117

def data
  @data
end

#data_sheetObject

See Also:



73
74
75
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 73

def data_sheet
  @data_sheet || @sheet
end

#nameString (readonly)

The name of the table.

Returns:

  • (String)


62
63
64
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 62

def name
  @name
end

#no_subtotals_on_headersArray

Defines the headers in which subtotals are not to be included.

Returns:

  • (Array)


35
36
37
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 35

def no_subtotals_on_headers
  @no_subtotals_on_headers
end

#pagesString

The pages

Returns:

  • (String)


141
142
143
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 141

def pages
  @pages
end

#rangeString

The range where the data for this pivot table lives.

Returns:

  • (String)


79
80
81
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 79

def range
  @range
end

#refString (readonly)

The reference to the table data

Returns:

  • (String)


58
59
60
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 58

def ref
  @ref
end

#rowsArray

The rows

Returns:

  • (Array)


91
92
93
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 91

def rows
  @rows
end

#sheetString (readonly)

The name of the sheet.

Returns:

  • (String)


66
67
68
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 66

def sheet
  @sheet
end

#sort_on_headersHash

Defines the headers in which sort is applied. Can be an array of headers to sort ascending by default, or a hash for specific control (with headers as keys, :ascending or :descending as values).

Examples: ["year", "month"] or {"year" => :descending, "month" => :descending}

Returns:

  • (Hash)


43
44
45
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 43

def sort_on_headers
  @sort_on_headers
end

#style_infoHash

Style info for the pivot table

Returns:

  • (Hash)


54
55
56
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 54

def style_info
  @style_info
end

Instance Method Details

#cache_definitionPivotTableCacheDefinition

The cache_definition for this pivot table



172
173
174
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 172

def cache_definition
  @cache_definition ||= PivotTableCacheDefinition.new(self)
end

#header_cell_refsArray

References for header cells

Returns:

  • (Array)


267
268
269
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 267

def header_cell_refs
  Axlsx::range_to_a(header_range).first
end

#header_cell_valuesArray

The values in the header cells collection

Returns:

  • (Array)


279
280
281
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 279

def header_cell_values
  header_cells.map(&:value)
end

#header_cellsArray

The header cells for the pivot table

Returns:

  • (Array)


273
274
275
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 273

def header_cells
  data_sheet[header_range]
end

#header_cells_countInteger

The number of cells in the header_cells collection

Returns:

  • (Integer)


285
286
287
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 285

def header_cells_count
  header_cells.count
end

#header_index_of(value) ⇒ Integer

The index of a given value in the header cells

Returns:

  • (Integer)


291
292
293
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 291

def header_index_of(value)
  header_cell_values.index(value)
end

#indexInteger

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)


154
155
156
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 154

def index
  @sheet.workbook.pivot_tables.index(self)
end

#pnString

The part name for this table

Returns:

  • (String)


160
161
162
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 160

def pn
  "#{PIVOT_TABLE_PN % (index + 1)}"
end

#relationshipsRelationships

The relationships for this pivot table.

Returns:



178
179
180
181
182
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 178

def relationships
  r = Relationships.new
  r << Relationship.new(cache_definition, PIVOT_TABLE_CACHE_DEFINITION_R, "../#{cache_definition.pn}")
  r
end

#rels_pnString

The relationship part name of this pivot table

Returns:

  • (String)


166
167
168
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 166

def rels_pn
  "#{PIVOT_TABLE_RELS_PN % (index + 1)}"
end

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/axlsx/workbook/worksheet/pivot_table.rb', line 187

def to_xml_string(str = '')
  str << '<?xml version="1.0" encoding="UTF-8"?>'

  str << ('<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"' << (data.size <= 1 ? ' dataOnRows="1"' : '') << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">')

  str << ('<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>')
  str << ('<pivotFields count="' << header_cells_count.to_s << '">')

  header_cell_values.each do |cell_value|
    subtotal = !no_subtotals_on_headers.include?(cell_value)
    sorttype = sort_on_headers[cell_value]
    str << pivot_field_for(cell_value, subtotal, sorttype)
  end

  str << '</pivotFields>'
  if rows.empty?
    str << '<rowFields count="1"><field x="-2"/></rowFields>'
    str << '<rowItems count="2"><i><x/></i> <i i="1"><x v="1"/></i></rowItems>'
  else
    str << ('<rowFields count="' << rows.size.to_s << '">')
    rows.each do |row_value|
      str << ('<field x="' << header_index_of(row_value).to_s << '"/>')
    end
    str << '</rowFields>'
    str << ('<rowItems count="' << rows.size.to_s << '">')
    rows.size.times do |i|
      str << '<i/>'
    end
    str << '</rowItems>'
  end
  if columns.empty?
    if data.size > 1
      str << '<colFields count="1"><field x="-2"/></colFields>'
      str << "<colItems count=\"#{data.size}\">"
      str << '<i><x/></i>'
      data[1..-1].each_with_index do |datum_value, i|
        str << "<i i=\"#{i + 1}\"><x v=\"#{i + 1}\"/></i>"
      end
      str << '</colItems>'
    else
      str << '<colItems count="1"><i/></colItems>'
    end
  else
    str << ('<colFields count="' << columns.size.to_s << '">')
    columns.each do |column_value|
      str << ('<field x="' << header_index_of(column_value).to_s << '"/>')
    end
    str << '</colFields>'
  end
  unless pages.empty?
    str << ('<pageFields count="' << pages.size.to_s << '">')
    pages.each do |page_value|
      str << ('<pageField fld="' << header_index_of(page_value).to_s << '"/>')
    end
    str << '</pageFields>'
  end
  unless data.empty?
    str << "<dataFields count=\"#{data.size}\">"
    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
    str << '</dataFields>'
  end
  # custom pivot table style
  unless style_info.empty?
    str << '<pivotTableStyleInfo'
    style_info.each do |k, v|
      str << ' ' << k.to_s << '="' << v.to_s << '"'
    end
    str << ' />'
  end
  str << '</pivotTableDefinition>'
end