Class: RubyDocx::Elements::Table

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/table.rb

Instance Attribute Summary

Attributes inherited from Element

#doc, #grid, #node, #style

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_xml

Constructor Details

This class inherits a constructor from RubyDocx::Elements::Element

Instance Method Details

#to_htmlObject



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
# File 'lib/ruby_docx/elements/table.rb', line 19

def to_html
  s = "<table>"

  self.trs.each_with_index do |tr, i|
    if self.grid
      w = self.grid.width_of(i).to_i
    end

    s += "<tr>"

    tr.cells.map do |cell|
      if w.to_i > 0
        s += "<td style='width: #{(w/100.0).round(2)}%'>#{cell.to_html}</td>"
      else
        s += "<td>#{cell.to_html}</td>"
      end
    end

    s += "</tr>"
  end

  s += "</table>"

  s
end

#to_sObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_docx/elements/table.rb', line 4

def to_s
  s = "\n"
  self.trs.map do |tr|
    tr.cells.map do |cell|
      s += "#{cell.to_s}\t"
    end

    s += "\n"
  end

  s += "\n"

  s
end

#trsObject



46
47
48
# File 'lib/ruby_docx/elements/table.rb', line 46

def trs
  self.elements
end