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.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spreadsheet/workbook.rb', line 18 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.
17 18 19 |
# File 'lib/spreadsheet/workbook.rb', line 17 def active_worksheet @active_worksheet end |
#default_format ⇒ Object
Returns the value of attribute default_format.
17 18 19 |
# File 'lib/spreadsheet/workbook.rb', line 17 def default_format @default_format end |
#encoding ⇒ Object
Returns the value of attribute encoding.
17 18 19 |
# File 'lib/spreadsheet/workbook.rb', line 17 def encoding @encoding end |
#fonts ⇒ Object (readonly)
Returns the value of attribute fonts.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def fonts @fonts end |
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def formats @formats end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def io @io end |
#palette ⇒ Object (readonly)
Returns the value of attribute palette.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 def palette @palette end |
#version ⇒ Object
Returns the value of attribute version.
17 18 19 |
# File 'lib/spreadsheet/workbook.rb', line 17 def version @version end |
#worksheets ⇒ Object (readonly)
Returns the value of attribute worksheets.
16 17 18 |
# File 'lib/spreadsheet/workbook.rb', line 16 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.
33 34 35 36 |
# File 'lib/spreadsheet/workbook.rb', line 33 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.
41 42 43 44 45 46 47 |
# File 'lib/spreadsheet/workbook.rb', line 41 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.
51 52 53 54 55 |
# File 'lib/spreadsheet/workbook.rb', line 51 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.
78 79 80 81 |
# File 'lib/spreadsheet/workbook.rb', line 78 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
59 60 61 |
# File 'lib/spreadsheet/workbook.rb', line 59 def delete_worksheet worksheet_index @worksheets.delete_at worksheet_index end |
#font(idx) ⇒ Object
The Font at idx
92 93 94 |
# File 'lib/spreadsheet/workbook.rb', line 92 def font idx @fonts[idx] end |
#format(idx) ⇒ Object
The Format at idx, or - if idx is a String - the Format with name == idx
99 100 101 102 103 104 105 106 |
# File 'lib/spreadsheet/workbook.rb', line 99 def format idx case idx when Integer @formats[idx] || @default_format || Format.new when String @formats.find { |fmt| fmt.name == idx } end end |
#inspect ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/spreadsheet/workbook.rb', line 108 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.
65 66 67 68 69 |
# File 'lib/spreadsheet/workbook.rb', line 65 def set_custom_color idx, red, green, blue raise "Invalid format" if [red, green, blue].find { |c| !(0..255).cover?(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.
86 87 88 |
# File 'lib/spreadsheet/workbook.rb', line 86 def sheet_count @worksheets.length end |
#uninspect_variables ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/spreadsheet/workbook.rb', line 120 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
127 128 129 130 131 132 133 134 |
# File 'lib/spreadsheet/workbook.rb', line 127 def worksheet idx case idx when Integer @worksheets[idx] when String @worksheets.find { |sheet| sheet.name == idx } 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.
139 140 141 142 143 144 145 |
# File 'lib/spreadsheet/workbook.rb', line 139 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)
150 151 152 153 154 155 156 |
# File 'lib/spreadsheet/workbook.rb', line 150 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 |