Module: Documatic::OpenDocumentSpreadsheet::Helper

Includes:
ERB::Util
Included in:
Component
Defined in:
lib/documatic/open_document_spreadsheet/helper.rb

Constant Summary collapse

TYPES =

Map of OOCalc’s visible types (selectable in the UI) to what’s stored internally as the office:value-type attribute of a cell.

{
  'Number'     => 'float',
  'Percent'    => 'percentage',
  'Currency'   => 'currency',
  'Date'       => 'date',
  'Time'       => 'time',
  'Scientific' => 'float',
  'Fraction'   => 'string',
  'Boolean'    => 'boolean',
  'Text'       => 'string',
}

Instance Method Summary collapse

Instance Method Details

#cell(value, opts = nil) ⇒ Object

Renders a complete cell element with options to control the type, style, formula, row and column spans, and other cell attributes. See the wiki for full details.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/documatic/open_document_spreadsheet/helper.rb', line 29

def cell(value, opts = nil)
  opts ||= Hash.new
  opts[:type] ||= (case value.class.to_s
                   when 'Fixnum' then 'Number'
                   when 'Float' then 'Number'
                   when 'DateTime' then 'Date'
                   when 'Date' then 'Date'
                   when 'Time' then 'Time'
                   when 'TrueClass' then 'Boolean'
                   when 'FalseClass' then 'Boolean'
                   else 'Text'
                   end )
  # Setting the :currency option forces the type to 'Currency'
  if opts.has_key?(:currency)
    opts[:type] = 'Currency'
  end
  
  # START OUTPUT
  output = '<table:table-cell'
  # Add style if specified
  opts.has_key?(:style) &&
    output << " table:style-name=\"#{opts[:style]}\""
  # Add formula if specified
  opts.has_key?(:formula) &&
    output << " table:formula=\"#{opts[:formula]}\""
  # Add the value-type attribute for the type
  output << " office:value-type=\"#{TYPES[opts[:type]]}\""
  # Add row and column spans if specified
  opts.has_key?(:colspan) &&
    output << " table:number-columns-spanned=\"#{opts[:colspan]}\""
  opts.has_key?(:rowspan) &&
    output << " table:number-rows-spanned=\"#{opts[:rowspan]}\""
  # The rest of the output depends on the type
  case opts[:type]
  when 'Number', 'Percent', 'Scientific'
    output << " office:value=\"#{ERB::Util.h(value)}\">"
  when 'Currency'
    output << " office:currency=\"#{ERB::Util.h(opts[:currency])}\""
    output << " office:value=\"#{ERB::Util.h(value)}\">"
  when 'Date'
    output << " office:date-value=\"#{value.strftime("%Y-%m-%dT%H:%M:%S")}\">"
  when 'Time'
    output << " office:time-value=\"#{value.strftime("PT%HH%MM%SS")}\">"
  when 'Boolean'
    output << " office:boolean-value=\"#{value.to_s}\">"
  else  # text or fraction
    output << "><text:p>#{ERB::Util.h(value)}</text:p>"
  end
  output << "</table:table-cell>"
  
  return output
end