Class: Workbook::Sheet

Inherits:
Array
  • Object
show all
Defined in:
lib/workbook/sheet.rb

Overview

A Sheet is a container of tables

Instance Method Summary collapse

Constructor Details

#initialize(table = Workbook::Table.new([], self), book = nil, options = {}) ⇒ Workbook::Sheet

Initialize a new sheet

Parameters:

  • table (Workbook::Table, Array<Array>) (defaults to: Workbook::Table.new([], self))

    The first table of this sheet

  • book (Workbook::Book) (defaults to: nil)

    The book this sheet belongs to

  • options (Hash) (defaults to: {})

    are forwarded to Workbook::Table.new



15
16
17
18
19
20
21
22
23
# File 'lib/workbook/sheet.rb', line 15

def initialize table=Workbook::Table.new([], self), book=nil, options={}
  if table.is_a? Workbook::Table
    push table
  else
    push Workbook::Table.new(table, self, options)
  end
  self.book = book
  return self
end

Instance Method Details

#bookWorkbook::Book

Returns the book this sheet belongs to

Returns:



70
71
72
73
74
75
76
77
# File 'lib/workbook/sheet.rb', line 70

def book
  if @book
    return @book
  else
    self.book = Workbook::Book.new(self)
    return @book
  end
end

#book=(b) ⇒ Object



79
80
81
# File 'lib/workbook/sheet.rb', line 79

def book= b
  @book = b
end

#cloneWorkbook::Sheet

clones itself and the tables it contains

Returns:



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

def clone
  s = self
  c = super
  c.delete_all
  s.each{|t| c << t.clone}
  return c
end

#create_or_open_table_at(index) ⇒ Object

Create or open the existing table at an index value

Parameters:

  • index (Integer)

    the index of the table



104
105
106
107
108
109
# File 'lib/workbook/sheet.rb', line 104

def create_or_open_table_at index
  t = self[index]
  t = self[index] = Workbook::Table.new if t == nil
  t.sheet = self
  t
end

#delete_allWorkbook::Table

Removes all lines from this table

Returns:



86
87
88
# File 'lib/workbook/sheet.rb', line 86

def delete_all
  self.delete_if{|b| true}
end

#has_contents?Boolean

Returns true if the first table of this sheet contains anything

Returns:

  • (Boolean)


28
29
30
# File 'lib/workbook/sheet.rb', line 28

def has_contents?
  table.has_contents?
end

#nameString

Returns the name of this sheet

Returns:

  • (String)

    the name, defaulting to sheet#index when none is set



42
43
44
# File 'lib/workbook/sheet.rb', line 42

def name
  @name ||= "Sheet #{book.index(self)+1}"
end

#name=(name) ⇒ String

Set the name of this sheet

Parameters:

  • the (String)

    new name of the sheet

Returns:

  • (String)

    the given name



50
51
52
# File 'lib/workbook/sheet.rb', line 50

def name= name
  @name = name
end

#tableWorkbook::Table

Returns the first table of this sheet

Returns:



35
36
37
# File 'lib/workbook/sheet.rb', line 35

def table
  first
end

#table=(table, options = {}) ⇒ Workbook::Table

Set the first table of this sheet with a table or array of cells/values

Parameters:

  • table (Workbook::Table, Array<Array>)

    The new first table of this sheet

  • options (Hash) (defaults to: {})

    are forwarded to Workbook::Table.new

Returns:



58
59
60
61
62
63
64
65
# File 'lib/workbook/sheet.rb', line 58

def table= table, options={}
  if table.is_a? Workbook::Table
    self[0] = table
  else
    self[0] = Workbook::Table.new(table, self, options)
  end
  return table
end