Class: Rbxl::WriteOnlyWorkbook

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

Overview

Write-only workbook for single-pass XLSX generation.

The workbook accumulates rows per worksheet and emits the full .xlsx package in a single pass when #save is called. By design a write-only workbook can only be saved once: #save calls #close on success, and any subsequent call raises WorkbookAlreadySavedError.

book  = Rbxl.new(write_only: true)
sheet = book.add_sheet("Report")
sheet.append(["id", "name"])
sheet.append([1, "alice"])
book.save("report.xlsx")

Style output is intentionally minimal: a single default style entry is emitted so that authored style_id references resolve, but arbitrary workbook styling is out of scope for the MVP API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriteOnlyWorkbook

Creates an empty write-only workbook with no worksheets.



24
25
26
27
28
# File 'lib/rbxl/write_only_workbook.rb', line 24

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

Instance Attribute Details

#worksheetsArray<Rbxl::WriteOnlyWorksheet> (readonly)

Returns worksheets in insertion order.

Returns:



21
22
23
# File 'lib/rbxl/write_only_workbook.rb', line 21

def worksheets
  @worksheets
end

Instance Method Details

#add_sheet(name) ⇒ Rbxl::WriteOnlyWorksheet

Creates and returns a new worksheet appended to this workbook.

Parameters:

  • name (String)

    visible sheet name

Returns:

Raises:



36
37
38
39
40
41
42
# File 'lib/rbxl/write_only_workbook.rb', line 36

def add_sheet(name)
  ensure_writable!

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

#closeBoolean

Marks the workbook as closed. Further mutating operations raise ClosedWorkbookError. This is called automatically by a successful #save.

Returns:

  • (Boolean)

    the new closed state (always true)



87
88
89
# File 'lib/rbxl/write_only_workbook.rb', line 87

def close
  @closed = true
end

#closed?Boolean

Returns whether the workbook has been closed.

Returns:

  • (Boolean)

    whether the workbook has been closed



92
93
94
# File 'lib/rbxl/write_only_workbook.rb', line 92

def closed?
  @closed
end

#save(path) ⇒ String

Serializes the workbook to an .xlsx file at path.

On success the workbook is closed automatically; the method returns the path that was written, suitable for chaining.

Parameters:

  • path (String, #to_path)

    destination filesystem path

Returns:

  • (String)

    the saved path

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbxl/write_only_workbook.rb', line 54

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

  previous_zip64 = Zip.write_zip64_support
  begin
    Zip.write_zip64_support = false

    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
  ensure
    Zip.write_zip64_support = previous_zip64
  end

  @saved = true
  close
  path
end