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


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

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)

32
33
34
# File 'lib/axlsx/workbook/worksheet/table.rb', line 32

def name
  @name
end

#refString (readonly)

The reference to the table data

Returns:

  • (String)

28
29
30
# File 'lib/axlsx/workbook/worksheet/table.rb', line 28

def ref
  @ref
end

#styleTableStyle (readonly)

The style for the table.

Returns:


36
37
38
# File 'lib/axlsx/workbook/worksheet/table.rb', line 36

def style
  @style
end

Instance Method Details

#indexInteger

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)

40
41
42
# File 'lib/axlsx/workbook/worksheet/table.rb', line 40

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

#pnString

The part name for this table

Returns:

  • (String)

46
47
48
# File 'lib/axlsx/workbook/worksheet/table.rb', line 46

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

#rIdString

The relationship id for this table.

Returns:

  • (String)

See Also:


53
54
55
# File 'lib/axlsx/workbook/worksheet/table.rb', line 53

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

#table_style_infoObject

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


69
70
71
# File 'lib/axlsx/workbook/worksheet/table.rb', line 69

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)

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

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.value << '"/>')
  end
  str << '</tableColumns>'
  table_style_info.to_xml_string(str)
  str << '</table>'
end