Class: Roo::Excel2003XML

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

Defined Under Namespace

Classes: Font

Constant Summary

Constants inherited from GenericSpreadsheet

GenericSpreadsheet::TEMP_PREFIX

Instance Attribute Summary

Attributes inherited from GenericSpreadsheet

#default_sheet, #header_line, #headers

Instance Method Summary collapse

Methods inherited from GenericSpreadsheet

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

Constructor Details

#initialize(filename, packed = nil, file_warning = :error) ⇒ Excel2003XML

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



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
37
# File 'lib/roo/excel2003xml.rb', line 11

def initialize(filename, packed=nil, file_warning=:error)
  make_tmpdir do |tmpdir|
    filename = open_from_uri(filename, tmpdir) if uri?(filename)
    filename = unzip(filename, tmpdir) if packed == :zip

    file_type_check(filename,'.xml','an Excel 2003 XML', file_warning)
    @cells_read = Hash.new
    @filename = filename
    unless File.file?(@filename)
      raise IOError, "file #{@filename} does not exist"
    end
    @doc = Nokogiri::XML(open(@filename))
  end
  @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
  @header_line = 1
  @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::GenericSpreadsheet

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.



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

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

  • :formula

  • :time

  • :datetime



109
110
111
112
113
114
115
116
117
118
# File 'lib/roo/excel2003xml.rb', line 109

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

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

Given a cell, return the cell’s style



93
94
95
96
97
98
99
# File 'lib/roo/excel2003xml.rb', line 93

def font(row, col, sheet=nil)
  sheet ||= @default_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.



57
58
59
60
61
62
63
64
65
66
# File 'lib/roo/excel2003xml.rb', line 57

def formula(row,col,sheet=nil)
  sheet ||= @default_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)


69
70
71
72
73
74
# File 'lib/roo/excel2003xml.rb', line 69

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


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/roo/excel2003xml.rb', line 148

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



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

def officeversion
  oo_version
  @officeversion
end

#saveObject

save spreadsheet



142
143
144
# File 'lib/roo/excel2003xml.rb', line 142

def save #:nodoc:
  42
end

#sheetsObject



120
121
122
123
124
# File 'lib/roo/excel2003xml.rb', line 120

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

#to_s(sheet = nil) ⇒ Object

shows the internal representation of all cells mainly for debugging purposes



135
136
137
138
139
# File 'lib/roo/excel2003xml.rb', line 135

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