Class: Tabula::Cell

Inherits:
ZoneEntity
  • Object
show all
Defined in:
lib/tabula/entities/cell.rb

Overview

cells are components of spreadsheets

Constant Summary collapse

NORMAL =
0
DEBUG =
1
SUPERDEBUG =
2

Instance Attribute Summary collapse

Attributes inherited from ZoneEntity

#texts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZoneEntity

#<=>, #inspect, #merge!, #points, #tlbr, #tlwh

Constructor Details

#initialize(top, left, width, height, options = {}) ⇒ Cell

Returns a new instance of Cell.



13
14
15
16
17
18
19
# File 'lib/tabula/entities/cell.rb', line 13

def initialize(top, left, width, height, options={})
  super(top, left, width, height)
  @placeholder = false
  @spanning = false
  @text_elements = []
  @options = ({:use_line_returns => true, :cell_debug => NORMAL}).merge options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/tabula/entities/cell.rb', line 11

def options
  @options
end

#placeholderObject

Returns the value of attribute placeholder.



11
12
13
# File 'lib/tabula/entities/cell.rb', line 11

def placeholder
  @placeholder
end

#spanningObject

Returns the value of attribute spanning.



11
12
13
# File 'lib/tabula/entities/cell.rb', line 11

def spanning
  @spanning
end

#text_elementsObject

Returns the value of attribute text_elements.



11
12
13
# File 'lib/tabula/entities/cell.rb', line 11

def text_elements
  @text_elements
end

Class Method Details

.new_from_points(topleft, bottomright, options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/tabula/entities/cell.rb', line 21

def self.new_from_points(topleft, bottomright, options={})
  width = bottomright.x - topleft.x
  height = bottomright.y - topleft.y
  Cell.new(topleft.y, topleft.x, width, height, options)
end

Instance Method Details

#textObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tabula/entities/cell.rb', line 27

def text
  return "placeholder" if @placeholder && @options[:cell_debug] >= DEBUG
  output = ""
  text_elements.sort #use the default sort for ZoneEntity
  text_elements.group_by(&:top).values.each do |row|
    output << row.map{|el| el.text}.join('') + (@options[:use_line_returns] ? "\r" : '')
    # per @bchartoff, https://github.com/jazzido/tabula-extractor/pull/65#issuecomment-32899336
    # line returns as \r behave better in Excel.
  end
  if (output.empty? && @options[:cell_debug] >= DEBUG) || @options[:cell_debug] >= SUPERDEBUG
    text_output = output.dup
    output = "top: #{top} left: #{left} \n w: #{width} h: #{height}"
    output += " \n #{text_output}"
  end
  output.strip
end

#to_json(*a) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/tabula/entities/cell.rb', line 44

def to_json(*a)
  {
    'json_class'   => self.class.name,
    'text' => text,
    'top' => top,
    'left' => left,
    'width' => width,
    'height' => height
  }.to_json(*a)
end