Class: RODF::Spreadsheet

Inherits:
Document show all
Defined in:
lib/rodf/spreadsheet.rb

Instance Method Summary collapse

Methods inherited from Document

#add_default_styles, #add_office_styles, #add_styles, #bytes, #default_style, #default_styles, #default_styles_xml, file, #office_style, #office_styles, #office_styles_xml, #style, #styles, #styles_xml, #write_to

Methods inherited from Container

create, #initialize

Constructor Details

This class inherits a constructor from RODF::Container

Instance Method Details

#add_data_styles(*elements) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/rodf/spreadsheet.rb', line 101

def add_data_styles(*elements)
  if elements.first.is_a?(Array)
    elements = elements.first
  end

  elements.each do |element|
    data_style(element)
  end
end

#add_tables(*elements) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/rodf/spreadsheet.rb', line 75

def add_tables(*elements)
  if elements.first.is_a?(Array)
    elements = elements.first
  end

  elements.each do |element|
    table(element)
  end
end

#data_style(*args, &block) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rodf/spreadsheet.rb', line 89

def data_style(*args, &block)
  x = DataStyle.new(*args, &block)

  data_styles << x

  return x
end

#data_stylesObject



85
86
87
# File 'lib/rodf/spreadsheet.rb', line 85

def data_styles
  @data_styles ||= []
end

#data_styles_xmlObject



97
98
99
# File 'lib/rodf/spreadsheet.rb', line 97

def data_styles_xml
  data_styles.map(&:xml).join
end

#set_column_widths(table:, column_widths:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rodf/spreadsheet.rb', line 41

def set_column_widths(table:, column_widths:)
  table.instance_variable_set(:@columns, []) ### Reset completely

  column_widths.each_with_index do |width, i|
    name = "col-width-#{i}"

    self.style(name, family: :column) do
      property(:column, {'column-width' => width})
    end

    table.columns << RODF::Column.new(style: name)
  end
end

#table(*args, column_widths: nil, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rodf/spreadsheet.rb', line 59

def table(*args, column_widths: nil, &block)
  x = Table.new(*args, &block)

  tables << x

  if column_widths
    set_column_widths(table: x, column_widths: column_widths)
  end

  return x
end

#tablesObject



55
56
57
# File 'lib/rodf/spreadsheet.rb', line 55

def tables
  @tables ||= []
end

#tables_xmlObject



71
72
73
# File 'lib/rodf/spreadsheet.rb', line 71

def tables_xml
  tables.map(&:xml).join
end

#xmlObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rodf/spreadsheet.rb', line 3

def xml
  builder = Builder::XmlMarkup.new

  builder.instruct!(:xml, version: '1.0', encoding: 'UTF-8')
  
  attrs = {
    'xmlns:office' => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
    'xmlns:table' => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
    'xmlns:text' => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
    'xmlns:oooc' => "http://openoffice.org/2004/calc",
    'xmlns:style' => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
    'xmlns:fo' => "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
    'xmlns:number' => "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
    'xmlns:xlink' => "http://www.w3.org/1999/xlink",
  }

  builder.tag!('office:document-content', attrs) do |xml|
    if !default_styles.empty?
      xml.tag! 'office:styles' do
        xml << default_styles_xml
      end
    end

    if !styles.empty? || !data_styles.empty?
      xml.tag! 'office:automatic-styles' do
        xml << styles_xml
        xml << data_styles_xml
      end
    end

    xml.office:body do
      xml.office:spreadsheet do
        xml << tables_xml
      end
    end
  end
end