Class: Roo::OpenOffice

Inherits:
Base
  • Object
show all
Defined in:
lib/roo/openoffice.rb

Direct Known Subclasses

LibreOffice

Defined Under Namespace

Classes: Font

Constant Summary

Constants inherited from Base

Base::TEMP_PREFIX

Instance Attribute Summary

Attributes inherited from Base

#default_sheet, #header_line, #headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#column, #each, #each_with_pagename, #empty?, #find, #first_column, #first_column_as_letter, #first_row, #info, #last_column, #last_column_as_letter, #last_row, #parse, #reload, #row, #row_with, #set, #sheet, #to_csv, #to_matrix, #to_xml, #to_yaml

Constructor Details

#initialize(filename, options = {}, deprecated_file_warning = :error, deprecated_tmpdir_root = nil) ⇒ OpenOffice

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/roo/openoffice.rb', line 33

def initialize(filename, options={}, deprecated_file_warning=:error, deprecated_tmpdir_root=nil)
  if Hash === options
    packed = options[:packed]
    file_warning = options[:file_warning] || :error
    tmpdir_root = options[:tmpdir_root]
  else
    warn 'Supplying `packed`, `file_warning`, or `tmpdir_root` as separate arguments to `Roo::OpenOffice.new` is deprecated. Use an options hash instead.'
    packed = options
    file_warning = deprecated_file_warning
    tmpdir_root = deprecated_tmpdir_root
  end

  file_type_check(filename,'.ods','an Roo::OpenOffice', file_warning, packed)
  make_tmpdir(tmpdir_root) do |tmpdir|
    filename = download_uri(filename, tmpdir) if uri?(filename)
    filename = unzip(filename, tmpdir) if packed == :zip
    #TODO: @cells_read[:default] = false
    @filename = filename
    unless File.file?(@filename)
      raise IOError, "file #{@filename} does not exist"
    end
    self.class.extract_content(tmpdir, @filename)
    @doc = load_xml(File.join(tmpdir, "roo_content.xml"))
  end
  super(filename, options)
  @formula = Hash.new
  @style = Hash.new
  @style_defaults = Hash.new { |h,k| h[k] = [] }
  @style_definitions = Hash.new
  @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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roo/openoffice.rb', line 66

def method_missing(m,*args)
  read_labels
  # 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

Class Method Details

.extract_content(tmpdir, filename) ⇒ Object



7
8
9
10
11
# File 'lib/roo/openoffice.rb', line 7

def extract_content(tmpdir, filename)
  Roo::ZipFile.open(filename) do |zip|
    process_zipfile(tmpdir, zip)
  end
end

.process_zipfile(tmpdir, zip, path = '') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/roo/openoffice.rb', line 13

def process_zipfile(tmpdir, zip, path='')
  if zip.file.file? path
    if path == "content.xml"
      open(File.join(tmpdir, 'roo_content.xml'),'wb') {|f|
        f << zip.read(path)
      }
    end
  else
    unless path.empty?
      path += '/'
    end
    zip.dir.foreach(path) do |filename|
      process_zipfile(tmpdir, zip, path+filename)
    end
  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.



82
83
84
85
86
87
88
89
90
91
# File 'lib/roo/openoffice.rb', line 82

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



151
152
153
154
155
156
157
158
159
160
# File 'lib/roo/openoffice.rb', line 151

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



214
215
216
217
218
219
220
# File 'lib/roo/openoffice.rb', line 214

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

Returns:

  • (Boolean)


223
224
225
226
227
228
# File 'lib/roo/openoffice.rb', line 223

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


233
234
235
236
237
238
239
240
241
242
243
# File 'lib/roo/openoffice.rb', line 233

def comments(sheet=nil)
  sheet ||= @default_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



135
136
137
138
139
140
141
# File 'lib/roo/openoffice.rb', line 135

def font(row, col, sheet=nil)
  sheet ||= @default_sheet
  read_cells(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 Also known as: formula?

Returns the formula at (row,col). Returns nil if there is no formula. The method #formula? checks if there is a formula.



96
97
98
99
100
101
# File 'lib/roo/openoffice.rb', line 96

def formula(row,col,sheet=nil)
  sheet ||= @default_sheet
  read_cells(sheet)
  row,col = normalize(row,col)
  @formula[sheet][[row,col]]
end

#formulas(sheet = nil) ⇒ Object

returns each formula in the selected sheet as an array of elements

row, col, formula


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roo/openoffice.rb', line 106

def formulas(sheet=nil)
  sheet ||= @default_sheet
  read_cells(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



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

def label(labelname)
  read_labels
  unless @label.size > 0
    return nil,nil,nil
  end
  if @label.has_key? labelname
    return @label[labelname][1].to_i,
      Roo::Base.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, [row,col,sheetname]


201
202
203
204
205
206
207
208
209
210
# File 'lib/roo/openoffice.rb', line 201

def labels(sheet=nil)
  read_labels
  @label.map do |label|
    [ label[0], # name
      [ label[1][1].to_i, # row
        Roo::Base.letter_to_number(label[1][2]), # column
        label[1][0], # sheet
      ] ]
  end
end

#officeversionObject

version of the Roo::OpenOffice document at 2007 this is always “1.0”



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

def officeversion
  oo_version
  @officeversion
end

#sheetsObject



162
163
164
165
166
# File 'lib/roo/openoffice.rb', line 162

def sheets
  @doc.xpath("//*[local-name()='table']").map do |sheet|
    sheet.attributes["name"].value
  end
end

#to_s(sheet = nil) ⇒ Object

shows the internal representation of all cells mainly for debugging purposes



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

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