Class: Openoffice

Inherits:
GenericSpreadsheet show all
Defined in:
lib/roo/openoffice.rb

Constant Summary collapse

@@nr =
0

Instance Attribute Summary

Attributes inherited from GenericSpreadsheet

#default_sheet, #header_line

Instance Method Summary collapse

Methods inherited from GenericSpreadsheet

#empty?, #find, #first_column, #first_column_as_letter, #first_row, #info, #last_column, #last_column_as_letter, #last_row, letter_to_number, #normalize, number_to_letter, #open_from_uri, #reload, #remove_tmp, #to_csv, #to_yaml

Constructor Details

#initialize(filename, packed = nil) ⇒ Openoffice

initialization and opening of a spreadsheet file values for packed: :zip



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 15

def initialize(filename, packed=nil) #, create = false)
  @tmpdir = "oo_"+$$.to_s
  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
  if File.extname(filename) != ".ods"
    warn "are you sure, this is an openoffice file?"
  end
  #if create and ! File.exists?(filename)
  #  self.create_openoffice(filename)
  #end
  @cells_read = Hash.new
  #TODO: @cells_read[:default] = false
  @filename = filename
  unless File.file?(@filename)
    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 = REXML::Document.new file
  file.close
  #if ENV["roo_local"] != "thomas-p"
  FileUtils::rm_r(@tmpdir)
  #end
  @default_sheet = nil
  # no need to set default_sheet if there is only one sheet in the document
  if self.sheets.size == 1
    @default_sheet = self.sheets.first
  end
  @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
  @header_line = 1
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}"].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



130
131
132
133
134
135
136
137
138
139
# File 'lib/roo/openoffice.rb', line 130

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

#column(columnnumber, sheet = nil) ⇒ Object

returns all values in this column as an array column numbers are 1,2,3,… like in the spreadsheet



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/roo/openoffice.rb', line 206

def column(columnnumber,sheet=nil)
  if columnnumber.class == String
    columnnumber = Openoffice.letter_to_number(columnnumber)
  end
  sheet = @default_sheet unless sheet
  read_cells(sheet) unless @cells_read[sheet]
  result = []
  first_row(sheet).upto(last_row(sheet)) do |row|
    result << cell(row,columnnumber,sheet)
  end
  result
end

#create_openoffice(filename) ⇒ Object

creates a new empty openoffice-spreadsheet file



59
60
61
62
63
64
65
66
67
68
# File 'lib/roo/openoffice.rb', line 59

def create_openoffice(filename) #:nodoc:
  #TODO: a better way for creating the file contents
  # now you have to call mkbase64...rb to create an include file with all
  # the empty files in an openoffice zip-file
  load 'base64include.rb'
  # puts @@empty_spreadsheet
  f = File.open(filename,'wb')
  f.print(Base64.decode64(@@empty_spreadsheet))
  f.close
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
# 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]
  end
end

#formula?(row, col, sheet = nil) ⇒ Boolean

true, if there is a formula

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/roo/openoffice.rb', line 100

def formula?(row,col,sheet=nil)
  sheet = @default_sheet unless sheet
  read_cells 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


240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/roo/openoffice.rb', line 240

def formulas(sheet=nil)
  theformulas = Array.new
  sheet = @default_sheet unless sheet
  read_cells(sheet) unless @cells_read[sheet]
  first_row(sheet).upto(last_row(sheet)) {|row|
    first_column(sheet).upto(last_column(sheet)) {|col|
      if formula?(row,col,sheet)
        f = [row, col, formula(row,col,sheet)]
        theformulas << f
      end
    }
  }
  theformulas
end

#officeversionObject

version of the openoffice document at 2007 this is always “1.0”



168
169
170
171
172
# File 'lib/roo/openoffice.rb', line 168

def officeversion
  # read_cells(false) unless @cells_read
  oo_version
  @officeversion
end

#row(rownumber, sheet = nil) ⇒ Object

returns all values in this row as an array row numbers are 1,2,3,… like in the spreadsheet



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/roo/openoffice.rb', line 184

def row(rownumber,sheet=nil)
  sheet = @default_sheet unless sheet
  read_cells(sheet) unless @cells_read[sheet]
  result = []
  tmp_arr = []
  @cell[sheet].each_pair {|key,value|
    y,x = key.split(',')
    x = x.to_i
    y = y.to_i
    if y == rownumber
      tmp_arr[x] = value
    end
  }
  result = tmp_arr[1..-1]
  while result[-1] == nil
    result = result[0..-2]
  end
  result
end

#saveObject

save spreadsheet



220
221
222
# File 'lib/roo/openoffice.rb', line 220

def save #:nodoc:
  42
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!)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roo/openoffice.rb', line 109

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

#sheetsObject

returns an array of sheet names in the spreadsheet



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/roo/openoffice.rb', line 142

def sheets
  return_sheets = []
  oo_document_count = 0
  @doc.each_element do |oo_document|
    oo_document_count += 1
    oo_element_count = 0
    oo_document.each_element do |oo_element|
      oo_element_count += 1
      if oo_element.name == "body"
        oo_element.each_element do |be|
          if be.name == "spreadsheet"
            be.each_element do |se|
              if se.name == "table"
                return_sheets << se.attributes['name']
              end
            end
          end
        end
      end
    end
  end
  return_sheets
end

#solve(row, col) ⇒ Object

evaluate the formula at this cell experimental: DO NOT USE THIS!



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/roo/openoffice.rb', line 226

def solve(row,col) #:nodoc:
  parser = SpreadsheetParser.new
  visitor = Visitor.new
  #puts cell(row,col)
  #puts formula(row,col)
  formula = formula(row,col)[1..-1] # .downcase
  # puts formula
  #eval formula
  #parser.parse(formula)
  parser.parse(formula).accept(visitor)
end

#to_s(sheet = nil) ⇒ Object

shows the internal representation of all cells mainly for debugging purposes



176
177
178
179
180
# File 'lib/roo/openoffice.rb', line 176

def to_s(sheet=nil)
  sheet = @default_sheet unless sheet
  read_cells(sheet) unless @cells_read[sheet]
  @cell[sheet].inspect
end