Class: Roo::Excel2003XML

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

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

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, #method_missing, #parse, #reload, #row, #row_with, #set, #sheet, #to_csv, #to_matrix, #to_xml, #to_yaml

Constructor Details

#initialize(filename, options = {}, deprecated_file_warning = :error) ⇒ Excel2003XML

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/roo/excel2003xml.rb', line 9

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

  make_tmpdir do |tmpdir|
    filename = download_uri(filename, tmpdir) if uri?(filename)
    filename = unzip(filename, tmpdir) if packed == :zip

    file_type_check(filename,'.xml','an Excel 2003 XML', file_warning)
    @filename = filename
    unless File.file?(@filename)
      raise IOError, "file #{@filename} does not exist"
    end
    @doc = load_xml(@filename)
  end
  super(filename, options)
  @formula = Hash.new
  @style = Hash.new
  @style_defaults = Hash.new { |h,k| h[k] = [] }
  @style_definitions = Hash.new
  read_styles
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Roo::Base

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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/roo/excel2003xml.rb', line 42

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]].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



97
98
99
100
101
102
103
104
105
106
# File 'lib/roo/excel2003xml.rb', line 97

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

#font(row, col, sheet = nil) ⇒ Object

Given a cell, return the cell’s style



81
82
83
84
85
86
87
# File 'lib/roo/excel2003xml.rb', line 81

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.



56
57
58
59
60
61
# File 'lib/roo/excel2003xml.rb', line 56

def formula(row,col,sheet=nil)
  sheet ||= @default_sheet
  read_cells(sheet)
  row,col = normalize(row,col)
  @formula[sheet][[row,col]] && @formula[sheet][[row,col]]["oooc:".length..-1]
end

#formulas(sheet = nil) ⇒ Object

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

row, col, formula


136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/roo/excel2003xml.rb', line 136

def formulas(sheet=nil)
  theformulas = Array.new
  sheet ||= @default_sheet
  read_cells(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”



116
117
118
119
# File 'lib/roo/excel2003xml.rb', line 116

def officeversion
  oo_version
  @officeversion
end

#saveObject

save spreadsheet



130
131
132
# File 'lib/roo/excel2003xml.rb', line 130

def save #:nodoc:
  42
end

#sheetsObject



108
109
110
111
112
# File 'lib/roo/excel2003xml.rb', line 108

def sheets
  @doc.xpath("/ss:Workbook/ss:Worksheet").map do |sheet|
    sheet['ss:Name']
  end
end

#to_s(sheet = nil) ⇒ Object

shows the internal representation of all cells mainly for debugging purposes



123
124
125
126
127
# File 'lib/roo/excel2003xml.rb', line 123

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