Class: Xlsxtream::Workbook
- Inherits:
-
Object
- Object
- Xlsxtream::Workbook
- Defined in:
- lib/xlsxtream/workbook.rb
Constant Summary collapse
- FONT_FAMILY_IDS =
{ '' => 0, 'roman' => 1, 'swiss' => 2, 'modern' => 3, 'script' => 4, 'decorative' => 5 }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(output, options = {}) ⇒ Workbook
constructor
A new instance of Workbook.
- #write_worksheet(name = nil, options = {}) {|worksheet| ... } ⇒ Object (also: #add_worksheet)
Constructor Details
#initialize(output, options = {}) ⇒ Workbook
Returns a new instance of Workbook.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/xlsxtream/workbook.rb', line 37 def initialize(output, = {}) if output.nil? fail Error, "Xlsxtream::Workbook.new output cannot be nil" end @options = if [:io_wrapper] fail Deprecation, "The Xlsxtream::Workbook.new :io_wrapper option is deprecated. "\ "Please pass an IO wrapper instance as the first argument instead." end if output.is_a?(String) || !output.respond_to?(:<<) @file = File.open(output, 'wb') @io = IO::ZipTricks.new(@file) elsif output.respond_to? :add_file @file = nil @io = output else @file = nil @io = IO::ZipTricks.new(output) end @sst = SharedStringTable.new @worksheets = Hash.new { |hash, name| hash[name] = hash.size + 1 } end |
Class Method Details
.open(output, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/xlsxtream/workbook.rb', line 22 def open(output, = {}) workbook = new(output, ) if block_given? begin yield workbook ensure workbook.close end else workbook end end |
Instance Method Details
#close ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/xlsxtream/workbook.rb', line 83 def close write_workbook write_styles write_sst unless @sst.empty? write_workbook_rels write_root_rels write_content_types @io.close if @io.respond_to? :close @file.close if @file nil end |
#write_worksheet(name = nil, options = {}) {|worksheet| ... } ⇒ Object Also known as: add_worksheet
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/xlsxtream/workbook.rb', line 61 def write_worksheet(name = nil, = {}) if name.is_a? Hash and .empty? = name name = nil end use_sst = .fetch(:use_shared_strings, @options[:use_shared_strings]) auto_format = .fetch(:auto_format, @options[:auto_format]) columns = .fetch(:columns, @options[:columns]) sst = use_sst ? @sst : nil name = name || [:name] || "Sheet#{@worksheets.size + 1}" sheet_id = @worksheets[name] @io.add_file "xl/worksheets/sheet#{sheet_id}.xml" worksheet = Worksheet.new(@io, :sst => sst, :auto_format => auto_format, :columns => columns) yield worksheet if block_given? worksheet.close nil end |