Class: Spreadsheet::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheet/builder.rb

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



6
7
8
# File 'lib/spreadsheet/builder.rb', line 6

def initialize
  @xml = XmlBuilder.new
end

Instance Method Details

#boolean_cell(*args) ⇒ Object



125
126
127
128
129
130
# File 'lib/spreadsheet/builder.rb', line 125

def boolean_cell(*args)
  value = args.shift
  options = args.last.is_a?(Hash) ? args.last : {}
  options[:align] ||= :center
  cell(:string, (value ? 'Да' : 'Нет'), options)
end

#cell(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spreadsheet/builder.rb', line 69

def cell(*args)
  options = (args.last.kind_of?(Hash) ? args.pop : {})
  if args.size > 1
    type = args.shift
    value = args.first
  elsif args.size == 1
    value = args.first
    type = case value
           when Numeric then :float
           when Date then :date
           when true, false then :boolean
           else :string
           end
  else
    type = :string
    value = ''
  end

  attrs = { 'office:value-type' => type }
  style_name = options.delete(:style) || (@in_header ? 'header' : type)
  if options[:align] || options[:background]
    style_name = style(:cell, :parent => style_name,
                              :align => options[:align],
                              :background => options[:background])
  end
  attrs.update('table:style-name' => style_name)
  attrs.update('table:number-columns-spanned' => options[:span]) if options[:span]
  attrs.update('table:number-rows-spanned' => options[:rowspan]) if options[:rowspan]

  value = value ? 'true' : 'false' if type == :boolean

  if options[:formula]
    attrs['table:formula'] = options[:formula]
  else
    if type == :float
      attrs['office:value'] = value
    else
      attrs["office:#{type}-value"] = value 
    end
  end

  @xml.tag! 'table:table-cell', attrs
end

#column(options = {}) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/spreadsheet/builder.rb', line 45

def column(options={})
  style_name = nil
  style_name = style(:column, :width => options[:width]) if options[:width]
  @xml.tag! 'table:table-column', 'table:style-name' => style_name

  @tables.last[:columns] += 1
end

#content!Object



10
11
12
# File 'lib/spreadsheet/builder.rb', line 10

def content!
  finish
end

#date_cell(*args) ⇒ Object



121
122
123
# File 'lib/spreadsheet/builder.rb', line 121

def date_cell(*args)
  cell :date, *args
end

#headerObject



63
64
65
66
67
# File 'lib/spreadsheet/builder.rb', line 63

def header
  @in_header = true
  @xml.tag!('table:table-header-rows') { yield if block_given? }
  @in_header = nil
end

#numeric_cell(*args) ⇒ Object



117
118
119
# File 'lib/spreadsheet/builder.rb', line 117

def numeric_cell(*args)
  cell :float, *args
end

#row(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/spreadsheet/builder.rb', line 53

def row(*args)
  @tables.last[ @in_header ? :hrows : :drows ] += 1

  options = args.last.is_a?(Hash) ? args.pop : {}
  attrs = {}
  attrs = attrs.merge('table:style-name' => options.delete(:style)) if options.has_key?(:style)

  @xml.tag!('table:table-row', attrs) { yield if block_given? }
end

#spreadsheetObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spreadsheet/builder.rb', line 14

def spreadsheet
  @styles = {}
  @tables = []

  @xml = XmlBuilder.new
  @xml.tag! 'office:body' do
    @xml.tag! 'office:spreadsheet' do
	    yield if block_given?
   
	    @xml.tag! 'table:database-ranges' do
	      for  in @tables
	        start_address = cell_address([:hrows], 1)
	        end_address = cell_address([:hrows]+[:drows], [:columns])
	        @xml.tag! 'table:database-range', 'table:target-range-address' => "'#{[:title]}':.#{start_address}:'#{[:title]}'.#{end_address}", 'table:display-filter-buttons' => 'true'
       end
     end
   end
  end
end

#string_cell(*args) ⇒ Object



113
114
115
# File 'lib/spreadsheet/builder.rb', line 113

def string_cell(*args)
  cell :string, *args
end

#table(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/spreadsheet/builder.rb', line 34

def table(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  title = args.first || "Sheet #{@tables.size+1}"
  # TODO: ensure table title is unique
  attrs = { 'table:name' => title }

  @tables << { :title => title, :columns => 0, :hrows => 0, :drows => 0 }

 @xml.tag!('table:table', attrs) { yield if block_given? }
end