Class: Excel::Cell
- Inherits:
-
Object
- Object
- Excel::Cell
- Defined in:
- lib/rexcel/cell.rb
Overview
Cell of a Worksheet.
A cell is part of a Row.
Cells define
-
Type . The type is defined by the type of the content.
-
String
-
Number
-
DateTime (not supported/tested yet)
-
-
Style (Reference to Excel::Style instance)
Constant Summary collapse
- TYPES =
[ nil, # :string, ]
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Content of the cell.
-
#style ⇒ Object
readonly
Style.
-
#type ⇒ Object
readonly
Target type for Excel.
Instance Method Summary collapse
-
#initialize(content = nil, options = {}) ⇒ Cell
constructor
Define a cell and add the content.
-
#to_csv ⇒ Object
Fill an CSV-Field.
-
#to_xls(cell, row = nil) ⇒ Object
Fill an Excel field.
-
#to_xml(xmlbuilder, ns, row = nil) ⇒ Object
Build the xml a work sheet row,.
Constructor Details
#initialize(content = nil, options = {}) ⇒ Cell
Define a cell and add the content.
Options:
-
:string [true/false]
-
:log: Replacement for the default Logger.
-
:style see Excel::Style
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rexcel/cell.rb', line 29 def initialize( content = nil, = {}) @log = [:log] || LOGGER .each{|key,value| case key when :log when :string if value @type = 'String' content = content.to_s end when :style @style = value raise ArgumentError, "Style is no Excel::Style" unless @style.is_a?(Style) else @log.warn("Excel::Cell: undefined option #{}") end } #Convert data to utf-8 #Without this, you get problems, when you mix encodings. #~ @content = content @content = content.respond_to?(:encode) ? content.encode('utf-8') : content @type = get_excel_type() unless @type end |
Instance Attribute Details
#content ⇒ Object (readonly)
Content of the cell. Strings are converted to utf-8.
53 54 55 |
# File 'lib/rexcel/cell.rb', line 53 def content @content end |
#style ⇒ Object (readonly)
Style
57 58 59 |
# File 'lib/rexcel/cell.rb', line 57 def style @style end |
#type ⇒ Object (readonly)
Target type for Excel
55 56 57 |
# File 'lib/rexcel/cell.rb', line 55 def type @type end |
Instance Method Details
#to_csv ⇒ Object
Fill an CSV-Field.
The output is set depending on cell content. The csv-value is optimized to be used by Excel.
Examples for conversion:
-
1.1 -> =‘1.1’ (1.1 would become 1st January)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rexcel/cell.rb', line 144 def to_csv() cellcontent = @content case @type when 'String' #Suppress automatic conversion from Excel cellcontent = "\"#{@content}\"" if @content =~ /^\A[\s\d]/ when 'Number' #~ cellcontent = "%.2f" % cellcontent if cellcontent.is_a?(Float) #http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#CSVAndExcel cellcontent = "=\"#{cellcontent}\"" if cellcontent.is_a?(Float) when 'DateTime' @log.warn("Cell type DateTime not tested") #avoid interpretation in excel, e.g. dates. does not work with numbers. cellcontent = " #{cellcontent}" when nil #no conversion cellcontent = @content else raise ArgumentError, "#{self.class}##{__method__}: Undefined CSV type #{@type}" end cellcontent end |
#to_xls(cell, row = nil) ⇒ Object
Fill an Excel field. Receives a OLE-cell and optional a row.
-
value is added
-
format is set (depending on cell and row format).
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rexcel/cell.rb', line 108 def to_xls(cell, row = nil) case @type when 'String' #Suppress automatic conversion from Excel cell.NumberFormat = "@" #as String cell.Value = @content.to_s when 'Number' cell.Value = @content when 'DateTime' @log.warn("Cell type DateTime not tested") cell.Value = @content when nil #no conversion cell.Value = @content else raise ArgumentError, "#{self.class}##{__method__}: Undefined Excel type #{@type}" end #Style must be defined in Workbook #As long I don't know how to do it, I will use Style#to_xls_direct if @style #~ cell.Style = @style.style_id # @style.to_xls_direct(cell) elsif row and row.style row.style.to_xls_direct(cell) end cell end |
#to_xml(xmlbuilder, ns, row = nil) ⇒ Object
Build the xml a work sheet row,
ns must be a method-object to implement the namespace definitions.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rexcel/cell.rb', line 85 def to_xml(xmlbuilder, ns, row = nil) #~ ns.call(:StyleID)=>"1" = {} if @style [ns.call('StyleID')] = @style.style_id #~ elsif row and row.style #~ cell_options[ns.call('StyleID')] = row.style.style_id end xmlbuilder[ns.call].Cell(){ #~ options[ns.call('Type')] = @type xmlbuilder[ns.call].Data(@content, ns.call('Type') => @type ) } end |