xlsxrb 0.2.1 → 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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -0
  3. data/README.md +24 -2
  4. data/lib/xlsxrb/dsl_helpers.rb +16 -0
  5. data/lib/xlsxrb/elements/styles.rb +15 -6
  6. data/lib/xlsxrb/number_formatter.rb +26 -8
  7. data/lib/xlsxrb/ooxml/cfb.rb +11 -3
  8. data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
  9. data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
  10. data/lib/xlsxrb/ooxml/crypto.rb +3 -0
  11. data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
  12. data/lib/xlsxrb/ooxml/workbook_writer.rb +30 -19
  13. data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
  14. data/lib/xlsxrb/ooxml/worksheet_writer.rb +43 -38
  15. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
  16. data/lib/xlsxrb/ooxml/writer/features_xml.rb +99 -99
  17. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
  18. data/lib/xlsxrb/ooxml/writer.rb +166 -159
  19. data/lib/xlsxrb/ooxml/xml_builder.rb +17 -7
  20. data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
  21. data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
  22. data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
  23. data/lib/xlsxrb/stream_sheet.rb +26 -11
  24. data/lib/xlsxrb/stream_writer.rb +9 -6
  25. data/lib/xlsxrb/version.rb +1 -1
  26. data/lib/xlsxrb/worksheet_builder.rb +6 -5
  27. data/lib/xlsxrb.rb +68 -21
  28. data/sig/generated/xlsxrb/dsl_helpers.rbs +9 -0
  29. data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
  30. data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
  31. data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
  32. data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
  33. data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
  34. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -0
  35. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
  36. data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
  37. data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +7 -2
  38. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
  39. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
  40. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
  41. data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
  42. data/sig/generated/xlsxrb/stream_writer.rbs +6 -2
  43. data/sig/generated/xlsxrb/worksheet_builder.rbs +3 -1
  44. data/sig/generated/xlsxrb.rbs +38 -8
  45. 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
@@ -372,7 +372,7 @@ module Xlsxrb
372
372
  rpr << "<u/>"
373
373
  else
374
374
  mapped_u = StyleBuilder::UNDERLINE_MAP[u_val.to_sym] || u_val
375
- rpr << %(<u val="#{mapped_u}"/>)
375
+ rpr << %(<u val="#{xml_escape(mapped_u)}"/>)
376
376
  end
377
377
  end
378
378
  va_val = font[:vert_align] || font[:vertical_align]
@@ -380,14 +380,14 @@ module Xlsxrb
380
380
  va_val ||= "subscript" if font[:subscript]
381
381
  if va_val
382
382
  mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val
383
- rpr << %(<vertAlign val="#{mapped_va}"/>)
383
+ rpr << %(<vertAlign val="#{xml_escape(mapped_va)}"/>)
384
384
  end
385
385
  font_sz = font[:size] || font[:sz]
386
- rpr << %(<sz val="#{font_sz}"/>) if font_sz
386
+ rpr << %(<sz val="#{xml_escape(font_sz)}"/>) if font_sz
387
387
  rpr << emit_color_xml(font)
388
388
  rpr << %(<rFont val="#{xml_escape(font[:name])}"/>) if font[:name]
389
- rpr << %(<family val="#{font[:family]}"/>) if font[:family]
390
- rpr << %(<scheme val="#{font[:scheme]}"/>) if font[:scheme]
389
+ rpr << %(<family val="#{xml_escape(font[:family])}"/>) if font[:family]
390
+ rpr << %(<scheme val="#{xml_escape(font[:scheme])}"/>) if font[:scheme]
391
391
  "<r><rPr>#{rpr}</rPr><t>#{xml_escape_text(text)}</t></r>"
392
392
  else
393
393
  "<r><t>#{xml_escape_text(text)}</t></r>"
@@ -397,25 +397,25 @@ module Xlsxrb
397
397
 
398
398
  # : (untyped cell_ref, untyped value, untyped style_idx, ?untyped? sst, ?ph: untyped?) -> untyped
399
399
  def cell_xml(cell_ref, value, style_idx, sst = nil, ph: nil) # rubocop:disable Naming/MethodParameterName
400
- s_attr = style_idx ? %( s="#{style_idx}") : ""
400
+ s_attr = style_idx ? %( s="#{xml_escape(style_idx)}") : ""
401
401
  ph_attr = ph ? ' ph="1"' : ""
402
402
  case value
403
403
  when Xlsxrb::Elements::CellError
404
- %(<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>)
405
405
  when Xlsxrb::Elements::Formula
