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

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

A new instance of Workbook



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

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

Instance Attribute Details

- (Object) active_worksheet

Returns the value of attribute active_worksheet



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

def active_worksheet
  @active_worksheet
end

- (Object) default_format

Returns the value of attribute default_format



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

def default_format
  @default_format
end

- (Object) encoding

Returns the value of attribute encoding



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

def encoding
  @encoding
end

- (Object) fonts (readonly)

Returns the value of attribute fonts



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

def fonts
  @fonts
end

- (Object) formats (readonly)

Returns the value of attribute formats



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

def formats
  @formats
end

- (Object) io (readonly)

Returns the value of attribute io



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

def io
  @io
end

- (Object) palette (readonly)

Returns the value of attribute palette



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

def palette
  @palette
end

- (Object) version

Returns the value of attribute version



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

def version
  @version
end

- (Object) worksheets (readonly)

Returns the value of attribute worksheets



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

def worksheets
  @worksheets
end

Instance Method Details

- (Object) add_font(font)

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



31
32
33
34
# File 'lib/spreadsheet/workbook.rb', line 31

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

- (Object) add_format(format)

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



38
39
40
41
42
43
44
# File 'lib/spreadsheet/workbook.rb', line 38

def add_format format
  if format && !@formats_set[format]
    @formats_set[format] = true
    @formats.push(format)
  end
  format
end

- (Object) add_worksheet(worksheet)

Add a Worksheet to the Workbook.



47
48
49
50
51
# File 'lib/spreadsheet/workbook.rb', line 47

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

- (Object) create_worksheet(opts = {})

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.



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

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

- (Object) font(idx)

The Font at idx



78
79
80
# File 'lib/spreadsheet/workbook.rb', line 78

def font idx
  @fonts[idx]
end

- (Object) format(idx)

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



84
85
86
87
88
89
90
91
# File 'lib/spreadsheet/workbook.rb', line 84

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

- (Object) inspect



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/spreadsheet/workbook.rb', line 92

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

- (Object) set_custom_color(idx, red, green, blue)

Change the RGB components of the elements in the colour palette.



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

def set_custom_color idx, red, green, blue
  raise 'Invalid format' if [red, green, blue].find { |c| ! (0..255).include?(c) }

  @palette[idx] = [red, green, blue]
end

- (Object) sheet_count

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



73
74
75
# File 'lib/spreadsheet/workbook.rb', line 73

def sheet_count
@worksheets.length
end

- (Object) uninspect_variables

:nodoc:



103
104
105
# File 'lib/spreadsheet/workbook.rb', line 103

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

- (Object) worksheet(idx)

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



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

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

- (Object) write(io_path_or_writer)

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.



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

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

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

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



130
131
132
133
134
135
136
# File 'lib/spreadsheet/workbook.rb', line 130

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