Sha256: 8f795a8ccd2cc3596f718e9a1cbeb7635dc3f678fa4329c260feff4ead9eae8d

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

require 'zip'
require 'libxml'

module Rspreadsheet
class Workbook
  attr_reader :worksheets, :filename
  def initialize(afilename=nil)
    @worksheets={}
    @filename = afilename
    if filename.nil?
    else
      @content_xml = Zip::File.open(filename) do |zip|
        LibXML::XML::Document.io zip.get_input_stream('content.xml')
      end

      ndx = 0
      @content_xml.find_first('//office:spreadsheet').each_element { |node|
        sheet = Worksheet.new(node)
        @worksheets[ndx]=sheet
        @worksheets[node.name]=sheet
        ndx+=1
      }
    end
  end
  def save(new_filename=nil)
    if @filename.nil? and new_filename.nil? then raise 'New file should be named on first save.' end
    # if the filename has changed than first copy the original file to new location (or template if it is a new file)
    if new_filename
      FileUtils.cp(@filename || './lib/rspreadsheet/empty_file_template.ods', new_filename)
      @filename = new_filename
    end
    Zip::File.open(@filename) do |zip|
      # it is easy, because @content_xml in in sync with contents all the time
      zip.get_output_stream('content.xml') do |f|
        f.write @content_xml
      end
    end
  end
  def create_worksheet(node=nil)
    sheet = Worksheet.new(node)
    @worksheets[worksheets_count]=sheet
    @worksheets[node.name]=sheet unless node.nil?
    return sheet
  end
  def worksheets
    @worksheets
  end
  def worksheets_count
    @worksheets.keys.select{ |k| k.kind_of? Numeric }.size #TODO: ?? max
  end
  def worksheet_names
    @worksheets.keys.reject{ |k| k.kind_of? Numeric }
  end
end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspreadsheet-0.0.5 lib/rspreadsheet/workbook.rb
rspreadsheet-0.0.4 lib/rspreadsheet/workbook.rb
rspreadsheet-0.0.2 lib/rspreadsheet/workbook.rb