406
406
  f_attrs = +""
407
407
  case value.type
408
408
  when :shared
409
- f_attrs << %( t="shared" si="#{value.shared_index}")
410
- 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
411
411
  when :array
412
- f_attrs << %( t="array" ref="#{value.ref}") if value.ref
412
+ f_attrs << %( t="array" ref="#{xml_escape(value.ref)}") if value.ref
413
413
  when :data_table
414
414
  f_attrs << ' t="dataTable"'
415
415
  f_attrs << ' dt2D="1"' if value.dt2d
416
416
  f_attrs << ' dtr="1"' if value.dtr
417
- f_attrs << %( r1="#{value.r1}") if value.r1
418
- 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
419
419
  end
420
420
  f_attrs << ' ca="1"' if value.calculate_always?
421
421
  f_attrs << ' aca="1"' if value.aca
@@ -437,7 +437,7 @@ module Xlsxrb
437
437
  else
438
438
  "<f#{f_attrs}/>"
439
439
  end
440
- parts = %(<c r="#{cell_ref}"#{t_attr}#{s_attr}#{ph_attr}>#{f_tag})
440
+ parts = %(<c r="#{xml_escape(cell_ref)}"#{t_attr}#{s_attr}#{ph_attr}>#{f_tag})
441
441
  parts << "<v>#{xml_escape_text(cached_val_str)}</v>" unless value.cached_value.nil?
442
442
  parts << "</c>"
443
443
  parts
@@ -445,31 +445,31 @@ module Xlsxrb
445
445
  if sst
446
446
  rt_sst, = sst
447
447
  idx = rt_sst[value]
448
- %(<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>)
449
449
  else
450
- %(<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>)
451
451
  end
452
452
  when true, false
453
- %(<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>)
454
454
  when Time
455
455
  date1904 = @workbook_properties&.[](:date1904) ? true : false
456
456
  serial = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
457
- dt_attr = s_attr.empty? && (dt_style = resolve_style_index(datetime_num_fmt_id)) ? %( s="#{dt_style}") : s_attr
458
- %(<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>)
459
459
  when Date
460
460
  date1904 = @workbook_properties&.[](:date1904) ? true : false
461
461
  serial = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
462
- ds_attr = s_attr.empty? && (date_style = resolve_style_index(date_num_fmt_id)) ? %( s="#{date_style}") : s_attr
463
- %(<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>)
464
464
  when Numeric
465
- %(<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>)
466
466
  else
467
467
  if sst
468
468
  _, str_sst = sst
469
469
  idx = str_sst[value.to_s]
470
- %(<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>)
471
471
  else
472
- %(<c r="#{cell_ref}" t="inlineStr"#{s_attr}#{ph_attr}><is><t>#{xml_escape_text(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>)
473
473
  end
474
474
  end
475
475
  end
@@ -517,20 +517,20 @@ module Xlsxrb
517
517
  case filter[:type]
518
518
  when :filters
519
519
  attrs = filter[:blank] ? +' blank="1"' : +""
520
- attrs << %( calendarType="#{filter[:calendar_type]}") if filter[:calendar_type]
520
+ attrs << %( calendarType="#{xml_escape(filter[:calendar_type])}") if filter[:calendar_type]
521
521
  has_values = filter[:values]&.any?
522
522
  has_date_groups = filter[:date_group_items]&.any?
523
523
  if has_values || has_date_groups
524
524
  parts = ["<filters#{attrs}>"]
525
525
  filter[:values]&.each { |v| parts << %(<filter val="#{xml_escape(v)}"/>) }
526
526
  filter[:date_group_items]&.each do |dg|
527
- dg_attrs = %(dateTimeGrouping="#{dg[:date_time_grouping]}")
528
- dg_attrs << %( year="#{dg[:year]}") if dg[:year]
529
- dg_attrs << %( month="#{dg[:month]}") if dg[:month]
530
- dg_attrs << %( day="#{dg[:day]}") if dg[:day]
531
- dg_attrs << %( hour="#{dg[:hour]}") if dg[:hour]
532
- dg_attrs << %( minute="#{dg[:minute]}") if dg[:minute]
533
- 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]
534
534
  parts << "<dateGroupItem #{dg_attrs}/>"
535
535
  end
536
536
  parts << "</filters>"
@@ -543,32 +543,32 @@ module Xlsxrb
543
543
  and_attr = filter[:and] ? ' and="1"' : ""
544
544
  parts = ["<customFilters#{and_attr}>"]
545
545
  filter[:filters].each do |cf|
546
- parts << %(<customFilter operator="#{cf[:operator]}" val="#{xml_escape(cf[:val])}"/>)
546
+ parts << %(<customFilter operator="#{xml_escape(cf[:operator])}" val="#{xml_escape(cf[:val])}"/>)
547
547
  end
548
548
  parts << "</customFilters>"
549
549
  parts.join
550
550
  else
551
- %(<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>)
552
552
  end
553
553
  when :dynamic
554
- dyn_attrs = %( type="#{filter[:dynamic_type]}")
555
- dyn_attrs << %( val="#{filter[:val]}") if filter[:val]
556
- dyn_attrs << %( valIso="#{filter[:val_iso]}") if filter[:val_iso]
557
- dyn_attrs << %( maxVal="#{filter[:max_val]}") if filter[:max_val]
558
- 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]
559
559
  "<dynamicFilter#{dyn_attrs}/>"
560
560
  when :top10
561
561
  top_attr = filter[:top] ? ' top="1"' : ""
562
562
  pct_attr = filter[:percent] ? ' percent="1"' : ""
563
- fv_attr = filter[:filter_val] ? %( filterVal="#{filter[:filter_val]}") : ""
564
- %(<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}/>)
565
565
  when :color_filter
