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.
-
#palette ⇒ Object
readonly
Returns the value of attribute palette.
-
#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.
-
#delete_worksheet(worksheet_index) ⇒ Object
Delete a Worksheet from Workbook by it’s index.
-
#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
-
#set_custom_color(idx, red, green, blue) ⇒ Object
Change the RGB components of the elements in the colour palette.
-
#sheet_count ⇒ Object
Returns the count of total worksheets present.
-
#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 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
#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 |
#palette ⇒ Object (readonly)
Returns the value of attribute palette.
15 16 17 |
# File 'lib/spreadsheet/workbook.rb', line 15 def palette @palette 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.
31 32 33 34 |
# File 'lib/spreadsheet/workbook.rb', line 31 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.
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 |
#add_worksheet(worksheet) ⇒ Object
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 |
#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.
71 72 73 74 |
# 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) ⇒ Object
Delete a Worksheet from Workbook by it’s index
54 55 56 |
# File 'lib/spreadsheet/workbook.rb', line 54 def delete_worksheet worksheet_index @worksheets.delete_at worksheet_index end |
#font(idx) ⇒ Object
The Font at idx
83 84 85 |
# File 'lib/spreadsheet/workbook.rb', line 83 def font idx @fonts[idx] end |
#format(idx) ⇒ Object
The Format at idx, or - if idx is a String - the Format with name == idx
89 90 91 92 93 94 95 96 |
# 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 ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
Change the RGB components of the elements in the colour palette.
59 60 61 62 63 |
# 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 ⇒ Object
Returns the count of total worksheets present. Takes no arguments. Just returns the length of @worksheets array.
78 79 80 |
# File 'lib/spreadsheet/workbook.rb', line 78 def sheet_count @worksheets.length end |
#uninspect_variables ⇒ Object
:nodoc:
108 109 110 |
# File 'lib/spreadsheet/workbook.rb', line 108 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
114 115 116 117 118 119 120 121 |
# 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) ⇒ 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.
125 126 127 128 129 130 131 |
# 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) ⇒ Object
Returns a new instance of the default Writer class for this Workbook (can only be an Excel::Writer::Workbook at this time)
135 136 137 138 139 140 141 |
# 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 |