Class: DocXify::Element::TableCell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, options = {}) ⇒ TableCell

Returns a new instance of TableCell.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/docxify/element/table_cell.rb', line 6

def initialize(content, options = {})
  super()
  @content = content
  @valign = options[:valign] || :top
  @align = options[:align] || :left
  @nowrap = options[:nowrap]
  @colspan = options[:colspan]
  @rowspan = options[:rowspan]
  @width_cm = options[:width_cm]
  @font = options[:font]
  @size = options[:size]
  @color = options[:color]
  @borders = options[:borders]&.map(&:to_sym) || []
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def align
  @align
end

#bordersObject

Returns the value of attribute borders.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def borders
  @borders
end

#colourObject

Returns the value of attribute colour.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def colour
  @colour
end

#colspanObject

Returns the value of attribute colspan.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def colspan
  @colspan
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def content
  @content
end

#fontObject

Returns the value of attribute font.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def font
  @font
end

#nowrapObject

Returns the value of attribute nowrap.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def nowrap
  @nowrap
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def size
  @size
end

#valignObject

Returns the value of attribute valign.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def valign
  @valign
end

#width_cmObject

Returns the value of attribute width_cm.



4
5
6
# File 'lib/docxify/element/table_cell.rb', line 4

def width_cm
  @width_cm
end

Instance Method Details

#to_sObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/docxify/element/table_cell.rb', line 21

def to_s
  xml = "<w:tc>"
  xml << "<w:tcPr>"
  xml << %Q(<w:tcW w:type="dxa" w:w="#{DocXify.cm2dxa(@width_cm)}"/>)
  if !@colspan.nil? && @colspan.to_i > 1
    xml << %Q(<w:gridSpan w:val="#{@colspan}"/>')
  end

  if borders.any?
    xml << "<w:tcBorders>"
    xml << (borders.include?(:top) ? '<w:top w:color="auto" w:space="0" w:sz="4" w:val="single"/>' : '<w:top w:val="nil"/>')
    xml << (borders.include?(:bottom) ? '<w:bottom w:color="auto" w:space="0" w:sz="4" w:val="single"/>' : '<w:bottom w:val="nil"/>')
    xml << (borders.include?(:left) ? '<w:left w:color="auto" w:space="0" w:sz="4" w:val="single"/>' : '<w:left w:val="nil"/>')
    xml << (borders.include?(:right) ? '<w:right w:color="auto" w:space="0" w:sz="4" w:val="single"/>' : '<w:right w:val="nil"/>')
    xml << "</w:tcBorders>"
  end

  if @valign != :top
    xml << %Q(<w:vAlign w:val="#{@valign}"/>)
  end

  if @nowrap
    xml << "<w:noWrap/>"
  end

  if @content.nil?
    @content = "" # Nil is useful for rowspan, but we need to output something or Word breaks
  end

  if @rowspan
    xml << '<w:vMerge w:val="restart"/>'
  elsif @content == ""
    xml << "<w:vMerge/>"
  end

  xml << "</w:tcPr>"

  xml << DocXify::Element::Paragraph.new(@content, document: @document, font: @font, size: @size, color: @color, align: @align).to_s

  xml << "</w:tc>"

  xml
end