Class: Spreadsheet::Row

Inherits:
Array
  • Object
show all
Includes:
Datatypes
Defined in:
lib/spreadsheet/row.rb

Overview

The Row class. Encapsulates Cell data and formatting. Since Row is a subclass of Array, you may use all the standard Array methods to manipulate a Row. By convention, Row#at will give you raw values, while Row#[] may be overridden to return enriched data if necessary (see also the Date- and DateTime-handling in Excel::Row#[]

Useful Attributes are:

#idx

The 0-based index of this Row in its Worksheet.

#formats

A parallel array containing Formatting information for all cells stored in a Row.

#default_format

The default Format used when writing a Cell if no explicit Format is stored in #formats for the cell.

#height

The height of this Row in points (defaults to 12).

Direct Known Subclasses

Excel::Row

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Datatypes

append_features

Methods included from Compatibility

#ivar_name, #method_name

Methods inherited from Array

#rcompact, #rcompact!

Constructor Details

#initialize(worksheet, idx, cells = []) ⇒ Row

Returns a new instance of Row.



59
60
61
62
63
64
65
66
# File 'lib/spreadsheet/row.rb', line 59

def initialize worksheet, idx, cells=[]
  @default_format = nil
  @worksheet = worksheet
  @idx = idx
  super cells
  @formats = []
  @height = 12.1
end

Instance Attribute Details

#default_formatObject

Returns the value of attribute default_format.



51
52
53
# File 'lib/spreadsheet/row.rb', line 51

def default_format
  @default_format
end

#formatsObject (readonly)

Returns the value of attribute formats.



51
52
53
# File 'lib/spreadsheet/row.rb', line 51

def formats
  @formats
end

#heightObject

Returns the value of attribute height.



52
53
54
# File 'lib/spreadsheet/row.rb', line 52

def height
  @height
end

#idxObject

Returns the value of attribute idx.



52
53
54
# File 'lib/spreadsheet/row.rb', line 52

def idx
  @idx
end

#worksheetObject

Returns the value of attribute worksheet.



52
53
54
# File 'lib/spreadsheet/row.rb', line 52

def worksheet
  @worksheet
end

Class Method Details

.format_updater(*keys) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spreadsheet/row.rb', line 22

def format_updater *keys
  keys.each do |key|
    unless instance_methods.include? "unupdated_#{key}="
      alias_method :"unupdated_#{key}=", :"#{key}="
      define_method "#{key}=" do |value|
        send "unupdated_#{key}=", value
        @worksheet.row_updated @idx, self if @worksheet
        value
      end
    end
  end
end

.updater(*keys) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spreadsheet/row.rb', line 34

def updater *keys
  keys.each do |key|
    ## Passing blocks to methods defined with define_method is not possible
    #  in Ruby 1.8:
    #  http://groups.google.com/group/ruby-talk-google/msg/778184912b769e5f
    #  use class_eval as suggested by someone else in
    #  http://rubyforge.org/tracker/index.php?func=detail&aid=25732&group_id=678&atid=2677
    class_eval <<-SRC, __FILE__, __LINE__
      def #{key}(*args)
        res = super(*args)
        @worksheet.row_updated @idx, self if @worksheet
        res
      end
    SRC
  end
end

Instance Method Details

#first_usedObject

#first_used the 0-based index of the first non-blank Cell.



77
78
79
# File 'lib/spreadsheet/row.rb', line 77

def first_used
  [ index_of_first(self), index_of_first(@formats) ].compact.min
end

#format(idx) ⇒ Object

The Format for the Cell at idx (0-based), or the first valid Format in Row#default_format, Column#default_format and Worksheet#default_format.



83
84
85
86
# File 'lib/spreadsheet/row.rb', line 83

def format idx
  @formats[idx] || @default_format \
    || @worksheet.column(idx).default_format if @worksheet
end

#formattedObject

Returns a copy of self with nil-values appended for empty cells that have an associated Format. This is primarily a helper-function for the writer classes.



91
92
93
94
95
96
97
98
# File 'lib/spreadsheet/row.rb', line 91

def formatted
  copy = dup
  @formats.rcompact!
  if copy.length < @formats.size
    copy.concat Array.new(@formats.size - copy.length)
  end
  copy
end

#formatted_sizeObject Also known as: first_unused

Same as Row#size, but takes into account formatted empty cells



101
102
103
104
105
106
# File 'lib/spreadsheet/row.rb', line 101

def formatted_size
  @formats.rcompact!
  sz = size
  fs = @formats.size
  fs > sz ? fs : sz
end

#inspectObject



111
112
113
114
115
116
# File 'lib/spreadsheet/row.rb', line 111

def inspect
  variables = instance_variables.collect do |name|
    "%s=%s" % [name, instance_variable_get(name)]
  end.join(' ')
  sprintf "#<%s:0x%014x %s %s>", self.class, object_id, variables, super
end

#set_format(idx, fmt) ⇒ Object

Set the Format for the Cell at idx (0-based).



119
120
121
122
123
124
# File 'lib/spreadsheet/row.rb', line 119

def set_format idx, fmt
  @formats[idx] = fmt
  @worksheet.add_format fmt
  @worksheet.row_updated @idx, self if @worksheet
  fmt
end