Class: Rbxl::WriteOnlyWorkbook
- Inherits:
-
Object
- Object
- Rbxl::WriteOnlyWorkbook
- 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
-
#worksheets ⇒ Array<Rbxl::WriteOnlyWorksheet>
readonly
Worksheets in insertion order.
Instance Method Summary collapse
-
#add_sheet(name) ⇒ Rbxl::WriteOnlyWorksheet
Creates and returns a new worksheet appended to this workbook.
-
#close ⇒ Boolean
Marks the workbook as closed.
-
#closed? ⇒ Boolean
Whether the workbook has been closed.
-
#initialize ⇒ WriteOnlyWorkbook
constructor
Creates an empty write-only workbook with no worksheets.
-
#save(path) ⇒ String
Serializes the workbook to an
.xlsxfile atpath.
Constructor Details
#initialize ⇒ WriteOnlyWorkbook
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
#worksheets ⇒ Array<Rbxl::WriteOnlyWorksheet> (readonly)
Returns worksheets in insertion order.
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.
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 |
#close ⇒ Boolean
Marks the workbook as closed. Further mutating operations raise ClosedWorkbookError. This is called automatically by a successful #save.
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.
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.
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 |