Class: Rbxl::WriteOnlyWorkbook

Inherits:
Object
  • Object
show all
Defined in:
lib/rbxl/write_only_workbook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriteOnlyWorkbook

Returns a new instance of WriteOnlyWorkbook.



5
6
7
8
9
# File 'lib/rbxl/write_only_workbook.rb', line 5

def initialize
  @worksheets = []
  @closed = false
  @saved = false
end

Instance Attribute Details

#worksheetsObject (readonly)

Returns the value of attribute worksheets.



3
4
5
# File 'lib/rbxl/write_only_workbook.rb', line 3

def worksheets
  @worksheets
end

Instance Method Details

#add_sheet(name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rbxl/write_only_workbook.rb', line 11

def add_sheet(name)
  ensure_writable!

  sheet = WriteOnlyWorksheet.new(name: name)
  @worksheets << sheet
  sheet
end

#closeObject



40
41
42
# File 'lib/rbxl/write_only_workbook.rb', line 40

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rbxl/write_only_workbook.rb', line 44

def closed?
  @closed
end

#save(path) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbxl/write_only_workbook.rb', line 19

def save(path)
  ensure_writable!
  raise Error, "at least one worksheet is required" if worksheets.empty?

  Zip::OutputStream.open(path) do |zip|
    write_entry(zip, "[Content_Types].xml", content_types_xml)
    write_entry(zip, "_rels/.rels", root_rels_xml)
    write_entry(zip, "xl/workbook.xml", workbook_xml)
    write_entry(zip, "xl/_rels/workbook.xml.rels", workbook_rels_xml)
    write_entry(zip, "xl/styles.xml", styles_xml)

    worksheets.each_with_index do |sheet, index|
      write_entry(zip, "xl/worksheets/sheet#{index + 1}.xml", sheet.to_xml)
    end
  end

  @saved = true
  close
  path
end