Class: RubyFromExcel::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/excelfile/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, name, reference, column_name_array, number_of_total_rows) ⇒ Table

Returns a new instance of Table.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/excelfile/table.rb', line 34

def initialize(worksheet,name,reference,column_name_array,number_of_total_rows)
  @worksheet = worksheet
  @name = name
  @reference = reference
  @column_name_array = column_name_array
  @number_of_total_rows = number_of_total_rows
  @all = Area.new(worksheet,*reference.split(':'))
  index_of_first_total_row = -number_of_total_rows.to_i
  @data = @all.rows(1,index_of_first_total_row - 1)
  @headers = @all.row(0)
  @totals = @all.rows(index_of_first_total_row,-1)
  @column_names = {}
  column_name_array.each_with_index do |name,index|
    @column_names[name.strip.downcase] = index # column['id'].to_i
  end
  Table.add(self)
  if $DEBUG
    RubyFromExcel.debug(:tables,"#{worksheet.name}.#{name} -> Table #{reference.inspect},#{column_name_array.inspect},#{number_of_total_rows}")
  end
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def all
  @all
end

#column_name_arrayObject (readonly)

Returns the value of attribute column_name_array.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def column_name_array
  @column_name_array
end

#column_namesObject (readonly)

Returns the value of attribute column_names.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def column_names
  @column_names
end

#dataObject (readonly)

Returns the value of attribute data.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def name
  @name
end

#number_of_total_rowsObject (readonly)

Returns the value of attribute number_of_total_rows.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def number_of_total_rows
  @number_of_total_rows
end

#referenceObject (readonly)

Returns the value of attribute reference.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def reference
  @reference
end

#totalsObject (readonly)

Returns the value of attribute totals.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def totals
  @totals
end

#worksheetObject (readonly)

Returns the value of attribute worksheet.



28
29
30
# File 'lib/excelfile/table.rb', line 28

def worksheet
  @worksheet
end

Class Method Details

.add(table) ⇒ Object



24
25
26
# File 'lib/excelfile/table.rb', line 24

def self.add(table)
  tables[table.name.downcase] = table
end

.from_xml(worksheet, xml) ⇒ Object



30
31
32
# File 'lib/excelfile/table.rb', line 30

def self.from_xml(worksheet,xml)
  Table.new(worksheet,xml['displayName'],xml['ref'],xml.css('tableColumn').map {|c| c['name']}.to_a,xml['totalsRowCount'])
end

.reference_for(table_name, structured_reference, cell_making_the_reference = nil) ⇒ Object



8
9
10
11
12
# File 'lib/excelfile/table.rb', line 8

def self.reference_for(table_name,structured_reference,cell_making_the_reference = nil)
  # puts "'#{table_name.downcase}' not found in #{tables.keys}" unless tables.include?(table_name.downcase)
  return ":ref" unless tables.include?(table_name.downcase)
  tables[table_name.downcase].reference_for(structured_reference,cell_making_the_reference)
end

.reference_for_local_reference(cell, structured_reference) ⇒ Object



14
15
16
17
18
# File 'lib/excelfile/table.rb', line 14

def self.reference_for_local_reference(cell,structured_reference)
  table = tables.find { |n,t| t.include?(cell) }
  return ":ref" unless table
  table.last.reference_for(structured_reference,cell.reference)
end

.tablesObject



20
21
22
# File 'lib/excelfile/table.rb', line 20

def self.tables
  @@tables ||= {}
end

Instance Method Details

#column(name) ⇒ Object



55
56
57
58
59
# File 'lib/excelfile/table.rb', line 55

def column(name)
  name = $1 if name.to_s =~ /^(\d+)\.0+$/
  return :na unless column = column_names[name.to_s.downcase]
  data.column(column)
end

#include?(cell) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/excelfile/table.rb', line 94

def include?(cell)
  @all.include_cell?(cell)
end

#inspectObject



90
91
92
# File 'lib/excelfile/table.rb', line 90

def inspect
  %Q{'Table.new(#{worksheet},#{name.inspect},#{reference.inspect},#{column_name_array.inspect},#{number_of_total_rows})'}
end

#reference_for(structured_reference, cell_making_the_reference = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/excelfile/table.rb', line 61

def reference_for(structured_reference,cell_making_the_reference = nil)
  structured_reference.strip!
  return this_row(cell_making_the_reference) if structured_reference == '#This Row'
  if structured_reference =~ /\[(.*?)\],\[(.*?)\]:\[(.*?)\]/
    return this_row_area_intersection(cell_making_the_reference,area_for_simple_reference($2),area_for_simple_reference($3)) if $1 == '#This Row'
    # return all.column(column_names[$2]) if $1 == '#All'
    # return intersection(area_for_simple_reference($1),area_for_simple_reference($2))
  elsif structured_reference =~ /\[(.*?)\],\[(.*?)\]/
    return this_row_intersection(cell_making_the_reference,area_for_simple_reference($2)) if $1 == '#This Row'
    return all.column(column_names[$2.to_s.downcase]) if $1 == '#All'
    return intersection(area_for_simple_reference($1),area_for_simple_reference($2))
  end
  if cell_making_the_reference
    if cell_making_the_reference.worksheet == self.worksheet
      if data.include?(cell_making_the_reference)
        case structured_reference
        when /#totals/i, /#headers/i
          return this_column_intersection(cell_making_the_reference,area_for_simple_reference(structured_reference))
        when /^#/i
          return area_for_simple_reference(structured_reference) 
        else
          return this_row_intersection(cell_making_the_reference,area_for_simple_reference(structured_reference))
        end
      end
    end
  end
  return area_for_simple_reference(structured_reference) 
end