Class: LibExcel::Worksheet

Inherits:
LibXML::XML::Node
  • Object
show all
Includes:
LibXML
Defined in:
lib/libexcel/worksheet.rb

Overview

Manages the worksheet of an Excel::Document

A Worksheet works by first creating a row then by adding one cell at a time until you call add_row again to start on a new row.

Examples:

Create a Worksheet and add “hello world” to the first cell.

worksheet = Excel::Worksheet.new('My worksheet')
worksheet.add_row
worksheet.add_cell("hello world")

Constant Summary collapse

DEFAULT_COLUMN_WIDTH =
'100'

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Worksheet

Creates the Worksheet object. name is the name of the Excel Document Tab. name gets truncated to 31 chars.



18
19
20
21
22
23
24
25
26
# File 'lib/libexcel/worksheet.rb', line 18

def initialize(name)
  super('Worksheet')
  # Excel complains if the name is more than 31 chars.
  self['ss:Name'] = name[0..30]
  self.append table
  @f_column = XML::Node.new('Column')
  @f_column['ss:Width'] = DEFAULT_COLUMN_WIDTH
  @table << @f_column
end

Instance Method Details

#<<(raw_data) ⇒ Object

Append anything that responds to :xml to the Worksheet



82
83
84
85
86
87
# File 'lib/libexcel/worksheet.rb', line 82

def <<(raw_data)
  if !raw_data.respond_to?(:xml)
    raise ArgumentError, "Need to have a Excel::Formula"
  end
  @row << raw_data.xml
end

#add_array(array) ⇒ Object

A helper method to add an array of data to the Worksheet.

Parameters:

  • the (Array)

    Array of Objects being appended to the Worksheet.



75
76
77
78
79
# File 'lib/libexcel/worksheet.rb', line 75

def add_array(array)
  for item in array
    self.add_cell(item)
  end
end

#add_cell(raw_data, attributes = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/libexcel/worksheet.rb', line 51

def add_cell(raw_data, attributes = {})
  @row << cell = XML::Node.new('Cell')
  if attributes[:formula]
    cell['ss:Formula'] = attributes[:formula]
  elsif not raw_data.nil?
    cell << data = XML::Node.new('Data')
    if raw_data.is_a?(Fixnum) or raw_data.is_a?(Float)
      data['ss:Type'] = 'Number'
    elsif raw_data.is_a? String
      data['ss:Type'] = 'String'
    end

    data << raw_data
  else
    cell << data = XML::Node.new('Data')
    data['ss:Type'] = 'String'
    data << raw_data
  end
  data
end

#add_rowObject

Creates a row in the Worksheet. This needs to be called before you can add data to the Worksheet.



47
48
49
# File 'lib/libexcel/worksheet.rb', line 47

def add_row
  @table << @row = XML::Node.new('Row')
end

#appendObject



13
# File 'lib/libexcel/worksheet.rb', line 13

alias_method :append, :<<

#f_columnObject



32
33
34
# File 'lib/libexcel/worksheet.rb', line 32

def f_column
  @f_column['ss:Width']
end

#f_column=(value) ⇒ Object



41
42
43
# File 'lib/libexcel/worksheet.rb', line 41

def f_column=(value)
  @f_column['ss:Width'] = value.to_s
end

#nameObject



28
29
30
# File 'lib/libexcel/worksheet.rb', line 28

def name
  self['ss:Name']
end

#name=(name) ⇒ Object

Sets the name and retuncates it to 31 chars.



37
38
39
# File 'lib/libexcel/worksheet.rb', line 37

def name=(name)
  self['ss:Name'] = name[0..30]
end

#reference(args, equal = '=') ⇒ Object

Creates a reference to a cell in this Worksheet to be used in another Worksheet.



91
92
93
# File 'lib/libexcel/worksheet.rb', line 91

def reference(args, equal = '=')
  Formula.new("#{equal}'#{name}'!#{LibExcel.static(args[:row], args[:col])}")
end