566
- cf_attrs = %(dxfId="#{filter[:dxf_id]}")
566
+ cf_attrs = %(dxfId="#{xml_escape(filter[:dxf_id])}")
567
567
  cf_attrs << ' cellColor="0"' if filter[:cell_color] == false
568
568
  %(<colorFilter #{cf_attrs}/>)
569
569
  when :icon_filter
570
- if_attrs = %(iconSet="#{filter[:icon_set]}")
571
- 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]
572
572
  %(<iconFilter #{if_attrs}/>)
573
573
  else
574
574
  ""
@@ -586,24 +586,24 @@ module Xlsxrb
586
586
  type = snake if CF_TYPE_MAP.key?(snake)
587
587
  end
588
588
  rule_type = CF_TYPE_MAP[type] || type.to_s
589
- rule_attrs = %(type="#{rule_type}")
590
- rule_attrs << %( priority="#{rule[:priority]}") if rule[:priority]
591
- 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]
592
592
  did = rule[:dxf_id] || rule[:format_id]
593
- rule_attrs << %( dxfId="#{did}") if did
593
+ rule_attrs << %( dxfId="#{xml_escape(did)}") if did
594
594
  rule_attrs << %( stopIfTrue="1") if rule[:stop_if_true]
595
595
  rule_attrs << %( aboveAverage="0") if rule[:above_average] == false
596
596
  rule_attrs << %( equalAverage="1") if rule[:equal_average]
597
- rule_attrs << %( rank="#{rule[:rank]}") if rule[:rank]
597
+ rule_attrs << %( rank="#{xml_escape(rule[:rank])}") if rule[:rank]
598
598
  rule_attrs << %( percent="1") if rule[:percent]
599
599
  rule_attrs << %( bottom="1") if rule[:bottom]
600
600
  rule_attrs << %( text="#{xml_escape(rule[:text])}") if rule[:text]
601
601
  if rule[:time_period]
602
602
  tp = rule[:time_period]
603
603
  tp_str = DslHelpers::CF_TIME_PERIODS[tp.to_sym] || tp.to_s
604
- rule_attrs << %( timePeriod="#{tp_str}")
604
+ rule_attrs << %( timePeriod="#{xml_escape(tp_str)}")
605
605
  end
606
- rule_attrs << %( stdDev="#{rule[:std_dev]}") if rule[:std_dev]
606
+ rule_attrs << %( stdDev="#{xml_escape(rule[:std_dev])}") if rule[:std_dev]
607
607
 
608
608
  case type
609
609
  when :cell_is, :expression, :above_average, :top10, :duplicate_values, :unique_values,
@@ -622,8 +622,8 @@ module Xlsxrb
622
622
  parts << "<cfRule #{rule_attrs}>"
623
623
  parts << "<colorScale>"
624
624
  cs[:cfvo]&.each do |cfvo|
625
- cfvo_attrs = %(type="#{cfvo[:type]}")
626
- 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]
627
627
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
628
628
  parts << "<cfvo #{cfvo_attrs}/>"
629
629
  end
@@ -634,13 +634,13 @@ module Xlsxrb
634
634
  db = rule[:data_bar]
635
635
  parts << "<cfRule #{rule_attrs}>"
