xlsxrb 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/README.md +25 -3
  4. data/lib/xlsxrb/dsl_helpers.rb +65 -1
  5. data/lib/xlsxrb/elements/styles.rb +15 -6
  6. data/lib/xlsxrb/elements/worksheet.rb +2 -2
  7. data/lib/xlsxrb/number_formatter.rb +26 -8
  8. data/lib/xlsxrb/ooxml/cfb.rb +11 -3
  9. data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
  10. data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
  11. data/lib/xlsxrb/ooxml/crypto.rb +3 -0
  12. data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
  13. data/lib/xlsxrb/ooxml/utils.rb +13 -0
  14. data/lib/xlsxrb/ooxml/workbook_writer.rb +43 -21
  15. data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
  16. data/lib/xlsxrb/ooxml/worksheet_writer.rb +338 -259
  17. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
  18. data/lib/xlsxrb/ooxml/writer/features_xml.rb +109 -102
  19. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
  20. data/lib/xlsxrb/ooxml/writer.rb +167 -160
  21. data/lib/xlsxrb/ooxml/xml_builder.rb +35 -7
  22. data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
  23. data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
  24. data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
  25. data/lib/xlsxrb/stream_row.rb +2 -2
  26. data/lib/xlsxrb/stream_sheet.rb +26 -11
  27. data/lib/xlsxrb/stream_writer.rb +212 -86
  28. data/lib/xlsxrb/style_builder.rb +339 -0
  29. data/lib/xlsxrb/version.rb +1 -1
  30. data/lib/xlsxrb/workbook_builder.rb +239 -88
  31. data/lib/xlsxrb/worksheet_builder.rb +847 -139
  32. data/lib/xlsxrb.rb +87 -21
  33. data/sig/generated/xlsxrb/dsl_helpers.rbs +37 -0
  34. data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
  35. data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
  36. data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
  37. data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
  38. data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
  39. data/sig/generated/xlsxrb/ooxml/utils.rbs +10 -0
  40. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -0
  41. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
  42. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +53 -0
  43. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +3 -0
  44. data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
  45. data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -2
  46. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
  47. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
  48. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
  49. data/sig/generated/xlsxrb/stream_row.rbs +2 -2
  50. data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
  51. data/sig/generated/xlsxrb/stream_writer.rbs +46 -6
  52. data/sig/generated/xlsxrb/style_builder.rbs +261 -0
  53. data/sig/generated/xlsxrb/workbook_builder.rbs +42 -9
  54. data/sig/generated/xlsxrb/worksheet_builder.rbs +618 -4
  55. data/sig/generated/xlsxrb.rbs +53 -8
  56. metadata +9 -3
@@ -10,7 +10,7 @@ module Xlsxrb
10
10
  # : (untyped pivot_table, untyped cache_id) -> untyped
11
11
  def generate_pivot_table_xml(pivot_table, cache_id)
12
12
  data_caption = pivot_table[:data_caption] || (pivot_table[:data_fields].first ? pivot_table[:data_fields].first[:name] : "Values")
13
- pt_attrs = %( xmlns="#{SSML_NS}" name="#{xml_escape(pivot_table[:name])}" cacheId="#{cache_id}" dataCaption="#{xml_escape(data_caption)}")
13
+ pt_attrs = %( xmlns="#{SSML_NS}" name="#{xml_escape(pivot_table[:name])}" cacheId="#{xml_escape(cache_id)}" dataCaption="#{xml_escape(data_caption)}")
14
14
  pt_attrs << %( grandTotalCaption="#{xml_escape(pivot_table[:grand_total_caption])}") if pivot_table[:grand_total_caption]
15
15
  pt_attrs << %( errorCaption="#{xml_escape(pivot_table[:error_caption])}") if pivot_table[:error_caption]
16
16
  pt_attrs << ' showError="1"' if pivot_table[:show_error]
@@ -28,15 +28,15 @@ module Xlsxrb
28
28
  pt_attrs << %( showHeaders="0") if pivot_table[:show_headers] == false
29
29
  pt_attrs << %( showMultipleLabel="0") if pivot_table[:show_multiple_label] == false
30
30
  pt_attrs << %( showDataDropDown="0") if pivot_table[:show_data_drop_down] == false
31
- pt_attrs << %( indent="#{pivot_table[:indent]}") if pivot_table[:indent]
31
+ pt_attrs << %( indent="#{xml_escape(pivot_table[:indent])}") if pivot_table[:indent]
32
32
  pt_attrs << ' published="1"' if pivot_table[:published]
33
33
  pt_attrs << ' editData="1"' if pivot_table[:edit_data]
34
34
  pt_attrs << ' disableFieldList="1"' if pivot_table[:disable_field_list]
35
35
  pt_attrs << ' visualTotals="0"' if pivot_table[:visual_totals] == false
36
36
  pt_attrs << ' printDrill="1"' if pivot_table[:print_drill]
37
- pt_attrs << %( createdVersion="#{pivot_table[:created_version]}") if pivot_table[:created_version]
38
- pt_attrs << %( updatedVersion="#{pivot_table[:updated_version]}") if pivot_table[:updated_version]
39
- pt_attrs << %( minRefreshableVersion="#{pivot_table[:min_refreshable_version]}") if pivot_table[:min_refreshable_version]
37
+ pt_attrs << %( createdVersion="#{xml_escape(pivot_table[:created_version])}") if pivot_table[:created_version]
38
+ pt_attrs << %( updatedVersion="#{xml_escape(pivot_table[:updated_version])}") if pivot_table[:updated_version]
39
+ pt_attrs << %( minRefreshableVersion="#{xml_escape(pivot_table[:min_refreshable_version])}") if pivot_table[:min_refreshable_version]
40
40
  anf = pivot_table.fetch(:apply_number_formats, false)
