Class: Spreadsheet::Workbook

Inherits:
Object
  • Object
show all
Includes:
Encodings
Defined in:
lib/spreadsheet/workbook.rb

Overview

The Workbook class represents a Spreadsheet-Document and is the entry point for all Spreadsheet manipulation.

Interesting Attributes:

#default_format

The default format used for all cells in this Workbook. that have no format set explicitly or in Row#default_format or Worksheet#default_format.

Direct Known Subclasses

Excel::Workbook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil, opts = {:default_format => Format.new}) ⇒ Workbook

Returns a new instance of Workbook.



17
18
19
20
21
22
23
24
25
# File 'lib/spreadsheet/workbook.rb', line 17

def initialize io = nil, opts={:default_format => Format.new}
  @worksheets = []
  @io = io
  @fonts = []
  @formats = []
  if @default_format = opts[:default_format]
    @formats.push @default_format
  end
end

Instance Attribute Details

#active_worksheetObject

Returns the value of attribute active_worksheet.



16
17
18
# File 'lib/spreadsheet/workbook.rb', line 16

def active_worksheet
  @active_worksheet
end

#default_formatObject

Returns the value of attribute default_format.



16
17
18
# File 'lib/spreadsheet/workbook.rb', line 16

def default_format
  @default_format
end

#encodingObject

Returns the value of attribute encoding.



16
17
18
# File 'lib/spreadsheet/workbook.rb', line 16

def encoding
  @encoding
end

#fontsObject (readonly)

Returns the value of attribute fonts.



15
16
17
# File 'lib/spreadsheet/workbook.rb', line 15

def fonts
  @fonts
end

#formatsObject (readonly)

Returns the value of attribute formats.



15
16
17
# File 'lib/spreadsheet/workbook.rb', line 15

def formats
  @formats
end

#ioObject (readonly)

Returns the value of attribute io.



15
16
17
# File 'lib/spreadsheet/workbook.rb', line 15

def io
  @io
end

#versionObject

Returns the value of attribute version.



16
17
18
# File 'lib/spreadsheet/workbook.rb', line 16

def version
  @version
end

#worksheetsObject (readonly)

Returns the value of attribute worksheets.



15
16
17
# File 'lib/spreadsheet/workbook.rb', line 15

def worksheets
  @worksheets
end

Instance Method Details

#add_font(font) ⇒ Object

Add a Font to the Workbook. Used by the parser. You should not need to use this Method.



29
30
31
32
# File 'lib/spreadsheet/workbook.rb', line 29

def add_font font
  @fonts.push(font).uniq! if font
  font
end

#add_format(format) ⇒ Object

Add a Format to the Workbook. If you use Row#set_format, you should not need to use this Method.



36
37
38
39
# File 'lib/spreadsheet/workbook.rb', line 36

def add_format format
  @formats.push(format) if format && !@formats.include?(format)
  format
end

#add_worksheet(worksheet) ⇒ Object

Add a Worksheet to the Workbook.



42
43
44
45
46
# File 'lib/spreadsheet/workbook.rb', line 42

def add_worksheet worksheet
  worksheet.workbook = self
  @worksheets.push worksheet
  worksheet
end

#create_worksheet(opts = {}) ⇒ Object

Create a new Worksheet in this Workbook. Used without options this creates a Worksheet with the name ‘WorksheetN’ where the new Worksheet is the Nth Worksheet in this Workbook.

Use the option :name => ‘My pretty Name’ to override this behavior.



54
55
56
57
# File 'lib/spreadsheet/workbook.rb', line 54

def create_worksheet opts = {}
  opts[:name] ||= client("Worksheet#{@worksheets.size.next}", 'UTF-8')
  add_worksheet Worksheet.new(opts)
end

#font(idx) ⇒ Object

The Font at idx



66
67
68
# File 'lib/spreadsheet/workbook.rb', line 66

def font idx
  @fonts[idx]
end

#format(idx) ⇒ Object

The Format at idx, or - if idx is a String - the Format with name == idx



72
73
74
75
76
77
78
79
# File 'lib/spreadsheet/workbook.rb', line 72

def format idx
  case idx
  when Integer
    @formats[idx] || @default_format
  when String
    @formats.find do |fmt| fmt.name == idx end
  end
end

#inspectObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/spreadsheet/workbook.rb', line 80

def inspect
  variables = (instance_variables - uninspect_variables).collect do |name|
    "%s=%s" % [name, instance_variable_get(name)]
  end.join(' ')
  uninspect = uninspect_variables.collect do |name|
    var = instance_variable_get name
    "%s=%s[%i]" % [name, var.class, var.size]
  end.join(' ')
  sprintf "#<%s:0x%014x %s %s>", self.class, object_id,
                                 variables, uninspect
end

#sheet_countObject

Returns the count of total worksheets present. Takes no arguments. Just returns the length of @worksheets array.



61
62
63
# File 'lib/spreadsheet/workbook.rb', line 61

def sheet_count
@worksheets.length
end

#uninspect_variablesObject

:nodoc:



91
92
93
# File 'lib/spreadsheet/workbook.rb', line 91

def uninspect_variables # :nodoc:
  %w{@formats @fonts @worksheets}
end

#worksheet(idx) ⇒ Object

The Worksheet at idx, or - if idx is a String - the Worksheet with name == idx



97
98
99
100
101
102
103
104
# File 'lib/spreadsheet/workbook.rb', line 97

def worksheet idx
  case idx
  when Integer
    @worksheets[idx]
  when String
    @worksheets.find do |sheet| sheet.name == idx end
  end
end

#write(io_path_or_writer) ⇒ Object

Write this Workbook to a File, IO Stream or Writer Object. The latter will make more sense once there are more than just an Excel-Writer available.



108
109
110
111
112
113
114
# File 'lib/spreadsheet/workbook.rb', line 108

def write io_path_or_writer
  if io_path_or_writer.is_a? Writer
    io_path_or_writer.write self
  else
    writer(io_path_or_writer).write(self)
  end
end

#writer(io_or_path, type = Excel, version = self.version) ⇒ Object

Returns a new instance of the default Writer class for this Workbook (can only be an Excel::Writer::Workbook at this time)



118
119
120
121
122
123
124
# File 'lib/spreadsheet/workbook.rb', line 118

def writer io_or_path, type=Excel, version=self.version
  if type == Excel
    Excel::Writer::Workbook.new io_or_path
  else
    raise NotImplementedError, "No Writer defined for #{type}"
  end
end