Class: RobustExcelOle::ListRow

Inherits:
VbaObjects show all
Defined in:
lib/robust_excel_ole/list_row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from VbaObjects

#to_reo

Constructor Details

#initialize(rownumber_or_oletablerow) ⇒ ListRow

Returns a new instance of ListRow.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/robust_excel_ole/list_row.rb', line 13

def initialize(rownumber_or_oletablerow)
  @ole_tablerow = if rownumber_or_oletablerow.is_a?(ListRow)
    rownumber_or_oletablerow.ole_tablerow
  else
    begin
      rownumber_or_oletablerow.Parent.send(:ListRows)
      rownumber_or_oletablerow
    rescue
      ole_table.ListRows.Item(rownumber_or_oletablerow)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:

  • (TableRowError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/robust_excel_ole/list_row.rb', line 99

def method_missing(name, *args)
  # this should not happen:
  raise(TableRowError, "internal error: ole_table not defined") unless self.class.method_defined?(:ole_table)
  name_str = name.to_s
  core_name = name_str.chomp('=')
  column_names = ole_table.HeaderRowRange.Value.first
  column_name = column_names.find do |c|
    c == core_name ||
    c.gsub(/\W/,'_') == core_name ||
    c.underscore == core_name ||
    c.underscore.gsub(/\W/,'_') == core_name ||
    c.replace_umlauts.gsub(/\W/,'_') == core_name ||
    c.replace_umlauts.underscore.gsub(/\W/,'_') == core_name 
  end         
  if column_name
    define_and_call_method(column_name, name, *args)
  else
    super(name, *args)
  end
end

Instance Attribute Details

#ole_tablerowObject (readonly)

Returns the value of attribute ole_tablerow



9
10
11
# File 'lib/robust_excel_ole/list_row.rb', line 9

def ole_tablerow
  @ole_tablerow
end

Instance Method Details

#==(other_listrow) ⇒ Object



95
96
97
# File 'lib/robust_excel_ole/list_row.rb', line 95

def == other_listrow
  other_listrow.is_a?(ListRow) && other_listrow.values == self.values
end

#[](column_number_or_name) ⇒ Variant

returns the value of the cell with given column name or number

Parameters:

  • column (Variant)

    number or column name

Returns:

  • (Variant)

    value of the cell

Raises:

  • (TableRowError)


29
30
31
32
33
34
35
36
# File 'lib/robust_excel_ole/list_row.rb', line 29

def [] column_number_or_name
  ole_cell = ole_table.Application.Intersect(
    @ole_tablerow.Range, ole_table.ListColumns.Item(column_number_or_name).Range)
  value = ole_cell.Value
  value.respond_to?(:gsub) ? value.encode('utf-8') : value
rescue WIN32OLERuntimeError
  raise TableRowError, "could not determine the value at column #{column_number_or_name}\n#{$!.message}"
end

#[]=(column_number_or_name, value) ⇒ Object

sets the value of the cell with given column name or number

Parameters:

  • column (Variant)

    number or column name

  • value (Variant)

    of the cell



41
42
43
44
45
46
47
48
49
# File 'lib/robust_excel_ole/list_row.rb', line 41

def []=(column_number_or_name, value)
  begin
    ole_cell = ole_table.Application.Intersect(
      @ole_tablerow.Range, ole_table.ListColumns.Item(column_number_or_name).Range)
    ole_cell.Value = value
  rescue WIN32OLERuntimeError
    raise TableRowError, "could not assign value #{value.inspect} to cell at column #{column_number_or_name}\n#{$!.message}"
  end
end

#delete_valuesObject

deletes the values of the row

Raises:

  • (TableError)


88
89
90
91
92
93
# File 'lib/robust_excel_ole/list_row.rb', line 88

def delete_values
  @ole_tablerow.Range.Value = [[].fill(nil,0..(ole_table.ListColumns.Count)-1)]
  nil
rescue WIN32OLERuntimeError
  raise TableError, "could not delete values\n#{$!.message}"
end

#keys_valuesHash

key-value pairs of the row

Returns:

  • (Hash)

    key-value pairs of the row



79
80
81
# File 'lib/robust_excel_ole/list_row.rb', line 79

def keys_values
  ole_table.column_names.zip(values).to_h
end

#valuesArray

values of the row

Returns:

  • (Array)

    values of the row

Raises:

  • (TableError)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/robust_excel_ole/list_row.rb', line 53

def values
  value = @ole_tablerow.Range.Value
  return value if value==[nil]
  value = if !value.respond_to?(:pop)
    [value]
  elsif value.first.respond_to?(:pop)
    value.first
  end
  value.map{|v| v.respond_to?(:gsub) ? v.encode('utf-8') : v}
rescue WIN32OLERuntimeError
  raise TableError, "could not read values\n#{$!.message}"
end

#values=(values) ⇒ Object

sets the values of the row

Parameters:

  • values (Array)

    of the row

Raises:

  • (TableError)


68
69
70
71
72
73
74
75
# File 'lib/robust_excel_ole/list_row.rb', line 68

def values= values
  updated_values = self.values
  updated_values[0,values.length] = values
  @ole_tablerow.Range.Value = [updated_values]
  values
rescue WIN32OLERuntimeError
  raise TableError, "could not set values #{values.inspect}\n#{$!.message}"
end