41
41
  abf = pivot_table.fetch(:apply_border_formats, false)
42
42
  aff = pivot_table.fetch(:apply_font_formats, false)
@@ -58,7 +58,7 @@ module Xlsxrb
58
58
  pt_attrs << ' fieldPrintTitles="1"' if pivot_table[:field_print_titles]
59
59
  pt_attrs << ' preserveFormatting="0"' if pivot_table[:preserve_formatting] == false
60
60
  pt_attrs << ' pageOverThenDown="1"' if pivot_table[:page_over_then_down]
61
- pt_attrs << %( pageWrap="#{pivot_table[:page_wrap]}") if pivot_table[:page_wrap]
61
+ pt_attrs << %( pageWrap="#{xml_escape(pivot_table[:page_wrap])}") if pivot_table[:page_wrap]
62
62
  parts = [
63
63
  XML_HEADER,
64
64
  "<pivotTableDefinition#{pt_attrs}>"
@@ -70,13 +70,13 @@ module Xlsxrb
70
70
  else
71
71
  (pivot_table[:row_fields].size + pivot_table[:col_fields].size + pivot_table[:data_fields].size).clamp(1, 100)
72
72
  end
73
- loc_attrs = %(<location ref="#{pivot_table[:dest_ref]}" firstHeaderRow="#{pivot_table[:first_header_row] || 1}" ) +
74
- %(firstDataRow="#{pivot_table[:first_data_row] || 1}" firstDataCol="#{pivot_table[:first_data_col] || 1}")
75
- loc_attrs << %( rowPageCount="#{pivot_table[:row_page_count]}") if pivot_table[:row_page_count]
76
- loc_attrs << %( colPageCount="#{pivot_table[:col_page_count]}") if pivot_table[:col_page_count]
73
+ loc_attrs = %(<location ref="#{xml_escape(pivot_table[:dest_ref])}" firstHeaderRow="#{xml_escape(pivot_table[:first_header_row] || 1)}" ) +
74
+ %(firstDataRow="#{xml_escape(pivot_table[:first_data_row] || 1)}" firstDataCol="#{xml_escape(pivot_table[:first_data_col] || 1)}")
75
+ loc_attrs << %( rowPageCount="#{xml_escape(pivot_table[:row_page_count])}") if pivot_table[:row_page_count]
76
+ loc_attrs << %( colPageCount="#{xml_escape(pivot_table[:col_page_count])}") if pivot_table[:col_page_count]
77
77
  loc_attrs << "/>"
78
78
  parts << loc_attrs
79
- parts << %(<pivotFields count="#{field_count}">)
79
+ parts << %(<pivotFields count="#{xml_escape(field_count)}">)
80
80
  field_count.times do |fi|
81
81
  attrs = +""
82
82
  if pivot_table[:row_fields].include?(fi)
@@ -90,7 +90,7 @@ module Xlsxrb
90
90
  attrs << %( outline="#{fa[:outline] ? "1" : "0"}") if fa && !fa[:outline].nil?
91
91
  attrs << %( subtotalTop="#{fa[:subtotal_top] ? "1" : "0"}") if fa && !fa[:subtotal_top].nil?
92
92
  attrs << %( showAll="#{fa && fa[:show_all] == true ? "1" : "0"}")
93
- attrs << %( numFmtId="#{fa[:num_fmt_id]}") if fa && fa[:num_fmt_id]
93
+ attrs << %( numFmtId="#{xml_escape(fa[:num_fmt_id])}") if fa && fa[:num_fmt_id]
94
94
  attrs << %( sortType="#{xml_escape(fa[:sort_type])}") if fa && fa[:sort_type]
95
95
  attrs << ' defaultSubtotal="0"' if fa && fa[:default_subtotal] == false
96
96
  attrs << ' insertBlankRow="1"' if fa && fa[:insert_blank_row]
@@ -107,8 +107,8 @@ module Xlsxrb
107
107
  # default subtotal, which Excel fills in when it refreshes the cache on open.
108
108
  field_items ||= []
109
109
  parts << "<pivotField#{attrs}>"
110
- parts << %(<items count="#{field_items.size + (default_subtotal ? 1 : 0)}">)
111
- field_items.size.times { |ix| parts << %(<item x="#{ix}"/>) }
110
+ parts << %(<items count="#{xml_escape(field_items.size + (default_subtotal ? 1 : 0))}">)
111
+ field_items.size.times { |ix| parts << %(<item x="#{xml_escape(ix)}"/>) }
112
112
  parts << '<item t="default"/>' if default_subtotal
113
113
  parts << "</items>"
114
114
  parts << "</pivotField>"
@@ -120,24 +120,24 @@ module Xlsxrb
120
120
 
121
121
  unless pivot_table[:row_fields].empty?
122
122
  parts << %(<rowFields count="#{pivot_table[:row_fields].size}">)
123
- pivot_table[:row_fields].each { |f| parts << %(<field x="#{f}"/>) }
123
+ pivot_table[:row_fields].each { |f| parts << %(<field x="#{xml_escape(f)}"/>) }
124
124
  parts << "</rowFields>"
125
125
  end
126
126
 
127
127
  unless pivot_table[:col_fields].empty?
128
128
  parts << %(<colFields count="#{pivot_table[:col_fields].size}">)
129
- pivot_table[:col_fields].each { |f| parts << %(<field x="#{f}"/>) }
129
+ pivot_table[:col_fields].each { |f| parts << %(<field x="#{xml_escape(f)}"/>) }
130
130
  parts << "</colFields>"
131
131
  end
132
132
 
133
133
  unless pivot_table[:data_fields].empty?
134
134
  parts << %(<dataFields count="#{pivot_table[:data_fields].size}">)
135
135
  pivot_table[:data_fields].each do |df|
136
- df_attrs = %( name="#{xml_escape(df[:name])}" fld="#{df[:fld]}" subtotal="#{df[:subtotal] || "sum"}")
136
+ df_attrs = %( name="#{xml_escape(df[:name])}" fld="#{xml_escape(df[:fld])}" subtotal="#{xml_escape(df[:subtotal] || "sum")}")
137
137
  df_attrs << %( showDataAs="#{xml_escape(df[:show_data_as])}") if df[:show_data_as]
138
- df_attrs << %( baseField="#{df[:base_field]}") if df[:base_field]
139
- df_attrs << %( baseItem="#{df[:base_item]}") if df[:base_item]
140
- df_attrs << %( numFmtId="#{df[:num_fmt_id]}") if df[:num_fmt_id]
138
+ df_attrs << %( baseField="#{xml_escape(df[:base_field])}") if df[:base_field]
139
+ df_attrs << %( baseItem="#{xml_escape(df[:base_item])}") if df[:base_item]
140
+ df_attrs << %( numFmtId="#{xml_escape(df[:num_fmt_id])}") if df[:num_fmt_id]
141
141
  parts << "<dataField#{df_attrs}/>"
142
142
  end
143
143
  parts << "</dataFields>"
@@ -165,9 +165,9 @@ module Xlsxrb
165
165
  pcd_attrs << ' saveData="0"' if pivot_table[:cache_save_data] == false
166
166
  pcd_attrs << ' enableRefresh="0"' if pivot_table[:cache_enable_refresh] == false
167
167
  pcd_attrs << %( refreshedBy="#{xml_escape(pivot_table[:cache_refreshed_by])}") if pivot_table[:cache_refreshed_by]
168
- pcd_attrs << %( refreshedVersion="#{pivot_table[:cache_refreshed_version]}") if pivot_table[:cache_refreshed_version]
169
- pcd_attrs << %( createdVersion="#{pivot_table[:cache_created_version]}") if pivot_table[:cache_created_version]
170
- pcd_attrs << %( recordCount="#{pivot_table[:cache_record_count]}") if pivot_table[:cache_record_count]
168
+ pcd_attrs << %( refreshedVersion="#{xml_escape(pivot_table[:cache_refreshed_version])}") if pivot_table[:cache_refreshed_version]
169
+ pcd_attrs << %( createdVersion="#{xml_escape(pivot_table[:cache_created_version])}") if pivot_table[:cache_created_version]
170
+ pcd_attrs << %( recordCount="#{xml_escape(pivot_table[:cache_record_count])}") if pivot_table[:cache_record_count]
171
171
  pcd_attrs << ' optimizeMemory="1"' if pivot_table[:cache_optimize_memory]
172
172
  parts = [
173
173
  XML_HEADER,
@@ -182,13 +182,13 @@ module Xlsxrb
182
182
  elsif source.include?("!")
183
183
  sname, srange = source.split("!", 2)
184
184
  sname = sname.delete("'")
185
- parts << %(<cacheSource type="worksheet"><worksheetSource ref="#{srange}" sheet="#{xml_escape(sname)}"#{ws_name_attr}/></cacheSource>)
185
+ parts << %(<cacheSource type="worksheet"><worksheetSource ref="#{xml_escape(srange)}" sheet="#{xml_escape(sname)}"#{ws_name_attr}/></cacheSource>)
186
186
  else
187
- parts << %(<cacheSource type="worksheet"><worksheetSource ref="#{source}"#{ws_name_attr}/></cacheSource>)
187
+ parts << %(<cacheSource type="worksheet"><worksheetSource ref="#{xml_escape(source)}"#{ws_name_attr}/></cacheSource>)
188
188
  end
189
189
 
190
190
  field_count = pivot_table[:field_names] ? pivot_table[:field_names].size : (pivot_table[:row_fields].size + pivot_table[:col_fields].size + pivot_table[:data_fields].size)
191
- parts << %(<cacheFields count="#{field_count}">)
191
+ parts << %(<cacheFields count="#{xml_escape(field_count)}">)
192
192
  field_count.times do |fi|
193
193
  fname = if pivot_table[:field_names] && pivot_table[:field_names][fi]
194
194
  pivot_table[:field_names][fi]
@@ -198,7 +198,7 @@ module Xlsxrb
198
198
  end
199
199
  fa = pivot_table[:field_attrs] && pivot_table[:field_attrs][fi]
200
200
  cf_num_fmt = (fa && fa[:cache_num_fmt_id]) || 0
201
- cf_attrs = %( name="#{xml_escape(fname)}" numFmtId="#{cf_num_fmt}")
201
+ cf_attrs = %( name="#{xml_escape(fname)}" numFmtId="#{xml_escape(cf_num_fmt)}")
202
202
  cf_attrs << %( caption="#{xml_escape(fa[:cache_caption])}") if fa && fa[:cache_caption]
203
203
  cf_attrs << %( formula="#{xml_escape(fa[:cache_formula])}") if fa && fa[:cache_formula]
204
204
  field_items = pivot_table[:items] && pivot_table[:items][fi]
@@ -229,7 +229,7 @@ module Xlsxrb
229
229
  unless numbers.empty?
230
230
  attrs << ' containsNumber="1"'
231
231
  attrs << ' containsInteger="1"' if numbers.all? { |n| n.is_a?(Integer) || n == n.to_i }
232
- attrs << %( minValue="#{numbers.min}" maxValue="#{numbers.max}")
232
+ attrs << %( minValue="#{xml_escape(numbers.min)}" maxValue="#{xml_escape(numbers.max)}")
233
233
  end
234
234
  attrs
235
235
  end
@@ -239,7 +239,7 @@ module Xlsxrb
239
239
  case value
240
240
  when nil then "<m/>"
241
241
  when true, false then %(<b v="#{value ? 1 : 0}"/>)
242
- when Numeric then %(<n v="#{value}"/>)
242
+ when Numeric then %(<n v="#{xml_escape(value)}"/>)
243
243
  else %(<s v="#{xml_escape(value.to_s)}"/>)
244
244
  end
245
245
  end
@@ -249,14 +249,14 @@ module Xlsxrb
249
249
  items = pivot_table[:items]
250
250
  if items&.values&.any? { |v| v && !v.empty? }
251
251
  max_len = items.values.map { |v| v ? v.size : 0 }.max
252
- parts = [XML_HEADER, %(<pivotCacheRecords xmlns="#{SSML_NS}" count="#{max_len}">)]
252
+ parts = [XML_HEADER, %(<pivotCacheRecords xmlns="#{SSML_NS}" count="#{xml_escape(max_len)}">)]
253
253
  max_len.times do |ri|
254
254
  parts << "<r>"
255
255
  field_count = pivot_table[:field_names] ? pivot_table[:field_names].size : (pivot_table[:row_fields].size + pivot_table[:col_fields].size + pivot_table[:data_fields].size)
256
256
  field_count.times do |fi|
257
257
  field_items = items[fi]
258
258
  parts << if field_items
259
- %(<x v="#{ri < field_items.size ? ri : 0}"/>)
259
+ %(<x v="#{xml_escape(ri < field_items.size ? ri : 0)}"/>)
260
260
  else
261
261
  %(<n v="0"/>)
262
262
  end
@@ -275,7 +275,7 @@ module Xlsxrb
275
275
  [
276
276
  XML_HEADER,
277
277
  %(<Relationships xmlns="#{REL_NS}">),
278
- %(<Relationship Id="rId1" Type="#{DOC_REL_NS}/pivotCacheRecords" Target="pivotCacheRecords#{cache_id}.xml"/>),
278
+ %(<Relationship Id="rId1" Type="#{DOC_REL_NS}/pivotCacheRecords" Target="pivotCacheRecords#{xml_escape(cache_id)}.xml"/>),
279
279
  "</Relationships>"
280
280
  ].join
281
281
  end
@@ -285,7 +285,7 @@ module Xlsxrb
285
285
  [
286
286
  XML_HEADER,
287
287
  %(<Relationships xmlns="#{REL_NS}">),
288
- %(<Relationship Id="rId1" Type="#{DOC_REL_NS}/pivotCacheDefinition" Target="../pivotCache/pivotCacheDefinition#{cache_id}.xml"/>),
288
+ %(<Relationship Id="rId1" Type="#{DOC_REL_NS}/pivotCacheDefinition" Target="../pivotCache/pivotCacheDefinition#{xml_escape(cache_id)}.xml"/>),
289
289
  "</Relationships>"
290
290
  ].join
291
291
  end
@@ -339,6 +339,13 @@ module Xlsxrb
339
339
  end
340
340
 
341
341
  # : (untyped value) -> untyped
342
+ # Escapes cell text: ST_Xstring (XmlBuilder.encode_xstring), then XML.
343
+ def xml_escape_text(value)
344
+ str = value.to_s
345
+ str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
346
+ xml_escape(XmlBuilder.encode_xstring(str))
347
+ end
348
+
342
349
  def xml_escape(value)
343
350
  value.to_s
344
351
  .gsub(/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/, "")
@@ -365,7 +372,7 @@ module Xlsxrb
365
372
  rpr << "<u/>"
366
373
  else
367
374
  mapped_u = StyleBuilder::UNDERLINE_MAP[u_val.to_sym] || u_val
368
- rpr << %(<u val="#{mapped_u}"/>)
375
+ rpr << %(<u val="#{xml_escape(mapped_u)}"/>)
369
376
  end
370
377
  end
371
378
  va_val = font[:vert_align] || font[:vertical_align]
@@ -373,42 +380,42 @@ module Xlsxrb
373
380
  va_val ||= "subscript" if font[:subscript]
374
381
  if va_val
375
382
  mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val
376
- rpr << %(<vertAlign val="#{mapped_va}"/>)
383
+ rpr << %(<vertAlign val="#{xml_escape(mapped_va)}"/>)
377
384
  end
378
385
  font_sz = font[:size] || font[:sz]
379
- rpr << %(<sz val="#{font_sz}"/>) if font_sz
386
+ rpr << %(<sz val="#{xml_escape(font_sz)}"/>) if font_sz
380
387
  rpr << emit_color_xml(font)
381
388
  rpr << %(<rFont val="#{xml_escape(font[:name])}"/>) if font[:name]
382
- rpr << %(<family val="#{font[:family]}"/>) if font[:family]
383
- rpr << %(<scheme val="#{font[:scheme]}"/>) if font[:scheme]
384
- "<r><rPr>#{rpr}</rPr><t>#{xml_escape(text)}</t></r>"
389
+ rpr << %(<family val="#{xml_escape(font[:family])}"/>) if font[:family]
390
+ rpr << %(<scheme val="#{xml_escape(font[:scheme])}"/>) if font[:scheme]
391
+ "<r><rPr>#{rpr}</rPr><t>#{xml_escape_text(text)}</t></r>"
385
392
  else
386
- "<r><t>#{xml_escape(text)}</t></r>"
393
+ "<r><t>#{xml_escape_text(text)}</t></r>"
387
394
  end
388
395
  end.join
389
396
  end
390
397
 
391
398
  # : (untyped cell_ref, untyped value, untyped style_idx, ?untyped? sst, ?ph: untyped?) -> untyped
392
399
  def cell_xml(cell_ref, value, style_idx, sst = nil, ph: nil) # rubocop:disable Naming/MethodParameterName
393
- s_attr = style_idx ? %( s="#{style_idx}") : ""
400
+ s_attr = style_idx ? %( s="#{xml_escape(style_idx)}") : ""
394
401
  ph_attr = ph ? ' ph="1"' : ""
395
402
  case value
396
403
  when Xlsxrb::Elements::CellError
397
- %(<c r="#{cell_ref}" t="e"#{s_attr}#{ph_attr}><v>#{xml_escape(value.code)}</v></c>)
404
+ %(<c r="#{xml_escape(cell_ref)}" t="e"#{s_attr}#{ph_attr}><v>#{xml_escape(value.code)}</v></c>)
398
405
  when Xlsxrb::Elements::Formula
399
406
  f_attrs = +""
400
407
  case value.type
401
408
  when :shared
402
- f_attrs << %( t="shared" si="#{value.shared_index}")
403
- f_attrs << %( ref="#{value.ref}") if value.ref
409
+ f_attrs << %( t="shared" si="#{xml_escape(value.shared_index)}")
410
+ f_attrs << %( ref="#{xml_escape(value.ref)}") if value.ref
404
411
  when :array
405
- f_attrs << %( t="array" ref="#{value.ref}") if value.ref
412
+ f_attrs << %( t="array" ref="#{xml_escape(value.ref)}") if value.ref
406
413
  when :data_table
407
414
  f_attrs << ' t="dataTable"'
408
415
  f_attrs << ' dt2D="1"' if value.dt2d
409
416
  f_attrs << ' dtr="1"' if value.dtr
410
- f_attrs << %( r1="#{value.r1}") if value.r1
411
- f_attrs << %( r2="#{value.r2}") if value.r2
417
+ f_attrs << %( r1="#{xml_escape(value.r1)}") if value.r1
418
+ f_attrs << %( r2="#{xml_escape(value.r2)}") if value.r2
412
419
  end
413
420
  f_attrs << ' ca="1"' if value.calculate_always?
414
421
  f_attrs << ' aca="1"' if value.aca
@@ -430,39 +437,39 @@ module Xlsxrb
430
437
  else
431
438
  "<f#{f_attrs}/>"
432
439
  end
433
- parts = %(<c r="#{cell_ref}"#{t_attr}#{s_attr}#{ph_attr}>#{f_tag})
434
- parts << "<v>#{xml_escape(cached_val_str)}</v>" unless value.cached_value.nil?
440
+ parts = %(<c r="#{xml_escape(cell_ref)}"#{t_attr}#{s_attr}#{ph_attr}>#{f_tag})
441
+ parts << "<v>#{xml_escape_text(cached_val_str)}</v>" unless value.cached_value.nil?
435
442
  parts << "</c>"
436
443
  parts
437
444
  when Xlsxrb::Elements::RichText
438
445
  if sst
439
446
  rt_sst, = sst
440
447
  idx = rt_sst[value]
441
- %(<c r="#{cell_ref}" t="s"#{s_attr}#{ph_attr}><v>#{idx}</v></c>)
448
+ %(<c r="#{xml_escape(cell_ref)}" t="s"#{s_attr}#{ph_attr}><v>#{idx}</v></c>)
442
449
  else
443
- %(<c r="#{cell_ref}" t="inlineStr"#{s_attr}#{ph_attr}><is>#{rich_text_xml(value)}</is></c>)
450
+ %(<c r="#{xml_escape(cell_ref)}" t="inlineStr"#{s_attr}#{ph_attr}><is>#{rich_text_xml(value)}</is></c>)
444
451
  end
445
452
  when true, false
446
- %(<c r="#{cell_ref}" t="b"#{s_attr}#{ph_attr}><v>#{value ? 1 : 0}</v></c>)
453
+ %(<c r="#{xml_escape(cell_ref)}" t="b"#{s_attr}#{ph_attr}><v>#{value ? 1 : 0}</v></c>)
447
454
  when Time
448
455
  date1904 = @workbook_properties&.[](:date1904) ? true : false
449
456
  serial = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
450
- dt_attr = s_attr.empty? && (dt_style = resolve_style_index(datetime_num_fmt_id)) ? %( s="#{dt_style}") : s_attr
451
- %(<c r="#{cell_ref}"#{dt_attr}#{ph_attr}><v>#{serial}</v></c>)
457
+ dt_attr = s_attr.empty? && (dt_style = resolve_style_index(datetime_num_fmt_id)) ? %( s="#{xml_escape(dt_style)}") : s_attr
458
+ %(<c r="#{xml_escape(cell_ref)}"#{dt_attr}#{ph_attr}><v>#{serial}</v></c>)
452
459
  when Date
453
460
  date1904 = @workbook_properties&.[](:date1904) ? true : false
454
461
  serial = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
455
- ds_attr = s_attr.empty? && (date_style = resolve_style_index(date_num_fmt_id)) ? %( s="#{date_style}") : s_attr
456
- %(<c r="#{cell_ref}"#{ds_attr}#{ph_attr}><v>#{serial}</v></c>)
462
+ ds_attr = s_attr.empty? && (date_style = resolve_style_index(date_num_fmt_id)) ? %( s="#{xml_escape(date_style)}") : s_attr
463
+ %(<c r="#{xml_escape(cell_ref)}"#{ds_attr}#{ph_attr}><v>#{serial}</v></c>)
457
464
  when Numeric
458
- %(<c r="#{cell_ref}"#{s_attr}#{ph_attr}><v>#{value}</v></c>)
465
+ %(<c r="#{xml_escape(cell_ref)}"#{s_attr}#{ph_attr}><v>#{value}</v></c>)
459
466
  else
460
467
  if sst
461
468
  _, str_sst = sst
462
469
  idx = str_sst[value.to_s]
463
- %(<c r="#{cell_ref}" t="s"#{s_attr}#{ph_attr}><v>#{idx}</v></c>)
470
+ %(<c r="#{xml_escape(cell_ref)}" t="s"#{s_attr}#{ph_attr}><v>#{idx}</v></c>)
464
471
  else
465
- %(<c r="#{cell_ref}" t="inlineStr"#{s_attr}#{ph_attr}><is><t>#{xml_escape(value)}</t></is></c>)
472
+ %(<c r="#{xml_escape(cell_ref)}" t="inlineStr"#{s_attr}#{ph_attr}><is><t>#{xml_escape_text(value)}</t></is></c>)
466
473
  end
467
474
  end
468
475
  end
@@ -510,20 +517,20 @@ module Xlsxrb
510
517
  case filter[:type]
511
518
  when :filters
512
519
  attrs = filter[:blank] ? +' blank="1"' : +""
513
- attrs << %( calendarType="#{filter[:calendar_type]}") if filter[:calendar_type]
520
+ attrs << %( calendarType="#{xml_escape(filter[:calendar_type])}") if filter[:calendar_type]
514
521
  has_values = filter[:values]&.any?
515
522
  has_date_groups = filter[:date_group_items]&.any?
516
523
  if has_values || has_date_groups
517
524
  parts = ["<filters#{attrs}>"]
518
525
  filter[:values]&.each { |v| parts << %(<filter val="#{xml_escape(v)}"/>) }
519
526
  filter[:date_group_items]&.each do |dg|
520
- dg_attrs = %(dateTimeGrouping="#{dg[:date_time_grouping]}")
521
- dg_attrs << %( year="#{dg[:year]}") if dg[:year]
522
- dg_attrs << %( month="#{dg[:month]}") if dg[:month]
523
- dg_attrs << %( day="#{dg[:day]}") if dg[:day]
524
- dg_attrs << %( hour="#{dg[:hour]}") if dg[:hour]
525
- dg_attrs << %( minute="#{dg[:minute]}") if dg[:minute]
526
- dg_attrs << %( second="#{dg[:second]}") if dg[:second]
527
+ dg_attrs = %(dateTimeGrouping="#{xml_escape(dg[:date_time_grouping])}")
528
+ dg_attrs << %( year="#{xml_escape(dg[:year])}") if dg[:year]
529
+ dg_attrs << %( month="#{xml_escape(dg[:month])}") if dg[:month]
530
+ dg_attrs << %( day="#{xml_escape(dg[:day])}") if dg[:day]
531
+ dg_attrs << %( hour="#{xml_escape(dg[:hour])}") if dg[:hour]
532
+ dg_attrs << %( minute="#{xml_escape(dg[:minute])}") if dg[:minute]
533
+ dg_attrs << %( second="#{xml_escape(dg[:second])}") if dg[:second]
527
534
  parts << "<dateGroupItem #{dg_attrs}/>"
528
535
  end
529
536
  parts << "</filters>"
@@ -536,32 +543,32 @@ module Xlsxrb
536
543
  and_attr = filter[:and] ? ' and="1"' : ""
537
544
  parts = ["<customFilters#{and_attr}>"]
538
545
  filter[:filters].each do |cf|
539
- parts << %(<customFilter operator="#{cf[:operator]}" val="#{xml_escape(cf[:val])}"/>)
546
+ parts << %(<customFilter operator="#{xml_escape(cf[:operator])}" val="#{xml_escape(cf[:val])}"/>)
540
547
  end
541
548
  parts << "</customFilters>"
542
549
  parts.join
543
550
  else
544
- %(<customFilters><customFilter operator="#{filter[:operator]}" val="#{xml_escape(filter[:val])}"/></customFilters>)
551
+ %(<customFilters><customFilter operator="#{xml_escape(filter[:operator])}" val="#{xml_escape(filter[:val])}"/></customFilters>)
545
552
  end
546
553
  when :dynamic
547
- dyn_attrs = %( type="#{filter[:dynamic_type]}")
548
- dyn_attrs << %( val="#{filter[:val]}") if filter[:val]
549
- dyn_attrs << %( valIso="#{filter[:val_iso]}") if filter[:val_iso]
550
- dyn_attrs << %( maxVal="#{filter[:max_val]}") if filter[:max_val]
551
- dyn_attrs << %( maxValIso="#{filter[:max_val_iso]}") if filter[:max_val_iso]
554
+ dyn_attrs = %( type="#{xml_escape(filter[:dynamic_type])}")
555
+ dyn_attrs << %( val="#{xml_escape(filter[:val])}") if filter[:val]
556
+ dyn_attrs << %( valIso="#{xml_escape(filter[:val_iso])}") if filter[:val_iso]
557
+ dyn_attrs << %( maxVal="#{xml_escape(filter[:max_val])}") if filter[:max_val]
558
+ dyn_attrs << %( maxValIso="#{xml_escape(filter[:max_val_iso])}") if filter[:max_val_iso]
552
559
  "<dynamicFilter#{dyn_attrs}/>"
553
560
  when :top10
554
561
  top_attr = filter[:top] ? ' top="1"' : ""
555
562
  pct_attr = filter[:percent] ? ' percent="1"' : ""
556
- fv_attr = filter[:filter_val] ? %( filterVal="#{filter[:filter_val]}") : ""
557
- %(<top10#{top_attr}#{pct_attr} val="#{filter[:val]}"#{fv_attr}/>)
563
+ fv_attr = filter[:filter_val] ? %( filterVal="#{xml_escape(filter[:filter_val])}") : ""
564
+ %(<top10#{top_attr}#{pct_attr} val="#{xml_escape(filter[:val])}"#{fv_attr}/>)
558
565
  when :color_filter
559
- cf_attrs = %(dxfId="#{filter[:dxf_id]}")
566
+ cf_attrs = %(dxfId="#{xml_escape(filter[:dxf_id])}")
560
567
  cf_attrs << ' cellColor="0"' if filter[:cell_color] == false
561
568
  %(<colorFilter #{cf_attrs}/>)
562
569
  when :icon_filter
563
- if_attrs = %(iconSet="#{filter[:icon_set]}")
564
- if_attrs << %( iconId="#{filter[:icon_id]}") if filter[:icon_id]
570
+ if_attrs = %(iconSet="#{xml_escape(filter[:icon_set])}")
571
+ if_attrs << %( iconId="#{xml_escape(filter[:icon_id])}") if filter[:icon_id]
565
572
  %(<iconFilter #{if_attrs}/>)
566
573
  else
567
574
  ""
@@ -579,24 +586,24 @@ module Xlsxrb
579
586
  type = snake if CF_TYPE_MAP.key?(snake)
580
587
  end
581
588
  rule_type = CF_TYPE_MAP[type] || type.to_s
582
- rule_attrs = %(type="#{rule_type}")
583
- rule_attrs << %( priority="#{rule[:priority]}") if rule[:priority]
584
- rule_attrs << %( operator="#{rule[:operator]}") if rule[:operator]
589
+ rule_attrs = %(type="#{xml_escape(rule_type)}")
590
+ rule_attrs << %( priority="#{xml_escape(rule[:priority])}") if rule[:priority]
591
+ rule_attrs << %( operator="#{xml_escape(rule[:operator])}") if rule[:operator]
585
592
  did = rule[:dxf_id] || rule[:format_id]
586
- rule_attrs << %( dxfId="#{did}") if did
593
+ rule_attrs << %( dxfId="#{xml_escape(did)}") if did
587
594
  rule_attrs << %( stopIfTrue="1") if rule[:stop_if_true]
588
595
  rule_attrs << %( aboveAverage="0") if rule[:above_average] == false
589
596
  rule_attrs << %( equalAverage="1") if rule[:equal_average]
590
- rule_attrs << %( rank="#{rule[:rank]}") if rule[:rank]
597
+ rule_attrs << %( rank="#{xml_escape(rule[:rank])}") if rule[:rank]
591
598
  rule_attrs << %( percent="1") if rule[:percent]
592
599
  rule_attrs << %( bottom="1") if rule[:bottom]
593
600
  rule_attrs << %( text="#{xml_escape(rule[:text])}") if rule[:text]
594
601
  if rule[:time_period]
595
602
  tp = rule[:time_period]
596
603
  tp_str = DslHelpers::CF_TIME_PERIODS[tp.to_sym] || tp.to_s
597
- rule_attrs << %( timePeriod="#{tp_str}")
604
+ rule_attrs << %( timePeriod="#{xml_escape(tp_str)}")
598
605
  end
599
- rule_attrs << %( stdDev="#{rule[:std_dev]}") if rule[:std_dev]
606
+ rule_attrs << %( stdDev="#{xml_escape(rule[:std_dev])}") if rule[:std_dev]
600
607
 
601
608
  case type
602
609
  when :cell_is, :expression, :above_average, :top10, :duplicate_values, :unique_values,
@@ -615,8 +622,8 @@ module Xlsxrb
615
622
  parts << "<cfRule #{rule_attrs}>"
616
623
  parts << "<colorScale>"
617
624
  cs[:cfvo]&.each do |cfvo|
618
- cfvo_attrs = %(type="#{cfvo[:type]}")
619
- cfvo_attrs << %( val="#{cfvo[:val]}") if cfvo[:val]
625
+ cfvo_attrs = %(type="#{xml_escape(cfvo[:type])}")
626
+ cfvo_attrs << %( val="#{xml_escape(cfvo[:val])}") if cfvo[:val]
620
627
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
621
628
  parts << "<cfvo #{cfvo_attrs}/>"
622
629
  end
@@ -627,13 +634,13 @@ module Xlsxrb
627
634
  db = rule[:data_bar]
628
635
  parts << "<cfRule #{rule_attrs}>"
629
636
  db_attrs = +""
630
- db_attrs << %( minLength="#{db[:min_length]}") if db[:min_length]
631
- db_attrs << %( maxLength="#{db[:max_length]}") if db[:max_length]
637
+ db_attrs << %( minLength="#{xml_escape(db[:min_length])}") if db[:min_length]
638
+ db_attrs << %( maxLength="#{xml_escape(db[:max_length])}") if db[:max_length]
632
639
  db_attrs << %( showValue="#{db[:show_value] ? 1 : 0}") unless db[:show_value].nil?
633
640
  parts << "<dataBar#{db_attrs}>"
634
641
  db[:cfvo]&.each do |cfvo|
635
- cfvo_attrs = %(type="#{cfvo[:type]}")
636
- cfvo_attrs << %( val="#{cfvo[:val]}") if cfvo[:val]
642
+ cfvo_attrs = %(type="#{xml_escape(cfvo[:type])}")
643
+ cfvo_attrs << %( val="#{xml_escape(cfvo[:val])}") if cfvo[:val]
637
644
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
638
645
  parts << "<cfvo #{cfvo_attrs}/>"
639
646
  end
@@ -644,14 +651,14 @@ module Xlsxrb
644
651
  is = rule[:icon_set]
645
652
  parts << "<cfRule #{rule_attrs}>"
646
653
  is_attrs = +""
647
- is_attrs << %( iconSet="#{is[:icon_set]}") if is[:icon_set]
654
+ is_attrs << %( iconSet="#{xml_escape(is[:icon_set])}") if is[:icon_set]
648
655
  is_attrs << %( reverse="#{is[:reverse] ? 1 : 0}") unless is[:reverse].nil?
649
656
  is_attrs << %( percent="#{is[:percent] ? 1 : 0}") unless is[:percent].nil?
650
657
  is_attrs << %( showValue="#{is[:show_value] ? 1 : 0}") unless is[:show_value].nil?
651
658
  parts << "<iconSet#{is_attrs}>"
652
659
  is[:cfvo]&.each do |cfvo|
653
- cfvo_attrs = %(type="#{cfvo[:type]}")
654
- cfvo_attrs << %( val="#{cfvo[:val]}") if cfvo[:val]
660
+ cfvo_attrs = %(type="#{xml_escape(cfvo[:type])}")
661
+ cfvo_attrs << %( val="#{xml_escape(cfvo[:val])}") if cfvo[:val]
655
662
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
656
663
  parts << "<cfvo #{cfvo_attrs}/>"
657
664
  end
@@ -668,7 +675,7 @@ module Xlsxrb
668
675
  if color.is_a?(Hash)
669
676
  emit_color_xml(color)
670
677
  else
671
- %(<color rgb="#{color}"/>)
678
+ %(<color rgb="#{xml_escape(color)}"/>)
672
679
  end
673
680
  end
674
681
 
@@ -740,7 +747,7 @@ module Xlsxrb
740
747
  hp = @app_properties[:heading_pairs]
741
748
  tp = @app_properties[:titles_of_parts]
742
749
  parts << "<HeadingPairs>"
743
- parts << %(<vt:vector size="#{hp.size * 2}" baseType="variant">)
750
+ parts << %(<vt:vector size="#{xml_escape(hp.size * 2)}" baseType="variant">)
744
751
  hp.each do |label, count|
745
752
  parts << "<vt:variant><vt:lpstr>#{xml_escape(label)}</vt:lpstr></vt:variant>"
746
753
  parts << "<vt:variant><vt:i4>#{count}</vt:i4></vt:variant>"
@@ -762,7 +769,7 @@ module Xlsxrb
762
769
  custom_ns = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
763
770
  parts = [
764
771
  XML_HEADER,
765
- %(<Properties xmlns="#{custom_ns}" xmlns:vt="#{VT_NS}">)
772
+ %(<Properties xmlns="#{xml_escape(custom_ns)}" xmlns:vt="#{VT_NS}">)
766
773
  ]
767
774
  list = if @custom_properties.is_a?(Hash)
768
775
  @custom_properties.map do |name, val|
@@ -786,16 +793,16 @@ module Xlsxrb
786
793
  list.each_with_index do |prop, idx|
787
794
  fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
788
795
  pid = idx + 2 # pids start at 2
789
- parts << %(<property fmtid="#{fmtid}" pid="#{pid}" name="#{xml_escape(prop[:name])}">)
796
+ parts << %(<property fmtid="#{xml_escape(fmtid)}" pid="#{xml_escape(pid)}" name="#{xml_escape(prop[:name])}">)
790
797
  parts << case prop[:type]
791
798
  when :number, :int, :i4
792
- "<vt:i4>#{prop[:value]}</vt:i4>"
799
+ "<vt:i4>#{xml_escape(prop[:value])}</vt:i4>"
793
800
  when :float, :r8
794
- "<vt:r8>#{prop[:value]}</vt:r8>"
801
+ "<vt:r8>#{xml_escape(prop[:value])}</vt:r8>"
795
802
  when :bool, :boolean
796
803
  "<vt:bool>#{prop[:value] ? "true" : "false"}</vt:bool>"
797
804
  when :date, :filetime
798
- "<vt:filetime>#{prop[:value]}</vt:filetime>"
805
+ "<vt:filetime>#{xml_escape(prop[:value])}</vt:filetime>"
799
806
  else
800
807
  "<vt:lpwstr>#{xml_escape(prop[:value].to_s)}</vt:lpwstr>"
801
808
  end