Class: Rbxl::WriteOnlyWorksheet
- Inherits:
-
Object
- Object
- Rbxl::WriteOnlyWorksheet
- Defined in:
- lib/rbxl/write_only_worksheet.rb
Overview
Worksheet builder used by WriteOnlyWorkbook.
Rows are appended in order and later serialized as SpreadsheetML by #to_xml when the workbook is saved. The builder never rewrites a previously appended row, so the worksheet’s in-memory footprint scales linearly with the number of appended rows.
Row values
Each element of an appended row may be one of:
-
nil— serialized as an empty cell -
true/false— serialized as a boolean cell -
Integer/Numeric— serialized as a numeric cell -
Date/DateTime/Time— serialized as an ISO-8601 inline string -
any other object — serialized as
value.to_sin an inline string -
WriteOnlyCell — same as the above, but with an optional style id
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Visible sheet name.
Instance Method Summary collapse
-
#<<(values) ⇒ Rbxl::WriteOnlyWorksheet
Appends a row of values.
-
#append(values) ⇒ Rbxl::WriteOnlyWorksheet
Appends a row of values.
-
#initialize(name:) ⇒ WriteOnlyWorksheet
constructor
A new instance of WriteOnlyWorksheet.
-
#to_xml ⇒ String
Serializes the worksheet to SpreadsheetML.
Constructor Details
#initialize(name:) ⇒ WriteOnlyWorksheet
Returns a new instance of WriteOnlyWorksheet.
24 25 26 27 28 |
# File 'lib/rbxl/write_only_worksheet.rb', line 24 def initialize(name:) @name = name @rows = [] @column_name_cache = [] end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns visible sheet name.
21 22 23 |
# File 'lib/rbxl/write_only_worksheet.rb', line 21 def name @name end |
Instance Method Details
#<<(values) ⇒ Rbxl::WriteOnlyWorksheet
Appends a row of values. Equivalent to #append so that the shell operator reads naturally at the call site:
sheet << ["id", "name"]
38 39 40 |
# File 'lib/rbxl/write_only_worksheet.rb', line 38 def <<(values) append(values) end |
#append(values) ⇒ Rbxl::WriteOnlyWorksheet
Appends a row of values.
48 49 50 51 52 53 54 55 |
# File 'lib/rbxl/write_only_worksheet.rb', line 48 def append(values) unless values.is_a?(Array) || values.is_a?(Enumerator) raise TypeError, "row must be an Array or Enumerator, got #{values.class}" end @rows << Array(values) self end |
#to_xml ⇒ String
Serializes the worksheet to SpreadsheetML.
When require "rbxl/native" has been loaded the native extension handles serialization for a significant speedup; otherwise a pure-Ruby implementation is used. Both paths produce equivalent output.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rbxl/write_only_worksheet.rb', line 65 def to_xml if defined?(Rbxl::Native) return Rbxl::Native.generate_sheet(@rows) end dimension_ref = @rows.empty? ? "A1" : "A1:#{column_name(max_columns)}#{@rows.length}" buf = +"" buf << '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' buf << "\n" buf << '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' buf << "\n" buf << ' <dimension ref="' buf << dimension_ref buf << '"/>' buf << "\n" buf << ' <sheetData>' @rows.each_with_index do |row_values, row_index| row_num_str = (row_index + 1).to_s buf << '<row r="' buf << row_num_str buf << '">' row_values.each_with_index do |value, col_index| serialize_cell_to(buf, column_name(col_index + 1), row_num_str, value) end buf << '</row>' end buf << "</sheetData>\n</worksheet>" buf end |