lib/roo/openoffice.rb in roo-0.2.2 vs lib/roo/openoffice.rb in roo-0.2.3
- old
+ new
@@ -1,9 +1,8 @@
require 'rubygems'
require 'rexml/document'
-# require 'matrix'
require 'fileutils'
require 'zip/zipfilesystem'
require 'date'
class Fixnum
@@ -12,21 +11,21 @@
end
end
class Openoffice
-
def initialize(filename)
@cells_read = false
@filename = filename
@tmpdir = "oo_"+$$.to_s
unless File.exists?(@tmpdir)
- FileUtils::mkdir(@tmpdir) #TODO:
+ FileUtils::mkdir(@tmpdir)
end
extract_content
- file = File.new(@tmpdir+"/"+"roo_content.xml") # TODO:
+ file = File.new(File.join(@tmpdir, "roo_content.xml"))
@doc = REXML::Document.new file
+ file.close
@cell = Hash.new
@cell_type = Hash.new
if DateTime.now > Date.new(2007,5,31)
FileUtils::rm_r(@tmpdir)
end
@@ -36,29 +35,19 @@
# return the content of a spreadsheet-cell
# (1,1) is the upper left corner
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
# cell at first line, first row
def cell(row,col)
- if row.class == String
- if col.class == Fixnum
- # ('A',1):
- # ('B', 5) -> (5, 2)
- row, col = col, row
- else
- raise FormatError
- end
- end
- if col.class == String
- col = Openoffice.letter_to_number(col)
- end
read_cells unless @cells_read
+ row,col = normalize(row,col)
@cell["#{row},#{col}"]
end
# returns the open-office type of a cell
def celltype(row,col)
read_cells unless @cells_read
+ row,col = normalize(row,col)
@cell_type["#{row},#{col}"]
end
# returns an array of sheets in the spreadsheet
@@ -95,10 +84,12 @@
def officeversion
read_cells unless @cells_read
@officeversion
end
+ # shows the internal representation of all cells
+ # mainly for debugging purposes
def to_s
read_cells unless @cells_read
@cell.inspect
end
@@ -192,12 +183,19 @@
num = n%26
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[num-1,1] + letters
n = n.div(26)
end
letters
- end
+ end
+ def empty?(row, col)
+ read_cells unless @cells_read
+ return true unless cell(row, col)
+ return true if celltype(row, col) == "string" && cell(row, col).empty?
+ false
+ end
+
private
# read all cells in the selected sheet
def read_cells
oo_document_count = 0
@@ -308,7 +306,24 @@
result = result * 26 + num
letters = letters[1..-1]
end
result
end
-
+
+ # converts cell coordinate to numeric values of row,col
+ def normalize(row,col)
+ if row.class == String
+ if col.class == Fixnum
+ # ('A',1):
+ # ('B', 5) -> (5, 2)
+ row, col = col, row
+ else
+ raise FormatError
+ end
+ end
+ if col.class == String
+ col = Openoffice.letter_to_number(col)
+ end
+ return row,col
+ end
+
end