lib/rspreadsheet/cell.rb in rspreadsheet-0.4.4 vs lib/rspreadsheet/cell.rb in rspreadsheet-0.4.5
- old
+ new
@@ -44,10 +44,14 @@
@rowi = arowi
initialize_xml_tied_item(row,acoli)
end
def row; @worksheet.rows(rowi) end
def coordinates; [rowi,coli] end
+ def address
+ Tools.convert_cell_coordinates_to_address(coordinates)
+ end
+
def to_s; value.to_s end
def valuexml; self.valuexmlnode.andand.inner_xml end
def valuexmlnode; self.xmlnode.elements.first end
# use this to find node in cell xml. ex. xmlfind('.//text:a') finds all link nodes
def valuexmlfindall(path)
@@ -126,37 +130,44 @@
case
when gt == nil then raise 'This value type is not storable to cell'
when gt == Float then
remove_all_value_attributes_and_content
set_type_attribute('float')
- Tools.set_ns_attribute(xmlnode,'office','value', avalue.to_s)
- xmlnode << Tools.prepare_ns_node('text','p', avalue.to_f.to_s)
+ sav=avalue.to_f.to_s # to_f handles case when avalue is decimal number
+ Tools.set_ns_attribute(xmlnode,'office','value', sav)
+ xmlnode << Tools.prepare_ns_node('text','p', sav)
when gt == String then
remove_all_value_attributes_and_content
set_type_attribute('string')
xmlnode << Tools.prepare_ns_node('text','p', avalue.to_s)
when gt == :datetime then
remove_all_value_attributes_and_content
set_type_attribute('date')
- avalue = avalue.strftime(InternalDateTimeFormat)
- Tools.set_ns_attribute(xmlnode,'office','date-value', avalue)
- xmlnode << Tools.prepare_ns_node('text','p', avalue)
+ if avalue.kind_of?(DateTime) or avalue.kind_of?(Date) or avalue.kind_of?(Time)
+ avalue = avalue.strftime(InternalDateTimeFormat)
+ Tools.set_ns_attribute(xmlnode,'office','date-value', avalue)
+ xmlnode << Tools.prepare_ns_node('text','p', avalue)
+ end
when gt == :time then
remove_all_value_attributes_and_content
set_type_attribute('time')
- Tools.set_ns_attribute(xmlnode,'office','time-value', avalue.strftime(InternalTimeFormat))
- xmlnode << Tools.prepare_ns_node('text','p', avalue.strftime('%H:%M'))
+ if avalue.kind_of?(DateTime) or avalue.kind_of?(Date) or avalue.kind_of?(Time)
+ Tools.set_ns_attribute(xmlnode,'office','time-value', avalue.strftime(InternalTimeFormat))
+ xmlnode << Tools.prepare_ns_node('text','p', avalue.strftime('%H:%M'))
+ end
when gt == :percentage then
remove_all_value_attributes_and_content
set_type_attribute('percentage')
Tools.set_ns_attribute(xmlnode,'office','value', '%0.2d%' % avalue.to_f)
xmlnode << Tools.prepare_ns_node('text','p', (avalue.to_f*100).round.to_s+'%')
when gt == :currency then
remove_all_value_attributes_and_content
set_type_attribute('currency')
- Tools.set_ns_attribute(xmlnode,'office','value', '%f' % avalue.to_d)
- xmlnode << Tools.prepare_ns_node('text','p', avalue.to_d.to_s+' '+self.format.currency)
+ unless avalue.nil?
+ Tools.set_ns_attribute(xmlnode,'office','value', '%f' % avalue.to_d)
+ xmlnode << Tools.prepare_ns_node('text','p', avalue.to_d.to_s+' '+self.format.currency)
+ end
end
else
raise "Unknown cell mode #{self.mode}"
end
end
@@ -201,11 +212,11 @@
when Date, DateTime then :datetime
when String,nil then nil
else nil
end
result = valueguess
-
+
if valueguess.nil? # valueguess is most important if not succesfull then try guessing by type from node xml
typ = xmlnode.nil? ? 'N/A' : xmlnode.attributes['value-type']
typeguess = case typ
when nil then nil
when 'float' then Float
@@ -253,12 +264,9 @@
end
result
end
def format
@format ||= CellFormat.new(self)
- end
- def address
- Tools.convert_cell_coordinates_to_address(coordinates)
end
def formula
rawformula = Tools.get_ns_attribute(xmlnode,'table','formula',nil).andand.value
if rawformula.nil?