Class: RubyXL::Writer::WorkbookWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ WorkbookWriter

Returns a new instance of WorkbookWriter.



13
14
15
16
17
# File 'lib/rubyXL/writer/workbook_writer.rb', line 13

def initialize(dirpath, wb)
  @dirpath = dirpath
  @workbook = wb
  @filepath = dirpath + '/xl/workbook.xml'
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



11
12
13
# File 'lib/rubyXL/writer/workbook_writer.rb', line 11

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



11
12
13
# File 'lib/rubyXL/writer/workbook_writer.rb', line 11

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



11
12
13
# File 'lib/rubyXL/writer/workbook_writer.rb', line 11

def workbook
  @workbook
end

Instance Method Details

#writeObject



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
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubyXL/writer/workbook_writer.rb', line 19

def write()
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.workbook('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main",
    'xmlns:r'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships") {
      #attributes out of order here
      xml.fileVersion('appName'=>'xl', 'lastEdited'=>'4','lowestEdited'=>'4','rupBuild'=>'4505')
      #TODO following line - date 1904? check if mac only
      if @workbook.date1904.nil? || @workbook.date1904.to_s == ''
        xml.workbookPr('showInkAnnotation'=>'0', 'autoCompressPictures'=>'0')
      else
        xml.workbookPr('date1904'=>@workbook.date1904.to_s, 'showInkAnnotation'=>'0', 'autoCompressPictures'=>'0')
      end
      xml.bookViews {
        #attributes out of order here
        xml.workbookView('xWindow'=>'-20', 'yWindow'=>'-20',
          'windowWidth'=>'21600','windowHeight'=>'13340','tabRatio'=>'500')
      }
      index = 0
      xml.sheets {
        @workbook.worksheets.each_with_index do |sheet,i|
          xml.sheet('name'=>sheet.sheet_name, 'sheetId'=>(i+1).to_s(),
          'r:id'=>'rId'+(i+1).to_s())
          index = i+1
        end
      }
      unless @workbook.external_links.nil?
        xml.externalReferences {
          index.upto(@workbook.external_links.size-1) do |id|
            xml.externalReference('r:id'=>"rId#{id+index}")
          end
        }
      end

      # nokogiri builder creates CDATA tag around content,
      # using .text creates "html safe" &lt; and &gt; in place of < and >
      # xml to hash method does not seem to function well for this particular piece of xml
      xml.cdata @workbook.defined_names.to_s

      #TODO see if this changes with formulas
      #attributes out of order here
      xml.calcPr('calcId'=>'130407', 'concurrentCalc'=>'0')
      xml.extLst {
        xml.ext('xmlns:mx'=>"http://schemas.microsoft.com/office/mac/excel/2008/main",
        'uri'=>"http://schemas.microsoft.com/office/mac/excel/2008/main") {
          xml['mx'].ArchID('Flags'=>'2')
        }
      }
    }
  end
  contents = builder.to_xml
  p contents
  contents = contents.gsub(/\n/,'')
  contents = contents.gsub(/>(\s)+</,'><')
  contents = contents.gsub(/<!\[CDATA\[(.*)\]\]>/,'\1')
  contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
  puts '

  '
  puts contents

  contents
end