Class: Excel::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/rexcel/row.rb

Overview

A Row in the spreadsheet.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Row

Define a new row.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rexcel/row.rb', line 9

def initialize( options = {})
  @log = options[:log] || LOGGER
  @columns = []
  options.each{|key,value|
    case key
      when :log
      when :style
        @style = value
        raise ArgumentError, "Style is no Excel::Style" unless @style.is_a?(Style)
      else
        @log.warn("Excel::Row: undefined option #{option}")
    end
  }
end

Instance Attribute Details

#columnsObject (readonly)

Array with columns of this row



24
25
26
# File 'lib/rexcel/row.rb', line 24

def columns
  @columns
end

#styleObject (readonly)

Style for the row. Is inherited to cells.



26
27
28
# File 'lib/rexcel/row.rb', line 26

def style
  @style
end

Instance Method Details

#<<(insertion) ⇒ Object

Add content to the Row.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rexcel/row.rb', line 30

def << (insertion)
  case insertion
    when Cell
      @columns << insertion
    when Array
      insertion.each{|value|
        @columns << Cell.new(value)
      }
    when Hash
      @log.error("Excel::Row: Hashs not supported")
      raise ArgumentError, "Excel::Row#<<: Hashs not supported"
      #fixme: if connectuion to worksheet, use columns.
    when Row, Worksheet, Workbook
      raise ArgumentError, "Excel::Row#<<: #{insertion.class} not supported"
    else
      @columns << Cell.new(insertion)
    end
    self  #for usage like " << (Excel::Row.new()  << 'a')"
end

#inspectObject

build_excel_csv



93
94
95
# File 'lib/rexcel/row.rb', line 93

def inspect
  "#<Excel::Row:#{object_id} #{columns.size} Columns>"
end

#to_csv(options = {}) ⇒ Object

Build the row for csv.

Details on options see Rexcel::Worksheet#to_csv



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rexcel/row.rb', line 76

def to_csv(options = {})
  
  @log.debug("Prepare csv-row")
  #Set options defaults
  options[:sep] ||= ";"
  
  csv_row = []
  self.columns.each_with_index{|col, colnum|
    @log.debug("Prepare cell #{colnum} for csv-output") if @log.debug?
    csv_row << col.to_csv()
    raise ArgumentError, "Cell separator <#{options[:sep]}> is part of the data #{csv_row.last.inspect}" if csv_row.last.to_s.include?(options[:sep])
    
  }#columns in row

  csv_row.join(options[:sep])
end

#to_xml(xmlbuilder, ns) ⇒ Object

Build the xml a work sheet row,

ns must be a method-object to implement the namespace definitions.

Format options (bold, italic, colors) are forwarded to cells.

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rexcel/row.rb', line 56

def to_xml(xmlbuilder, ns)
  raise EmptyError, "Row without content" if @columns.empty?
  
  #Build options
  row_options = {}
  if @style
    row_options[ns.call('StyleID')] = @style.style_id 
  end
  
  xmlbuilder[ns.call].Row( row_options ){
    @columns.each{|column|
        column.to_xml(xmlbuilder, ns, self)      
    }
  }
end