636
636
  db_attrs = +""
637
- db_attrs << %( minLength="#{db[:min_length]}") if db[:min_length]
638
- 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]
639
639
  db_attrs << %( showValue="#{db[:show_value] ? 1 : 0}") unless db[:show_value].nil?
640
640
  parts << "<dataBar#{db_attrs}>"
641
641
  db[:cfvo]&.each do |cfvo|
642
- cfvo_attrs = %(type="#{cfvo[:type]}")
643
- 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]
644
644
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
645
645
  parts << "<cfvo #{cfvo_attrs}/>"
646
646
  end
@@ -651,14 +651,14 @@ module Xlsxrb
651
651
  is = rule[:icon_set]
652
652
  parts << "<cfRule #{rule_attrs}>"
653
653
  is_attrs = +""
654
- is_attrs << %( iconSet="#{is[:icon_set]}") if is[:icon_set]
654
+ is_attrs << %( iconSet="#{xml_escape(is[:icon_set])}") if is[:icon_set]
655
655
  is_attrs << %( reverse="#{is[:reverse] ? 1 : 0}") unless is[:reverse].nil?
656
656
  is_attrs << %( percent="#{is[:percent] ? 1 : 0}") unless is[:percent].nil?
657
657
  is_attrs << %( showValue="#{is[:show_value] ? 1 : 0}") unless is[:show_value].nil?
658
658
  parts << "<iconSet#{is_attrs}>"
659
659
  is[:cfvo]&.each do |cfvo|
660
- cfvo_attrs = %(type="#{cfvo[:type]}")
661
- 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]
662
662
  cfvo_attrs << ' gte="0"' if cfvo[:gte] == false
663
663
  parts << "<cfvo #{cfvo_attrs}/>"
664
664
  end
@@ -675,7 +675,7 @@ module Xlsxrb
675
675
  if color.is_a?(Hash)
676
676
  emit_color_xml(color)
677
677
  else
678
- %(<color rgb="#{color}"/>)
678
+ %(<color rgb="#{xml_escape(color)}"/>)
679
679
  end
680
680
  end
681
681
 
@@ -747,7 +747,7 @@ module Xlsxrb
747
747
  hp = @app_properties[:heading_pairs]
748
748
  tp = @app_properties[:titles_of_parts]
749
749
  parts << "<HeadingPairs>"
750
- parts << %(<vt:vector size="#{hp.size * 2}" baseType="variant">)
750
+ parts << %(<vt:vector size="#{xml_escape(hp.size * 2)}" baseType="variant">)
751
751
  hp.each do |label, count|
752
752
  parts << "<vt:variant><vt:lpstr>#{xml_escape(label)}</vt:lpstr></vt:variant>"
753
753
  parts << "<vt:variant><vt:i4>#{count}</vt:i4></vt:variant>"
@@ -769,7 +769,7 @@ module Xlsxrb
769
769
  custom_ns = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
770
770
  parts = [
771
771
  XML_HEADER,
772
- %(<Properties xmlns="#{custom_ns}" xmlns:vt="#{VT_NS}">)
772
+ %(<Properties xmlns="#{xml_escape(custom_ns)}" xmlns:vt="#{VT_NS}">)
773
773
  ]
774
774
  list = if @custom_properties.is_a?(Hash)
775
775
  @custom_properties.map do |name, val|
@@ -793,16 +793,16 @@ module Xlsxrb
793
793
  list.each_with_index do |prop, idx|
794
794
  fmtid = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
795
795
  pid = idx + 2 # pids start at 2
796
- 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])}">)
797
797
  parts << case prop[:type]
798
798
  when :number, :int, :i4
799
- "<vt:i4>#{prop[:value]}</vt:i4>"
799
+ "<vt:i4>#{xml_escape(prop[:value])}</vt:i4>"
800
800
  when :float, :r8
801
- "<vt:r8>#{prop[:value]}</vt:r8>"
801
+ "<vt:r8>#{xml_escape(prop[:value])}</vt:r8>"
802
802
  when :bool, :boolean
803
803
  "<vt:bool>#{prop[:value] ? "true" : "false"}</vt:bool>"
804
804
  when :date, :filetime
805
- "<vt:filetime>#{prop[:value]}</vt:filetime>"
805
+ "<vt:filetime>#{xml_escape(prop[:value])}</vt:filetime>"
806
806
  else
807
807
  "<vt:lpwstr>#{xml_escape(prop[:value].to_s)}</vt:lpwstr>"
808
808
  end