Class: Roo::OpenOffice
- Extended by:
- Tempdir
- Defined in:
- lib/roo/open_office.rb
Constant Summary collapse
- ERROR_MISSING_CONTENT_XML =
'file missing required content.xml'.freeze
- XPATH_FIND_TABLE_STYLES =
"//*[local-name()='automatic-styles']".freeze
- XPATH_LOCAL_NAME_TABLE =
"//*[local-name()='table']".freeze
Constants inherited from Base
Base::MAX_ROW_COL, Base::MIN_ROW_COL
Instance Attribute Summary
Attributes inherited from Base
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.
-
#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?(*args) ⇒ Boolean
Predicate methods really should return a boolean value.
-
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements [row, col, formula].
-
#initialize(filename, options = {}) ⇒ OpenOffice
constructor
initialization and opening of a spreadsheet file values for packed: :zip.
- #initialize_default_variables ⇒ Object
-
#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 Roo::OpenOffice document at 2007 this is always “1.0”.
- #open_oo_file(options) ⇒ Object
- #sheets ⇒ Object
-
#to_s(sheet = nil) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes.
Methods included from Tempdir
finalize_tempdirs, make_tempdir
Methods inherited from Base
TEMP_PREFIX, #cell_type_by_value, #close, #collect_last_row_col_for_sheet, #column, #default_sheet, #default_sheet=, #each, #each_with_pagename, #empty?, finalize, #find, #first_column_as_letter, #first_last_row_col_for_sheet, #info, #inspect, #last_column_as_letter, #parse, #reload, #row, #row_with, #set, #sheet
Methods included from Formatters::YAML
Methods included from Formatters::XML
Methods included from Formatters::Matrix
Methods included from Formatters::CSV
Methods included from Formatters::Base
Constructor Details
#initialize(filename, options = {}) ⇒ OpenOffice
initialization and opening of a spreadsheet file values for packed: :zip
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 |
# File 'lib/roo/open_office.rb', line 20 def initialize(filename, = {}) packed = [:packed] file_warning = [:file_warning] || :error @only_visible_sheets = [:only_visible_sheets] file_type_check(filename, '.ods', 'an Roo::OpenOffice', file_warning, packed) # NOTE: Create temp directory and allow Ruby to cleanup the temp directory # when the object is garbage collected. Initially, the finalizer was # created in the Roo::Tempdir module, but that led to a segfault # when testing in Ruby 2.4.0. @tmpdir = self.class.make_tempdir(self, find_basename(filename), [:tmpdir_root]) ObjectSpace.define_finalizer(self, self.class.finalize(object_id)) @filename = local_filename(filename, @tmpdir, packed) # TODO: @cells_read[:default] = false open_oo_file() super(filename, ) initialize_default_variables unless @table_display.any? doc.xpath(XPATH_FIND_TABLE_STYLES).each do |style| read_table_styles(style) end end @sheet_names = doc.xpath(XPATH_LOCAL_NAME_TABLE).map do |sheet| if !@only_visible_sheets || @table_display[attribute(sheet, 'style-name')] sheet.attributes['name'].value end end.compact rescue self.class.finalize_tempdirs(object_id) raise end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/roo/open_office.rb', line 75 def method_missing(m, *args) read_labels # is method name a label name if @label.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.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/roo/open_office.rb', line 91 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
148 149 150 151 152 153 |
# File 'lib/roo/open_office.rb', line 148 def celltype(row, col, sheet = nil) sheet ||= default_sheet read_cells(sheet) row, col = normalize(row, col) @formula[sheet][[row, col]] ? :formula : @cell_type[sheet][[row, col]] end |
#comment(row, col, sheet = nil) ⇒ Object
returns the comment at (row/col) nil if there is no comment
201 202 203 204 205 206 207 |
# File 'lib/roo/open_office.rb', line 201 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 |
#comments(sheet = nil) ⇒ Object
returns each comment in the selected sheet as an array of elements
- row, col, comment
211 212 213 214 215 216 217 218 |
# File 'lib/roo/open_office.rb', line 211 def comments(sheet = nil) sheet ||= default_sheet read_comments(sheet) unless @comments_read[sheet] return [] unless @comment[sheet] @comment[sheet].each.collect do |elem| [elem[0][0], elem[0][1], elem[1]] end end |
#font(row, col, sheet = nil) ⇒ Object
Given a cell, return the cell’s style
132 133 134 135 136 137 138 |
# File 'lib/roo/open_office.rb', line 132 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' @font_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.
106 107 108 109 110 111 |
# File 'lib/roo/open_office.rb', line 106 def formula(row, col, sheet = nil) sheet ||= default_sheet read_cells(sheet) row, col = normalize(row, col) @formula[sheet][[row, col]] end |
#formula?(*args) ⇒ Boolean
Predicate methods really should return a boolean value. Hopefully no one was relying on the fact that this previously returned either nil/formula
116 117 118 |
# File 'lib/roo/open_office.rb', line 116 def formula?(*args) !!formula(*args) end |
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements
- row, col, formula
122 123 124 125 126 127 128 129 |
# File 'lib/roo/open_office.rb', line 122 def formulas(sheet = nil) sheet ||= default_sheet read_cells(sheet) return [] unless @formula[sheet] @formula[sheet].each.collect do |elem| [elem[0][0], elem[0][1], elem[1]] end end |
#initialize_default_variables ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/roo/open_office.rb', line 65 def initialize_default_variables @formula = {} @style = {} @style_defaults = Hash.new { |h, k| h[k] = [] } @table_display = Hash.new { |h, k| h[k] = true } @font_style_definitions = {} @comment = {} @comments_read = {} end |
#label(labelname) ⇒ Object
returns the row,col values of the labelled cell (nil,nil) if label is not defined
176 177 178 179 180 181 182 183 184 |
# File 'lib/roo/open_office.rb', line 176 def label(labelname) read_labels return [nil, nil, nil] if @label.size < 1 || !@label.key?(labelname) [ @label[labelname][1].to_i, ::Roo::Utils.letter_to_number(@label[labelname][2]), @label[labelname][0] ] end |
#labels(_sheet = nil) ⇒ Object
Returns an array which all labels. Each element is an array with
- labelname, [row,col,sheetname]
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/roo/open_office.rb', line 188 def labels(_sheet = nil) read_labels @label.map do |label| [label[0], # name [label[1][1].to_i, # row ::Roo::Utils.letter_to_number(label[1][2]), # column label[1][0], # sheet ]] end end |
#officeversion ⇒ Object
version of the Roo::OpenOffice document at 2007 this is always “1.0”
161 162 163 164 |
# File 'lib/roo/open_office.rb', line 161 def officeversion oo_version @officeversion end |
#open_oo_file(options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/roo/open_office.rb', line 54 def open_oo_file() Zip::File.open(@filename) do |zip_file| content_entry = zip_file.glob('content.xml').first fail ArgumentError, ERROR_MISSING_CONTENT_XML unless content_entry roo_content_xml_path = ::File.join(@tmpdir, 'roo_content.xml') content_entry.extract(roo_content_xml_path) decrypt_if_necessary(zip_file, content_entry, roo_content_xml_path, ) end end |
#sheets ⇒ Object
155 156 157 |
# File 'lib/roo/open_office.rb', line 155 def sheets @sheet_names end |
#to_s(sheet = nil) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes
168 169 170 171 172 |
# File 'lib/roo/open_office.rb', line 168 def to_s(sheet = nil) sheet ||= default_sheet read_cells(sheet) @cell[sheet].inspect end |