Class: Oxcelix::Sheet

Inherits:
Matrix
  • Object
show all
Includes:
Cellhelper, Numberhelper
Defined in:
lib/oxcelix/sheet.rb

Overview

The Sheet class represents an excel sheet.

Constant Summary

Constants included from Numformats

Numformats::Dtmap, Numformats::Formatarray

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Numformats

#add_custom_formats, #datetime, #numeric

Methods included from Cellhelper

#x, #y

Methods inherited from Matrix

#[]=

Instance Attribute Details

#nameString


13
14
15
# File 'lib/oxcelix/sheet.rb', line 13

def name
  @name
end

#relationIdString


13
# File 'lib/oxcelix/sheet.rb', line 13

attr_accessor :name, :sheetId, :relationId

#sheetIdString


13
# File 'lib/oxcelix/sheet.rb', line 13

attr_accessor :name, :sheetId, :relationId

Instance Method Details

#[](i, *j) ⇒ Cell

The [] method overrides the standard Matrix::[]. It will now accept Excel-style cell coordinates.

Examples:

Select a cell in a sheet

w = Oxcelix::Workbook.new('Example.xlsx')
w.sheets[0][3,1] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">
w.sheets[0]['B4'] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">

22
23
24
25
26
27
28
# File 'lib/oxcelix/sheet.rb', line 22

def [](i, *j)
  if i.is_a? String
    super(y(i),x(i))
  else
    super(i,j[0])
  end
end

#to_fmtMatrix

The to_fmt method returns a Matrix of “formatted” values. It basically builds a new Matrix and puts the result of the #Cell::to_fmt method of every cell of the original sheet in the corresponding Matrix cell. The #Cell::to_fmt will pass the original values to to_ru, and then depending on the value, will run strftime on DateTime objects and sprintf on numeric types.


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/oxcelix/sheet.rb', line 74

def to_fmt
  m=Matrix.build(self.row_size, self.column_size){nil}
  self.each_with_index do |x, row, col|
    if x.nil? || x.value.nil?
      m[row, col] = nil
    else
      m[row, col] = x.to_fmt
    end
  end
  return m
end

#to_fmt!Object

Invokes the #Cell::to_fmt method on each element of self, replacing each element of the Sheet with the value returned.


86
87
88
89
90
91
92
93
94
# File 'lib/oxcelix/sheet.rb', line 86

def to_fmt!
  self.each_with_index do |x, row, col|
    if x.nil? || x.value.nil?
      self[row, col] = nil
    else
      self[row, col] = x.to_fmt
    end
  end
end

#to_m(*attrs) ⇒ Matrix

The to_m method returns a simple Matrix object filled with the raw values of the original Sheet object.


32
33
34
35
36
37
38
39
40
# File 'lib/oxcelix/sheet.rb', line 32

def to_m(*attrs)
  m=Matrix.build(self.row_size, self.column_size){nil}
  self.each_with_index do |x, row, col|
    if attrs.size == 0 || attrs.nil?
      m[row, col] = x.value
    end
  end
  return m
end

#to_ruMatrix

The to_ru method returns a Matrix of “rubified” values. It basically builds a new Matrix and puts the result of the #Cell::to_ru method of every cell of the original sheet in the corresponding Matrix cell.


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/oxcelix/sheet.rb', line 46

def to_ru
  m=Matrix.build(self.row_size, self.column_size){nil}
  self.each_with_index do |x, row, col|
    if x.nil? || x.value.nil?
      m[row, col] = nil
    else
      m[row, col] = x.to_ru
    end
  end
  return m
end

#to_ru!Object

Invokes the #Cell::to_ru method on each element of self, replacing each element of the Sheet with the value returned.


59
60
61
62
63
64
65
66
67
# File 'lib/oxcelix/sheet.rb', line 59

def to_ru!
  self.each_with_index do |x, row, col|
    if x.nil? || x.value.nil?
      self[row, col] = nil
    else
      self[row, col] = x.to_ru
    end
  end
end