Class: Xmlss::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlss/writer.rb

Defined Under Namespace

Classes: AttrsHash, Markup

Constant Summary collapse

XML_NS =

Xmlss uses Undies to stream its xml markup The Undies writer is responsible for driving the Undies API to generate the xmlss xml markup for the workbook. Because order doesn’t matter when defining style and worksheet elements, the writer has to buffer the style and worksheet markup separately, then put them together according to Xmlss spec to build the final workbook markup.

"xmlns"
SHEET_NS =
"ss"
NS_URI =
"urn:schemas-microsoft-com:office:spreadsheet"
LB =
"
"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_opts = {}) ⇒ Writer

Returns a new instance of Writer.



23
24
25
26
27
28
# File 'lib/xmlss/writer.rb', line 23

def initialize(output_opts={})
  @opts = output_opts || {}

  @styles_markup     = Markup.new(@opts.merge(:level => 2))
  @worksheets_markup = Markup.new(@opts.merge(:level => 1))
end

Instance Attribute Details

#styles_markupObject (readonly)

Returns the value of attribute styles_markup.



20
21
22
# File 'lib/xmlss/writer.rb', line 20

def styles_markup
  @styles_markup
end

#worksheets_markupObject (readonly)

Returns the value of attribute worksheets_markup.



21
22
23
# File 'lib/xmlss/writer.rb', line 21

def worksheets_markup
  @worksheets_markup
end

Instance Method Details

#alignment(alignment) ⇒ Object

workbook style markup directives



71
72
73
74
75
76
77
78
79
# File 'lib/xmlss/writer.rb', line 71

def alignment(alignment)
  styles_markup.inline_element("Alignment", AttrsHash.new.
    value("Horizontal", alignment.horizontal).
    value("Vertical",   alignment.vertical).
    value("Rotate",     alignment.rotate).
    bool( "WrapText",   alignment.wrap_text).
    raw
  )
end

#border(border) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/xmlss/writer.rb', line 81

def border(border)
  styles_markup.inline_element("Border", AttrsHash.new.
    value("Color",     border.color).
    value("Position",  border.position).
    value("Weight",    border.weight).
    value("LineStyle", border.line_style).
    raw
  )
end

#borders(borders) ⇒ Object



91
92
93
# File 'lib/xmlss/writer.rb', line 91

def borders(borders)
  styles_markup.element("Borders", nil, {})
end

#cell(cell) ⇒ Object

workbook element markup directives



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xmlss/writer.rb', line 136

def cell(cell)
  # write the cell markup and push
  worksheets_markup.element("Cell", nil, AttrsHash.new.
    value("Index",       cell.index).
    value("StyleID",     cell.style_id).
    value("Formula",     cell.formula).
    value("HRef",        cell.href).
    value("MergeAcross", cell.merge_across).
    value("MergeDown",   cell.merge_down).
    raw
  )
  push(:worksheets)

  # write nested data markup and push
  worksheets_markup.element(
    "Data",
    worksheets_markup.raw(cell.data_xml_value),
    AttrsHash.new.value("Type", cell.type).raw
  )

  pop(:worksheets)
end

#column(column) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/xmlss/writer.rb', line 169

def column(column)
  worksheets_markup.inline_element("Column", AttrsHash.new.
    value("StyleID",      column.style_id).
    value("Width",        column.width).
    bool( "AutoFitWidth", column.auto_fit_width).
    bool( "Hidden",       column.hidden).
    raw
  )
end

#flushObject



42
43
44
45
46
47
48
# File 'lib/xmlss/writer.rb', line 42

def flush
  # flush the worksheets markup first b/c it may add style elements to
  # the styles markup
  worksheets_markup.flush
  styles_markup.flush
  self
end

#font(font) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/xmlss/writer.rb', line 95

def font(font)
  styles_markup.inline_element("Font", AttrsHash.new.
    bool( "Bold",          font.bold).
    value("Color",         font.color).
    bool( "Italic",        font.italic).
    value("Size",          font.size).
    bool( "Shadow",        font.shadow).
    value("FontName",      font.name).
    bool( "StrikeThrough", font.strike_through).
    value("Underline",     font.underline).
    value("VerticalAlign", font.alignment).
    raw
  )
end

#interior(interior) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/xmlss/writer.rb', line 110

def interior(interior)
  styles_markup.inline_element("Interior", AttrsHash.new.
    value("Color",        interior.color).
    value("Pattern",      interior.pattern).
    value("PatternColor", interior.pattern_color).
    raw
  )
end

#number_format(number_format) ⇒ Object



119
120
121
122
# File 'lib/xmlss/writer.rb', line 119

def number_format(number_format)
  a = AttrsHash.new.value("Format", number_format.format).raw
  styles_markup.inline_element("NumberFormat", a)
end

#pop(template) ⇒ Object



38
39
40
# File 'lib/xmlss/writer.rb', line 38

def pop(template)
  self.send("#{template}_markup").pop
end

#protection(protection) ⇒ Object



124
125
126
127
# File 'lib/xmlss/writer.rb', line 124

def protection(protection)
  a = AttrsHash.new.bool("Protect", protection.protect).raw
  styles_markup.inline_element("Protection", a)
end

#push(template) ⇒ Object



34
35
36
# File 'lib/xmlss/writer.rb', line 34

def push(template)
  self.send("#{template}_markup").push
end

#row(row) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/xmlss/writer.rb', line 159

def row(row)
  worksheets_markup.element("Row", nil, AttrsHash.new.
    value("StyleID",       row.style_id).
    value("Height",        row.height).
    bool( "AutoFitHeight", row.auto_fit_height).
    bool( "Hidden",        row.hidden).
    raw
  )
end

#style(style) ⇒ Object



129
130
131
132
# File 'lib/xmlss/writer.rb', line 129

def style(style)
  a = AttrsHash.new.value("ID", style.id).raw
  styles_markup.element("Style", nil, a)
end

#workbookObject

return the full workbook markup, combining the buffers to xmlss spec



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xmlss/writer.rb', line 51

def workbook
  self.flush
  "".tap do |markup|
    Undies::Template.new(Undies::Source.new(Proc.new do
      _ raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
      __open_element("Workbook", XML_NS => NS_URI, "#{XML_NS}:#{SHEET_NS}" => NS_URI) {
        __open_element("Styles") {
          __partial @styles
        }
        __partial @worksheets
      }
    end), {
      :styles => styles_markup.to_s,
      :worksheets => worksheets_markup.to_s
    }, Undies::IO.new(markup, @opts))
  end.strip
end

#worksheet(worksheet) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/xmlss/writer.rb', line 179

def worksheet(worksheet)
  # flush any previous worksheet markup
  worksheets_markup.flush

  # write the worksheet markup and push
  a = AttrsHash.new.value("Name", worksheet.name).raw
  worksheets_markup.element("Worksheet", nil, a)
  push(:worksheets)

  # write the table container
  worksheets_markup.element("Table", nil, {})
end

#write(element) ⇒ Object



30
31
32
# File 'lib/xmlss/writer.rb', line 30

def write(element)
  self.send(element.class.writer, element)
end