class Spreadsheet::Workbook

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 Spreadsheet::Row#default_format or Spreadsheet::Worksheet#default_format.

Attributes

active_worksheet[RW]
default_format[RW]
encoding[RW]
fonts[R]
formats[R]
io[R]
palette[R]
version[RW]
worksheets[R]

Public Class Methods

new(io = nil, opts={:default_format => Format.new}) click to toggle source
# 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

Public Instance Methods

add_font(font) click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 31
def add_font font
  @fonts.push(font).uniq! if font
  font
end
add_format(format) click to toggle source

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

# 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
add_worksheet(worksheet) click to toggle source

Add a Worksheet to the Workbook.

# File lib/spreadsheet/workbook.rb, line 47
def add_worksheet worksheet
  worksheet.workbook = self
  @worksheets.push worksheet
  worksheet
end
create_worksheet(opts = {}) click to toggle source

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.

# File lib/spreadsheet/workbook.rb, line 71
def create_worksheet opts = {}
  opts[:name] ||= client("Worksheet#{@worksheets.size.next}", 'UTF-8')
  add_worksheet Worksheet.new(opts)
end
delete_worksheet(worksheet_index) click to toggle source

Delete a Worksheet from Workbook by it’s index

# File lib/spreadsheet/workbook.rb, line 54
def delete_worksheet worksheet_index
  @worksheets.delete_at worksheet_index
end
font(idx) click to toggle source

The Font at idx

# File lib/spreadsheet/workbook.rb, line 83
def font idx
  @fonts[idx]
end
format(idx) click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 89
def format idx
  case idx
  when Integer
    @formats[idx] || @default_format || Format.new
  when String
    @formats.find do |fmt| fmt.name == idx end
  end
end
inspect() click to toggle source
# File lib/spreadsheet/workbook.rb, line 97
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
set_custom_color(idx, red, green, blue) click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 59
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
sheet_count() click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 78
def sheet_count
@worksheets.length
end
worksheet(idx) click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 114
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) click to toggle source

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.

# File lib/spreadsheet/workbook.rb, line 125
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) click to toggle source

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

# File lib/spreadsheet/workbook.rb, line 135
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