Class: Spreadsheet::Workbook
- Inherits:
-
Object
- Object
- Spreadsheet::Workbook
- 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
Instance Attribute Summary collapse
-
#active_worksheet ⇒ Object
Returns the value of attribute active_worksheet.
-
#default_format ⇒ Object
Returns the value of attribute default_format.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#fonts ⇒ Object
readonly
Returns the value of attribute fonts.
-
#formats ⇒ Object
readonly
Returns the value of attribute formats.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#version ⇒ Object
Returns the value of attribute version.
-
#worksheets ⇒ Object
readonly
Returns the value of attribute worksheets.
Instance Method Summary collapse
-
#add_font(font) ⇒ Object
Add a Font to the Workbook.
-
#add_format(format) ⇒ Object
Add a Format to the Workbook.
-
#add_worksheet(worksheet) ⇒ Object
Add a Worksheet to the Workbook.
-
#create_worksheet(opts = {}) ⇒ Object
Create a new Worksheet in this Workbook.
-
#font(idx) ⇒ Object
The Font at idx.
-
#format(idx) ⇒ Object
The Format at idx, or - if idx is a String - the Format with name == idx.
-
#initialize(io = nil, opts = {:default_format => Format.new}) ⇒ Workbook
constructor
A new instance of Workbook.
- #inspect ⇒ Object
-
#uninspect_variables ⇒ Object
:nodoc:.
-
#worksheet(idx) ⇒ Object
The Worksheet at idx, or - if idx is a String - the Worksheet with name == idx.
-
#write(io_path_or_writer) ⇒ Object
Write this Workbook to a File, IO Stream or Writer Object.
-
#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).
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_worksheet ⇒ Object
Returns the value of attribute active_worksheet.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def active_worksheet @active_worksheet end |
#default_format ⇒ Object
Returns the value of attribute default_format.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def default_format @default_format end |
#encoding ⇒ Object
Returns the value of attribute encoding.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def encoding @encoding end |
#fonts ⇒ Object (readonly)
Returns the value of attribute fonts.
15 16 17 |
# File 'lib/spreadsheet/workbook.rb', line 15 def fonts @fonts end |
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
15 16 17 |
# File 'lib/spreadsheet/workbook.rb', line 15 def formats @formats end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
15 16 17 |
# File 'lib/spreadsheet/workbook.rb', line 15 def io @io end |
#version ⇒ Object
Returns the value of attribute version.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def version @version end |
#worksheets ⇒ Object (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
60 61 62 |
# File 'lib/spreadsheet/workbook.rb', line 60 def font idx @fonts[idx] end |
#format(idx) ⇒ Object
The Format at idx, or - if idx is a String - the Format with name == idx
66 67 68 69 70 71 72 73 |
# File 'lib/spreadsheet/workbook.rb', line 66 def format idx case idx when Integer @formats[idx] || @default_format when String @formats.find do |fmt| fmt.name == idx end end end |
#inspect ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/spreadsheet/workbook.rb', line 74 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 |
#uninspect_variables ⇒ Object
:nodoc:
85 86 87 |
# File 'lib/spreadsheet/workbook.rb', line 85 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
91 92 93 94 95 96 97 98 |
# File 'lib/spreadsheet/workbook.rb', line 91 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.
102 103 104 105 106 107 108 |
# File 'lib/spreadsheet/workbook.rb', line 102 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)
112 113 114 115 116 117 118 |
# File 'lib/spreadsheet/workbook.rb', line 112 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 |