Class: DocXify::Element::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/docxify/element/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, options = {}) ⇒ Table

Returns a new instance of Table.



24
25
26
27
28
# File 'lib/docxify/element/table.rb', line 24

def initialize(rows, options = {})
  super()
  @document = options[:document]
  @rows = rows
end

Instance Attribute Details

#column_widthsObject

Returns the value of attribute column_widths.



22
23
24
# File 'lib/docxify/element/table.rb', line 22

def column_widths
  @column_widths
end

#rowsObject

Returns the value of attribute rows.



22
23
24
# File 'lib/docxify/element/table.rb', line 22

def rows
  @rows
end

Instance Method Details

#table_element_endObject



102
103
104
# File 'lib/docxify/element/table.rb', line 102

def table_element_end
  "</w:tbl>"
end

#table_element_startObject



42
43
44
# File 'lib/docxify/element/table.rb', line 42

def table_element_start
  "<w:tbl>"
end

#table_gridObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/docxify/element/table.rb', line 62

def table_grid
  default_equal_width = (@document.width / @rows.first.size).to_i

  @column_widths = []
  xml = "<w:tblGrid>"
  @rows.first.each do |cell|
    width = if cell.width_cm
      DocXify.cm2dxa(cell.width_cm)
    else
      default_equal_width
    end
    xml << "<w:gridCol w:w=\"#{width}\"/>"
    @column_widths << width
  end

  xml << "</w:tblGrid>"
  xml
end

#table_propertiesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/docxify/element/table.rb', line 46

def table_properties
  xml = ""
  xml << "<w:tblPr>"
  xml << '<w:tblStyle w:val="TableGrid"/>'
  xml << '<w:tblW w:type="auto" w:w="0"/>'
  xml << "<w:tblBorders>"
  xml << '<w:top w:color="auto" w:space="0" w:sz="0" w:val="none"/>'
  xml << '<w:left w:color="auto" w:space="0" w:sz="0" w:val="none"/>'
  xml << '<w:bottom w:color="auto" w:space="0" w:sz="0" w:val="none"/>'
  xml << '<w:right w:color="auto" w:space="0" w:sz="0" w:val="none"/>'
  xml << "</w:tblBorders>"
  xml << '<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>'
  xml << "</w:tblPr>"
  xml
end

#table_row(row) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/docxify/element/table.rb', line 81

def table_row(row)
  xml = "<w:tr>"
  widths = @column_widths.dup
  row.each do |cell|
    if cell.nil?
      cell = DocXify::Element::TableCell.new(nil)
    end

    if cell.width_cm.nil?
      cell.width_cm = 0
      (cell.colspan || 1).times do
        cell.width_cm += DocXify.dxa2cm(widths.shift)
      end
    end

    xml << cell.to_s
  end
  xml << "</w:tr>"
  xml
end

#to_s(_container = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/docxify/element/table.rb', line 30

def to_s(_container = nil)
  xml = ""
  xml << table_element_start
  xml << table_properties
  xml << table_grid
  @rows.each do |row|
    xml << table_row(row)
  end
  xml << table_element_end
  xml
end