Class: RubyXL::Writer::AppWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ AppWriter

Returns a new instance of AppWriter.



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

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

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



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

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



11
12
13
# File 'lib/writer/app_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
# File 'lib/writer/app_writer.rb', line 19

def write()

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.Properties('xmlns' => 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties',
    'xmlns:vt'=>'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes') {
      xml.Application @workbook.application
      xml.DocSecurity '0'
      xml.ScaleCrop 'false'
      xml.HeadingPairs {
        xml['vt'].vector(:size => '2', :baseType => 'variant') {
          xml['vt'].variant {
            xml['vt'].lpstr 'Worksheets'
          }
          xml['vt'].variant {
            xml['vt'].i4 @workbook.worksheets.size.to_s()
          }
        }
      }
      xml.TitlesOfParts {
        xml['vt'].vector(:size=>@workbook.worksheets.size.to_s(), :baseType=>"lpstr") {
          @workbook.worksheets.each do |sheet|
            xml['vt'].lpstr sheet.sheet_name
          end
        }
      }
      xml.Company @workbook.company
      xml.LinksUpToDate 'false'
      xml.SharedDoc 'false'
      xml.HyperlinksChanged 'false'
      xml.AppVersion @workbook.appversion
    }
  end
  contents = builder.to_xml
  if(contents =~ /xmlns:vt=\"(.*)\" xmlns=\"(.*)\"/)
    contents.sub(/xmlns:vt=\"(.*)\" xmlns=\"(.*)\"<A/,'xmlns="'+$2+'" xmlns:vt="'+$1+'"<A')
  end
  contents = contents.gsub(/\n/,'')
  contents = contents.gsub(/>(\s)+</,'><')
  contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
  contents
end