class Ruby::Ods::Manager

Constants

NAMESPACES
XPATH_SHEETS

Attributes

content[R]
sheets[R]

Public Class Methods

new(path) click to toggle source
# File lib/ruby/ods.rb, line 49
def initialize(path)
  @path = path
  Zip::File.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

Public Instance Methods

create_sheet() click to toggle source
# File lib/ruby/ods.rb, line 85
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) click to toggle source
# File lib/ruby/ods.rb, line 61
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::File.open(dest) do |zip|
    zip.get_output_stream('content.xml') do |io|
      io << @content.to_s
    end
  end
end