Class: Axlsx::Table

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

Overview

Note:

Worksheet#add_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, sheet, options = {}) {|_self| ... } ⇒ Table

Creates a new Table object

Parameters:

  • ref (String)

    The reference to the table data like 'A1:G24'.

  • sheet (Worksheet)

    The sheet containing the table data.

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

    a customizable set of options

Options Hash (options):

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Table)

    the object that the method was called on



13
14
15
16
17
18
19
20
21
22
# File 'lib/axlsx/workbook/worksheet/table.rb', line 13

def initialize(ref, sheet, options = {})
  @ref = ref
  @sheet = sheet
  @style = nil
  @sheet.workbook.tables << self
  @table_style_info = TableStyleInfo.new(options[:style_info]) if options[:style_info]
  @name = "Table#{index + 1}"
  parse_options options
  yield self if block_given?
end

Instance Attribute Details

#nameString

The name of the table.

Returns:

  • (String)


30
31
32
# File 'lib/axlsx/workbook/worksheet/table.rb', line 30

def name
  @name
end

#refString (readonly)

The reference to the table data

Returns:

  • (String)


26
27
28
# File 'lib/axlsx/workbook/worksheet/table.rb', line 26

def ref
  @ref
end

#styleTableStyle (readonly)

The style for the table.

Returns:



34
35
36
# File 'lib/axlsx/workbook/worksheet/table.rb', line 34

def style
  @style
end

Instance Method Details

#indexInteger

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)


38
39
40
# File 'lib/axlsx/workbook/worksheet/table.rb', line 38

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

#pnString

The part name for this table

Returns:

  • (String)


44
45
46
# File 'lib/axlsx/workbook/worksheet/table.rb', line 44

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

#rIdString

The relationship id for this table.

Returns:

  • (String)

See Also:



51
52
53
# File 'lib/axlsx/workbook/worksheet/table.rb', line 51

def rId
  @sheet.relationships.for(self).Id
end

#table_style_infoObject

TableStyleInfo for the table. initialization can be fed via the :style_info option



67
68
69
# File 'lib/axlsx/workbook/worksheet/table.rb', line 67

def table_style_info
  @table_style_info ||= TableStyleInfo.new
end

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

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

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/axlsx/workbook/worksheet/table.rb', line 74

def to_xml_string(str = '')
  str << '<?xml version="1.0" encoding="UTF-8"?>'
  str << ('<table xmlns="' << XML_NS << '" id="' << (index + 1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/, '_') << '" ')
  str << ('ref="' << @ref << '" totalsRowShown="0">')
  str << ('<autoFilter ref="' << @ref << '"/>')
  str << ('<tableColumns count="' << header_cells.length.to_s << '">')
  header_cells.each_with_index do |cell, index|
    str << ('<tableColumn id ="' << (index + 1).to_s << '" name="' << cell.clean_value << '"/>')
  end
  str << '</tableColumns>'
  table_style_info.to_xml_string(str)
  str << '</table>'
end