Class: Rbxl::WriteOnlyWorksheet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ WriteOnlyWorksheet

Returns a new instance of WriteOnlyWorksheet.



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

def initialize(name:)
  @name = name
  @rows = []
  @column_name_cache = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#<<(values) ⇒ Object



11
12
13
# File 'lib/rbxl/write_only_worksheet.rb', line 11

def <<(values)
  append(values)
end

#append(values) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/rbxl/write_only_worksheet.rb', line 15

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_xmlObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbxl/write_only_worksheet.rb', line 24

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