Class: Openoffice
- Inherits:
-
GenericSpreadsheet
- Object
- GenericSpreadsheet
- Openoffice
- Defined in:
- lib/roo/openoffice.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Font
Constant Summary collapse
- @@nr =
0
Instance Attribute Summary
Attributes inherited from GenericSpreadsheet
Instance Method Summary collapse
-
#cell(row, col, sheet = nil) ⇒ Object
Returns the content of a spreadsheet-cell.
-
#celltype(row, col, sheet = nil) ⇒ Object
returns the type of a cell: * :float * :string * :date * :percentage * :formula * :time * :datetime.
-
#comment(row, col, sheet = nil) ⇒ Object
returns the comment at (row/col) nil if there is no comment.
-
#comment?(row, col, sheet = nil) ⇒ Boolean
true, if there is a comment.
-
#comments(sheet = nil) ⇒ Object
returns each comment in the selected sheet as an array of elements [row, col, comment].
-
#font(row, col, sheet = nil) ⇒ Object
Given a cell, return the cell’s style.
-
#formula(row, col, sheet = nil) ⇒ Object
Returns the formula at (row,col).
-
#formula?(row, col, sheet = nil) ⇒ Boolean
true, if there is a formula.
-
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements [row, col, formula].
-
#initialize(filename, packed = nil, file_warning = :error, tmpdir = nil) ⇒ Openoffice
constructor
initialization and opening of a spreadsheet file values for packed: :zip.
-
#label(labelname) ⇒ Object
returns the row,col values of the labelled cell (nil,nil) if label is not defined.
-
#labels(sheet = nil) ⇒ Object
Returns an array which all labels.
- #method_missing(m, *args) ⇒ Object
-
#officeversion ⇒ Object
version of the openoffice document at 2007 this is always “1.0”.
-
#set(row, col, value, sheet = nil) ⇒ Object
set a cell to a certain value (this will not be saved back to the spreadsheet file!).
- #sheets ⇒ Object
-
#to_s(sheet = nil) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes.
Methods inherited from GenericSpreadsheet
#column, #empty?, #find, #first_column, #first_column_as_letter, #first_row, #info, #last_column, #last_column_as_letter, #last_row, #reload, #remove_tmp, #row, #to_csv, #to_matrix, #to_xml, #to_yaml
Constructor Details
#initialize(filename, packed = nil, file_warning = :error, tmpdir = nil) ⇒ Openoffice
initialization and opening of a spreadsheet file values for packed: :zip
14 15 16 17 18 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 |
# File 'lib/roo/openoffice.rb', line 14 def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil) @file_warning = file_warning super() file_type_check(filename,'.ods','an openoffice', packed) @tmpdir = GenericSpreadsheet.next_tmpdir @tmpdir = File.join(ENV['ROO_TMP'], @tmpdir) if ENV['ROO_TMP'] @tmpdir = File.join(tmpdir, @tmpdir) if tmpdir unless File.exists?(@tmpdir) FileUtils::mkdir(@tmpdir) end filename = open_from_uri(filename) if filename[0,7] == "http://" filename = unzip(filename) if packed and packed == :zip @cells_read = Hash.new #TODO: @cells_read[:default] = false @filename = filename unless File.file?(@filename) FileUtils::rm_r(@tmpdir) raise IOError, "file #{@filename} does not exist" end @@nr += 1 @file_nr = @@nr extract_content file = File.new(File.join(@tmpdir, @file_nr.to_s+"_roo_content.xml")) @doc = Nokogiri::XML(file) file.close FileUtils::rm_r(@tmpdir) @default_sheet = self.sheets.first @cell = Hash.new @cell_type = Hash.new @formula = Hash.new @first_row = Hash.new @last_row = Hash.new @first_column = Hash.new @last_column = Hash.new @style = Hash.new @style_defaults = Hash.new { |h,k| h[k] = [] } @style_definitions = Hash.new @header_line = 1 @label = Hash.new @labels_read = false @comment = Hash.new @comments_read = Hash.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/roo/openoffice.rb', line 58 def method_missing(m,*args) read_labels unless @labels_read # is method name a label name if @label.has_key?(m.to_s) row,col = label(m.to_s) cell(row,col) else # call super for methods like #a1 super end end |
Instance Method Details
#cell(row, col, sheet = nil) ⇒ Object
Returns 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 the first line and first row.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/roo/openoffice.rb', line 74 def cell(row, col, sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) if celltype(row,col,sheet) == :date yyyy,mm,dd = @cell[sheet][[row,col]].to_s.split('-') return Date.new(yyyy.to_i,mm.to_i,dd.to_i) end @cell[sheet][[row,col]] end |
#celltype(row, col, sheet = nil) ⇒ Object
returns the type of a cell:
-
:float
-
:string
-
:date
-
:percentage
-
:formula
-
:time
-
:datetime
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/roo/openoffice.rb', line 180 def celltype(row,col,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) if @formula[sheet][[row,col]] return :formula else @cell_type[sheet][[row,col]] end end |
#comment(row, col, sheet = nil) ⇒ Object
returns the comment at (row/col) nil if there is no comment
247 248 249 250 251 252 253 |
# File 'lib/roo/openoffice.rb', line 247 def comment(row,col,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) return nil unless @comment[sheet] @comment[sheet][[row,col]] end |
#comment?(row, col, sheet = nil) ⇒ Boolean
true, if there is a comment
256 257 258 259 260 261 |
# File 'lib/roo/openoffice.rb', line 256 def comment?(row,col,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) comment(row,col) != nil end |
#comments(sheet = nil) ⇒ Object
returns each comment in the selected sheet as an array of elements
- row, col, comment
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/roo/openoffice.rb', line 266 def comments(sheet=nil) sheet = @default_sheet unless sheet read_comments(sheet) unless @comments_read[sheet] if @comment[sheet] @comment[sheet].each.collect do |elem| [elem[0][0],elem[0][1],elem[1]] end else [] end end |
#font(row, col, sheet = nil) ⇒ Object
Given a cell, return the cell’s style
146 147 148 149 150 151 152 |
# File 'lib/roo/openoffice.rb', line 146 def font(row, col, sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) style_name = @style[sheet][[row,col]] || @style_defaults[sheet][col - 1] || 'Default' @style_definitions[style_name] end |
#formula(row, col, sheet = nil) ⇒ Object
Returns the formula at (row,col). Returns nil if there is no formula. The method #formula? checks if there is a formula.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/roo/openoffice.rb', line 88 def formula(row,col,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) if @formula[sheet][[row,col]] == nil return nil else # return @formula[sheet][[row,col]]["oooc:".length..-1] # TODO: #str = @formula[sheet][[row,col]] #if str.include? ':' #return str[str.index(':')+1..-1] #else #return str #end @formula[sheet][[row,col]] end end |
#formula?(row, col, sheet = nil) ⇒ Boolean
true, if there is a formula
108 109 110 111 112 113 |
# File 'lib/roo/openoffice.rb', line 108 def formula?(row,col,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) formula(row,col) != nil end |
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements
- row, col, formula
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/roo/openoffice.rb', line 117 def formulas(sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] if @formula[sheet] @formula[sheet].each.collect do |elem| [elem[0][0], elem[0][1], elem[1]] end else [] end end |
#label(labelname) ⇒ Object
returns the row,col values of the labelled cell (nil,nil) if label is not defined
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/roo/openoffice.rb', line 216 def label(labelname) read_labels unless @labels_read unless @label.size > 0 return nil,nil,nil end if @label.has_key? labelname return @label[labelname][1].to_i, GenericSpreadsheet.letter_to_number(@label[labelname][2]), @label[labelname][0] else return nil,nil,nil end end |
#labels(sheet = nil) ⇒ Object
Returns an array which all labels. Each element is an array with
- labelname, [sheetname,row,col]
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/roo/openoffice.rb', line 232 def labels(sheet=nil) read_labels unless @labels_read result = [] @label.each do |label| result << [ label[0], # name [ label[1][1].to_i, # row GenericSpreadsheet.letter_to_number(label[1][2]), # column label[1][0], # sheet ] ] end result end |
#officeversion ⇒ Object
version of the openoffice document at 2007 this is always “1.0”
201 202 203 204 |
# File 'lib/roo/openoffice.rb', line 201 def officeversion oo_version @officeversion end |
#set(row, col, value, sheet = nil) ⇒ Object
set a cell to a certain value (this will not be saved back to the spreadsheet file!)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/roo/openoffice.rb', line 156 def set(row,col,value,sheet=nil) #:nodoc: sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) set_value(row,col,value,sheet) if value.class == Fixnum set_type(row,col,:float,sheet) elsif value.class == String set_type(row,col,:string,sheet) elsif value.class == Float set_type(row,col,:string,sheet) else raise ArgumentError, "Type for "+value.to_s+" not set" end end |
#sheets ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/roo/openoffice.rb', line 191 def sheets return_sheets = [] @doc.xpath("//*[local-name()='table']").each do |sheet| return_sheets << sheet['name'] end return_sheets end |
#to_s(sheet = nil) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes
208 209 210 211 212 |
# File 'lib/roo/openoffice.rb', line 208 def to_s(sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] @cell[sheet].inspect end |