Class: RobustExcelOle::ListRow
- Inherits:
-
VbaObjects
- Object
- Base
- VbaObjects
- RobustExcelOle::ListRow
- Defined in:
- lib/robust_excel_ole/list_row.rb
Instance Attribute Summary collapse
-
#ole_tablerow ⇒ Object
readonly
Returns the value of attribute ole_tablerow.
Instance Method Summary collapse
- #==(other_listrow) ⇒ Object
-
#[](column_number_or_name) ⇒ Variant
returns the value of the cell with given column name or number.
-
#[]=(column_number_or_name, value) ⇒ Object
sets the value of the cell with given column name or number.
-
#alive? ⇒ Boolean
returns true, if the listrow reacts to methods, false otherwise.
-
#delete_values ⇒ Object
deletes the values of the row.
-
#initialize(rownumber_or_oletablerow) ⇒ ListRow
constructor
A new instance of ListRow.
-
#keys_values ⇒ Hash
key-value pairs of the row.
-
#values ⇒ Array
values of the row.
-
#values=(values) ⇒ Object
sets the values of the row.
Methods inherited from VbaObjects
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(meth_name, *args) ⇒ Object (private)
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/robust_excel_ole/list_row.rb', line 175 def method_missing(meth_name, *args) # this should not happen: raise(TableRowError, "internal error: ole_table not defined") unless self.class.method_defined?(:ole_table) if respond_to?(meth_name) core_name = meth_name.to_s.chomp('=') column_name = column_names.find{ |c| valid_similar_names(c).include?(core_name) } define_and_call_method(column_name, meth_name, *args) if column_name else super(meth_name, *args) end end |
Instance Attribute Details
#ole_tablerow ⇒ Object (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
97 98 99 |
# File 'lib/robust_excel_ole/list_row.rb', line 97 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/robust_excel_ole/list_row.rb', line 29 def [] column_number_or_name column_number_or_name = column_number_or_name.to_s if column_number_or_name.is_a?(Symbol) 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
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/robust_excel_ole/list_row.rb', line 42 def []=(column_number_or_name, value) begin column_number_or_name = column_number_or_name.to_s if column_number_or_name.is_a?(Symbol) 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 |
#alive? ⇒ Boolean
returns true, if the listrow reacts to methods, false otherwise
127 128 129 130 131 132 133 |
# File 'lib/robust_excel_ole/list_row.rb', line 127 def alive? @ole_tablerow.Parent true rescue @ole_tablerow = nil # dead object won't be alive again false end |
#delete_values ⇒ Object
deletes the values of the row
90 91 92 93 94 95 |
# File 'lib/robust_excel_ole/list_row.rb', line 90 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_values ⇒ Hash
key-value pairs of the row
81 82 83 |
# File 'lib/robust_excel_ole/list_row.rb', line 81 def keys_values ole_table.column_names.zip(values).to_h end |
#values ⇒ Array
values of the row
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/robust_excel_ole/list_row.rb', line 55 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
70 71 72 73 74 75 76 77 |
# File 'lib/robust_excel_ole/list_row.rb', line 70 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 |