Class | Spreadsheet::Workbook |
In: |
lib/spreadsheet/workbook.rb
|
Parent: | Object |
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. |
active_worksheet | [RW] | |
default_format | [RW] | |
encoding | [RW] | |
fonts | [R] | |
formats | [R] | |
io | [R] | |
version | [RW] | |
worksheets | [R] |
# File lib/spreadsheet/workbook.rb, line 17 17: def initialize io = nil, opts={:default_format => Format.new} 18: @worksheets = [] 19: @io = io 20: @fonts = [] 21: @formats = [] 22: if @default_format = opts[:default_format] 23: @formats.push @default_format 24: end 25: end
Add a Format to the Workbook. If you use Row#set_format, you should not need to use this Method.
# File lib/spreadsheet/workbook.rb, line 36 36: def add_format format 37: @formats.push(format) if format && !@formats.include?(format) 38: format 39: end
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 54 54: def create_worksheet opts = {} 55: opts[:name] ||= client("Worksheet#{@worksheets.size.next}", 'UTF-8') 56: add_worksheet Worksheet.new(opts) 57: end
# File lib/spreadsheet/workbook.rb, line 74 74: def inspect 75: variables = (instance_variables - uninspect_variables).collect do |name| 76: "%s=%s" % [name, instance_variable_get(name)] 77: end.join(' ') 78: uninspect = uninspect_variables.collect do |name| 79: var = instance_variable_get name 80: "%s=%s[%i]" % [name, var.class, var.size] 81: end.join(' ') 82: sprintf "#<%s:0x%014x %s %s>", self.class, object_id, 83: variables, uninspect 84: end
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 102 102: def write io_path_or_writer 103: if io_path_or_writer.is_a? Writer 104: io_path_or_writer.write self 105: else 106: writer(io_path_or_writer).write(self) 107: end 108: end
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 112 112: def writer io_or_path, type=Excel, version=self.version 113: if type == Excel 114: Excel::Writer::Workbook.new io_or_path 115: else 116: raise NotImplementedError, "No Writer defined for #{type}" 117: end 118: end