Class: RubyXL::Writer::WorkbookWriter

Inherits:
GenericWriter show all
Defined in:
lib/rubyXL/writer/workbook_writer.rb

Instance Method Summary collapse

Methods inherited from GenericWriter

#add_to_zip, #initialize, #ooxml_object, #render_xml

Constructor Details

This class inherits a constructor from RubyXL::Writer::GenericWriter

Instance Method Details

#filepathObject



5
6
7
# File 'lib/rubyXL/writer/workbook_writer.rb', line 5

def filepath
  File.join('xl', 'workbook.xml')
end

#writeObject



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
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
# File 'lib/rubyXL/writer/workbook_writer.rb', line 9

def write()
  new_xml = render_xml do |xml|
    xml << (xml.create_element('workbook',
            :xmlns => 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
            'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships') { |root|
      root << xml.create_element('fileVersion', { :appName => 'xl', :lastEdited => 4,
                                                  :lowestEdited => 4, :rupBuild => 4505 })

      params = { :showInkAnnotation => 0, :autoCompressPictures => 0 }

      #TODO following line - date 1904? check if mac only
      params[:date1904] = 1 if @workbook.date1904
      root << xml.create_element('workbookPr', params)

      root << (xml.create_element('bookViews') { |views|
        views << xml.create_element('workbookView', { :xWindow => -20, :yWindow => -20,
                                      :windowWidth => 21600, :windowHeight => 13340, :tabRatio => 500 })
      })

      index = 1
      root << (xml.create_element('sheets') { |sheet_xml|
        @workbook.worksheets.each_with_index { |sheet, i|
          sheet_xml << xml.create_element('sheet', { :name => sheet.sheet_name,
                                                     :sheetId => sheet.sheet_id || index, 
                                                     'r:id'=> "rId#{index}" })
          index += 1
        }
      })

      unless @workbook.external_links.empty?

        root << (xml.create_element('externalReferences') { |refs|
          # Need to correlate these with WorkbookRelsWriter
          @workbook.external_links.each_value { 
            refs << xml.create_element('externalReference', { 'r:id' => "rId#{index}" })
            index += 1
          }
        })
      end

      if @workbook.defined_names && !@workbook.defined_names.empty? then
        root << xml.create_element('definedNames') { |names|
          @workbook.defined_names.each { |name| names << name.write_xml(xml) }
        }
      end
           
      #TODO see if this changes with formulas
      #attributes out of order here
      root << xml.create_element('calcPr', { :calcId => 130407, :concurrentCalc => 0 } )

      root << (xml.create_element('extLst') { |list| 
        list << (xml.create_element('ext',  {
                   'xmlns:mx' => 'http://schemas.microsoft.com/office/mac/excel/2008/main',
                   :uri => 'http://schemas.microsoft.com/office/mac/excel/2008/main'}) { |ext|
          ext << xml.create_element('mx:ArchID', { :Flags => 2 })

        })
      })
    })
  end
end