Class: Ods

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

Defined Under Namespace

Classes: Cell, Column, Row, Sheet

Constant Summary collapse

XPATH_SHEETS =
'//office:body/office:spreadsheet/table:table'
NAMESPACES =
{
  'office' => 'urn:oasis:names:tc:opendocument:xmlns:office:1.0',
  'style'  => 'urn:oasis:names:tc:opendocument:xmlns:style:1.0',
  'text'   => 'urn:oasis:names:tc:opendocument:xmlns:text:1.0',
  'table'  => 'urn:oasis:names:tc:opendocument:xmlns:table:1.0',
  'draw'   => 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0',
  'fo'     => 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0',
  'xlink'  => 'http://www.w3.org/1999/xlink',
  'dc'     => 'http://purl.org/dc/elements/1.1/',
  'meta'   => 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0',
  'number' => 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0',
  'presentation' => 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0',
  'svg'    => 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0',
  'chart'  => 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0',
  'dr3d'   => 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0',
  'math'   => 'http://www.w3.org/1998/Math/MathML',
  'form'   => 'urn:oasis:names:tc:opendocument:xmlns:form:1.0',
  'script' => 'urn:oasis:names:tc:opendocument:xmlns:script:1.0',
  'ooo'    => 'http://openoffice.org/2004/office',
  'ooow'   => 'http://openoffice.org/2004/writer',
  'oooc'   => 'http://openoffice.org/2004/calc',
  'dom'    => 'http://www.w3.org/2001/xml-events',
  'xforms' => 'http://www.w3.org/2002/xforms',
  'xsd'    => 'http://www.w3.org/2001/XMLSchema',
  'xsi'    => 'http://www.w3.org/2001/XMLSchema-instance',
  'rpt'    => 'http://openoffice.org/2005/report',
  'of'     => 'urn:oasis:names:tc:opendocument:xmlns:of:1.2',
  'rdfa'   => 'http://docs.oasis-open.org/opendocument/meta/rdfa#',
  'field'  => 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0',
  'formx'  => 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Ods

Returns a new instance of Ods.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ods.rb', line 45

def initialize(path)
  @path = path
  Zip::ZipFile.open(@path) do |zip|
    @content = Nokogiri::XML::Document.parse(zip.read('content.xml'))
  end
  @sheets = []
  @content.root.xpath(XPATH_SHEETS).each do |sheet|
    @sheets.push(Sheet.new(sheet))
  end
  @content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#sheetsObject (readonly)

Returns the value of attribute sheets.



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

def sheets
  @sheets
end

Instance Method Details

#create_sheetObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ods.rb', line 81

def create_sheet
  parent = @content.root.xpath(XPATH_SHEETS.split('/')[0..-2].join('/'))[0]
  table = parent.add_element('table:table',
                             'name'       => "Sheet#{@sheets.length + 1}",
                             'style-name' => 'ta1',
                             'print'      => 'false')
  table.add_element('table:table-column',
                    'style-name'              => 'co1',
                    'default-cell-style-name' => 'Default')
  new_sheet = Sheet.new(table)
  @sheets.push(new_sheet)
  new_sheet
end

#save(dest = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ods.rb', line 57

def save(dest=nil)
  if dest
    FileUtils.cp(@path, dest)
  else
    dest = @path
  end

  @sheets.each do |sheet|
    column = sheet.column
    max_length = 0
    column.content.parent.xpath('table:table-row').each do |row|
      length = row.xpath('table:table-cell').length
      max_length = length if max_length < length
    end
    column.set_attr('repeated', max_length)
  end

  Zip::ZipFile.open(dest) do |zip|
    zip.get_output_stream('content.xml') do |io|
      io << @content.to_s
    end
  end
end