Class: RubyExcel::Row

Inherits:
Section show all
Defined in:
lib/rubyexcel/section.rb

Overview

A Row in the Sheet

Instance Attribute Summary collapse

Attributes inherited from Section

#data, #sheet

Instance Method Summary collapse

Methods inherited from Section

#cell, #delete, #each, #each_cell, #each_cell_without_headers, #each_without_headers, #empty?, #find, #inspect, #last, #last_cell, #map!, #map_without_headers!, #read, #summarise, #to_s, #write

Methods included from Address

#address_to_col_index, #address_to_indices, #col_index, #col_letter, #column_id, #expand, #indices_to_address, #multi_array?, #offset, #row_id, #to_range_address

Constructor Details

#initialize(sheet, idx) ⇒ Row

Creates a RubyExcel::Row instance

Parameters:

  • sheet (RubyExcel::Sheet)

    the Sheet which holds this Row

  • idx (Fixnum)

    the index of this Row



212
213
214
215
# File 'lib/rubyexcel/section.rb', line 212

def initialize( sheet, idx )
  @idx = idx.to_i
  super( sheet )
end

Instance Attribute Details

#idxObject (readonly) Also known as: index

The Row number



199
200
201
# File 'lib/rubyexcel/section.rb', line 199

def idx
  @idx
end

#lengthObject (readonly)

The number of Columns in the Row



199
200
201
# File 'lib/rubyexcel/section.rb', line 199

def length
  @length
end

Instance Method Details

#<<(value) ⇒ Object

Note:

This only adds an extra cell if it is the first Row This prevents a loop through Rows from extending diagonally away from the main data.

Append a value to the Row.

Parameters:

  • value (Object)

    the object to append



225
226
227
# File 'lib/rubyexcel/section.rb', line 225

def <<( value )
  data[ translate_address( idx == 1 ? data.cols + 1 : data.cols ) ] = value
end

#cell_by_header(header) ⇒ RubyExcel::Cell Also known as: cell_h

Access a Cell by its header

Parameters:

  • header (String)

    the header to search for

Returns:



236
237
238
# File 'lib/rubyexcel/section.rb', line 236

def cell_by_header( header )
  cell( getref( header ) )
end

#getref(header) ⇒ String

Find the Address of a header

Parameters:

  • header (String)

    the header to search for

Returns:

  • (String)

    the address of the header



248
249
250
251
252
253
254
# File 'lib/rubyexcel/section.rb', line 248

def getref( header )
  sheet.header_rows.times do |t|
    res = sheet.row( t + 1 ).find { |v| v == header }
    return column_id( res ) if res
  end
  fail ArgumentError, 'Invalid header: ' + header.to_s
end

#set_value_by_header(header, val) ⇒ Object Also known as: set_val

Set a value in this Row by its header

Parameters:

  • header (String)

    the header to search for

  • val (Object)

    the value to write



283
284
285
# File 'lib/rubyexcel/section.rb', line 283

def set_value_by_header( header, val )
  self[ getref( header ) ] = val
end

#value_by_header(header) ⇒ Object Also known as: val

Find a value in this Row by its header

Parameters:

  • header (String)

    the header to search for

Returns:

  • (Object)

    the value at the address



271
272
273
# File 'lib/rubyexcel/section.rb', line 271

def value_by_header( header )
  self[ getref( header ) ]
end