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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +88 -0
- data/README.md +25 -3
- data/lib/xlsxrb/dsl_helpers.rb +65 -1
- data/lib/xlsxrb/elements/styles.rb +15 -6
- data/lib/xlsxrb/elements/worksheet.rb +2 -2
- data/lib/xlsxrb/number_formatter.rb +26 -8
- data/lib/xlsxrb/ooxml/cfb.rb +11 -3
- data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
- data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
- data/lib/xlsxrb/ooxml/crypto.rb +3 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
- data/lib/xlsxrb/ooxml/utils.rb +13 -0
- data/lib/xlsxrb/ooxml/workbook_writer.rb +43 -21
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +338 -259
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +109 -102
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
- data/lib/xlsxrb/ooxml/writer.rb +167 -160
- data/lib/xlsxrb/ooxml/xml_builder.rb +35 -7
- data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
- data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
- data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
- data/lib/xlsxrb/stream_row.rb +2 -2
- data/lib/xlsxrb/stream_sheet.rb +26 -11
- data/lib/xlsxrb/stream_writer.rb +212 -86
- data/lib/xlsxrb/style_builder.rb +339 -0
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +239 -88
- data/lib/xlsxrb/worksheet_builder.rb +847 -139
- data/lib/xlsxrb.rb +87 -21
- data/sig/generated/xlsxrb/dsl_helpers.rbs +37 -0
- data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
- data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/utils.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +53 -0
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -2
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
- data/sig/generated/xlsxrb/stream_row.rbs +2 -2
- data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
- data/sig/generated/xlsxrb/stream_writer.rbs +46 -6
- data/sig/generated/xlsxrb/style_builder.rbs +261 -0
- data/sig/generated/xlsxrb/workbook_builder.rbs +42 -9
- data/sig/generated/xlsxrb/worksheet_builder.rbs +618 -4
- data/sig/generated/xlsxrb.rbs +53 -8
- metadata +9 -3
|
@@ -26,18 +26,64 @@ module Xlsxrb
|
|
|
26
26
|
|
|
27
27
|
INTEGER_STRINGS = (0..65_535).map { |n| -n.to_s }.freeze
|
|
28
28
|
|
|
29
|
+
# Matches a value that claims to be a Time through is_a? without being one, such as
|
|
30
|
+
# ActiveSupport::TimeWithZone, which `when Time` does not match. The writers try it
|
|
31
|
+
# after their Time and Date branches, so other values never reach it. Such a value
|
|
32
|
+
# is written as the Time that to_time gives.
|
|
33
|
+
# @api private
|
|
34
|
+
module TimeLike
|
|
35
|
+
#: (untyped value) -> bool
|
|
36
|
+
def self.===(value) = value.is_a?(Time)
|
|
37
|
+
|
|
38
|
+
# A Time with the value's year, month, day, hour, minute, second, and UTC offset,
|
|
39
|
+
# which keeps its wall clock.
|
|
40
|
+
#: (untyped value) -> Time
|
|
41
|
+
def self.to_time(value)
|
|
42
|
+
seconds = value.sec + (value.respond_to?(:subsec) ? value.subsec : 0)
|
|
43
|
+
Time.new(value.year, value.month, value.day, value.hour, value.min, seconds, value.utc_offset)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
29
47
|
def initialize(io, date1904: false)
|
|
30
48
|
@io = io
|
|
31
49
|
@date1904 = date1904 ? true : false
|
|
32
50
|
@builder = XmlBuilder.new(@io)
|
|
33
51
|
@row_buffer = String.new(capacity: 65_536)
|
|
52
|
+
@style_attrs = {} # style id => '" s="<id>' fragment, built once per id
|
|
34
53
|
@started = false
|
|
35
54
|
@finished = false
|
|
55
|
+
@column_style_ids = nil # column index => resolved style id, see column_style_ids
|
|
56
|
+
@column_style_ids_map = nil
|
|
57
|
+
@time_like_style_resolver = nil
|
|
58
|
+
@strict_excel_mode = false
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Makes the row writers reject text longer than a cell holds (32,767 characters),
|
|
62
|
+
# as the strict Excel mode of the caller asks.
|
|
63
|
+
# @api private
|
|
64
|
+
#: (bool strict) -> void
|
|
65
|
+
def strict_excel_mode=(strict)
|
|
66
|
+
@strict_excel_mode = strict ? true : false
|
|
36
67
|
end
|
|
37
68
|
|
|
69
|
+
# Sets what gives the style id for a Time-like value in a styled cell, so that a
|
|
70
|
+
# style without a number format can be given its date-time variant.
|
|
71
|
+
# @api private
|
|
72
|
+
#: (untyped resolver) -> void
|
|
73
|
+
attr_writer :time_like_style_resolver
|
|
74
|
+
|
|
38
75
|
def set_column_style(col_index, style_id)
|
|
39
76
|
@column_styles ||= {}
|
|
40
77
|
@column_styles[col_index] = style_id
|
|
78
|
+
@column_style_ids = nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Drops the resolved column style ids, for a caller that changed the style id a
|
|
82
|
+
# name in the style map stands for.
|
|
83
|
+
# @api private
|
|
84
|
+
#: () -> void
|
|
85
|
+
def forget_column_style_ids
|
|
86
|
+
@column_style_ids = nil
|
|
41
87
|
end
|
|
42
88
|
|
|
43
89
|
# Write the worksheet header. Call once before writing rows.
|
|
@@ -99,15 +145,17 @@ module Xlsxrb
|
|
|
99
145
|
buf << '<row r="' << row_num_str << '"'
|
|
100
146
|
buf << ' s="' << attrs[:style_index].to_s << '" customFormat="1"' if attrs[:style_index].is_a?(Integer)
|
|
101
147
|
if attrs[:height]
|
|
102
|
-
buf << ' ht="' << attrs[:height]
|
|
148
|
+
buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"'
|
|
103
149
|
elsif attrs[:custom_height]
|
|
104
150
|
buf << ' customHeight="1"'
|
|
105
151
|
end
|
|
106
152
|
buf << ' hidden="1"' if attrs[:hidden]
|
|
107
153
|
buf << ' collapsed="1"' if attrs[:collapsed]
|
|
108
|
-
buf << ' outlineLevel="' << attrs[:outline_level]
|
|
154
|
+
buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level]
|
|
109
155
|
buf << ">"
|
|
110
156
|
|
|
157
|
+
row_style = resolve_style_id(attrs[:style_index], nil)
|
|
158
|
+
style_attrs = @style_attrs
|
|
111
159
|
cells.each do |cell|
|
|
112
160
|
if cell.is_a?(Elements::Cell)
|
|
113
161
|
value = cell.value
|
|
@@ -121,10 +169,14 @@ module Xlsxrb
|
|
|
121
169
|
value = cell[:value]
|
|
122
170
|
cell_style_id = cell[:style_index]
|
|
123
171
|
col_idx = cell[:column_index]
|
|
124
|
-
col_ref = cell[:ref]
|
|
172
|
+
col_ref = if cell[:ref]
|
|
173
|
+
escape_xml(cell[:ref])
|
|
174
|
+
elsif col_idx
|
|
175
|
+
"#{column_letter(col_idx)}#{row_num_str}"
|
|
176
|
+
end
|
|
125
177
|
formula = cell[:formula]
|
|
126
178
|
formula_ca = cell[:formula_ca]
|
|
127
|
-
cell_type_val = cell[:type]
|
|
179
|
+
cell_type_val = cell[:type] && escape_xml(cell[:type])
|
|
128
180
|
else
|
|
129
181
|
value = cell
|
|
130
182
|
cell_style_id = nil
|
|
@@ -136,41 +188,45 @@ module Xlsxrb
|
|
|
136
188
|
end
|
|
137
189
|
|
|
138
190
|
cell_style_id = resolve_style_id(cell_style_id, nil)
|
|
139
|
-
|
|
140
|
-
col_style = resolve_style_id(col_style, nil)
|
|
141
|
-
row_style = resolve_style_id(attrs[:style_index], nil)
|
|
142
|
-
style_id = cell_style_id.nil? ? (row_style || col_style) : cell_style_id
|
|
191
|
+
style_id = cell_style_id.nil? ? (row_style || (@column_styles && col_idx && resolve_style_id(@column_styles[col_idx], nil))) : cell_style_id
|
|
143
192
|
style_id = nil unless style_id.is_a?(Integer)
|
|
144
193
|
|
|
145
194
|
raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" if value.is_a?(Float) && !value.finite?
|
|
146
195
|
|
|
147
|
-
# Fast path for common
|
|
148
|
-
if !formula && !
|
|
196
|
+
# Fast path for common cells, styled or not
|
|
197
|
+
if !formula && !cell_type_val
|
|
198
|
+
# '" s="<id>' to append after the cell reference, or "" for an unstyled cell
|
|
199
|
+
s_attr = style_id ? (style_attrs[style_id] ||= -%(" s="#{style_id})) : ""
|
|
149
200
|
case value
|
|
150
201
|
when Integer, Float
|
|
151
|
-
buf << '<c r="' << col_ref << '"><v>' << value.to_s << "</v></c>"
|
|
202
|
+
buf << '<c r="' << col_ref << s_attr << '"><v>' << value.to_s << "</v></c>"
|
|
152
203
|
next
|
|
153
204
|
when String
|
|
154
205
|
if sst_index && (idx = sst_index[value])
|
|
155
|
-
buf << '<c r="' << col_ref << '" t="s"><v>' << idx.to_s << "</v></c>"
|
|
206
|
+
buf << '<c r="' << col_ref << s_attr << '" t="s"><v>' << idx.to_s << "</v></c>"
|
|
156
207
|
next
|
|
157
208
|
end
|
|
158
209
|
when true
|
|
159
|
-
buf << '<c r="' << col_ref << '" t="b"><v>1</v></c>'
|
|
210
|
+
buf << '<c r="' << col_ref << s_attr << '" t="b"><v>1</v></c>'
|
|
160
211
|
next
|
|
161
212
|
when false
|
|
162
|
-
buf << '<c r="' << col_ref << '" t="b"><v>0</v></c>'
|
|
213
|
+
buf << '<c r="' << col_ref << s_attr << '" t="b"><v>0</v></c>'
|
|
163
214
|
next
|
|
164
215
|
when nil
|
|
165
|
-
|
|
216
|
+
# An empty cell keeps only its own style, not the row or column style.
|
|
217
|
+
if cell_style_id.is_a?(Integer)
|
|
218
|
+
buf << '<c r="' << col_ref << '" s="' << cell_style_id.to_s << '"/>'
|
|
219
|
+
else
|
|
220
|
+
buf << '<c r="' << col_ref << '"/>'
|
|
221
|
+
end
|
|
166
222
|
next
|
|
167
223
|
when DateTime
|
|
168
224
|
serial = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
|
|
169
|
-
buf << '<c r="' << col_ref << '"><v>' << serial.to_s << "</v></c>"
|
|
225
|
+
buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>"
|
|
170
226
|
next
|
|
171
227
|
when Date
|
|
172
228
|
serial = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
|
|
173
|
-
buf << '<c r="' << col_ref << '"><v>' << serial.to_s << "</v></c>"
|
|
229
|
+
buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>"
|
|
174
230
|
next
|
|
175
231
|
end
|
|
176
232
|
end
|
|
@@ -259,6 +315,8 @@ module Xlsxrb
|
|
|
259
315
|
xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
|
|
260
316
|
when Date
|
|
261
317
|
xml_val = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
|
|
318
|
+
when TimeLike # after Date, so Date values skip TimeLike.===
|
|
319
|
+
xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)
|
|
262
320
|
when BigDecimal
|
|
263
321
|
xml_val = value.to_s("F")
|
|
264
322
|
when Xlsxrb::Elements::CellError
|
|
@@ -275,7 +333,7 @@ module Xlsxrb
|
|
|
275
333
|
if xml_val.is_a?(Xlsxrb::Elements::RichText)
|
|
276
334
|
write_inline_rich_text(buf, xml_val)
|
|
277
335
|
else
|
|
278
|
-
buf << "><is><t>" <<
|
|
336
|
+
buf << "><is><t>" << escape_text(xml_val.to_s) << "</t></is></c>"
|
|
279
337
|
end
|
|
280
338
|
elsif formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
|
|
281
339
|
f_attrs = +""
|
|
@@ -283,11 +341,11 @@ module Xlsxrb
|
|
|
283
341
|
case formula_type.to_sym
|
|
284
342
|
when :array
|
|
285
343
|
f_attrs << ' t="array"'
|
|
286
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
344
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
287
345
|
when :shared
|
|
288
346
|
f_attrs << ' t="shared"'
|
|
289
|
-
f_attrs << %( si="#{formula_si}") if formula_si
|
|
290
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
347
|
+
f_attrs << %( si="#{escape_xml(formula_si)}") if formula_si
|
|
348
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
291
349
|
end
|
|
292
350
|
end
|
|
293
351
|
f_attrs << ' ca="1"' if formula_ca
|
|
@@ -301,7 +359,7 @@ module Xlsxrb
|
|
|
301
359
|
else
|
|
302
360
|
v_str = xml_val.to_s
|
|
303
361
|
v_str = v_str.dup.force_encoding(Encoding::UTF_8) if v_str.encoding == Encoding::BINARY
|
|
304
|
-
v_str =
|
|
362
|
+
v_str = escape_text(v_str) if type == "str" || xml_val.is_a?(String)
|
|
305
363
|
buf << "<v>" << v_str << "</v></c>"
|
|
306
364
|
end
|
|
307
365
|
else
|
|
@@ -348,7 +406,7 @@ module Xlsxrb
|
|
|
348
406
|
|
|
349
407
|
buf << '<c r="' << col_ref << row_num_str << '"><v>' << value.to_s << "</v></c>"
|
|
350
408
|
when String
|
|
351
|
-
|
|
409
|
+
DslHelpers.check_cell_text_length!(value) if @strict_excel_mode && value.bytesize > DslHelpers::MAX_CELL_TEXT_LENGTH
|
|
352
410
|
|
|
353
411
|
if sst_index
|
|
354
412
|
idx = (sst_index[value] ||= begin
|
|
@@ -358,7 +416,7 @@ module Xlsxrb
|
|
|
358
416
|
idx_str = idx < 65_536 ? INTEGER_STRINGS[idx] : idx.to_s
|
|
359
417
|
buf << '<c r="' << col_ref << row_num_str << '" t="s"><v>' << idx_str << "</v></c>"
|
|
360
418
|
else
|
|
361
|
-
buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' <<
|
|
419
|
+
buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_text(value) << "</t></is></c>"
|
|
362
420
|
end
|
|
363
421
|
when true
|
|
364
422
|
buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>1</v></c>'
|
|
@@ -366,18 +424,25 @@ module Xlsxrb
|
|
|
366
424
|
buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>'
|
|
367
425
|
when Time, DateTime
|
|
368
426
|
time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
|
|
369
|
-
buf << if time_style_id
|
|
427
|
+
buf << if time_style_id.is_a?(Integer)
|
|
370
428
|
"<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
|
|
371
429
|
else
|
|
372
430
|
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
|
|
373
431
|
end
|
|
374
432
|
when Date
|
|
375
433
|
date_style_id = style_map ? style_map["__xlsxrb_date"] : nil
|
|
376
|
-
buf << if date_style_id
|
|
434
|
+
buf << if date_style_id.is_a?(Integer)
|
|
377
435
|
"<c r=\"#{col_ref}#{row_num_str}\" s=\"#{date_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>"
|
|
378
436
|
else
|
|
379
437
|
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>"
|
|
380
438
|
end
|
|
439
|
+
when TimeLike # after Date, so Date values skip TimeLike.===
|
|
440
|
+
time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
|
|
441
|
+
buf << if time_style_id.is_a?(Integer)
|
|
442
|
+
"<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>"
|
|
443
|
+
else
|
|
444
|
+
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>"
|
|
445
|
+
end
|
|
381
446
|
when Xlsxrb::Elements::Formula
|
|
382
447
|
raise ArgumentError, "Non-finite Float #{value.cached_value} is not supported in XLSX" if value.cached_value.is_a?(Float) && !value.cached_value.finite?
|
|
383
448
|
|
|
@@ -388,11 +453,11 @@ module Xlsxrb
|
|
|
388
453
|
case value.type.to_sym
|
|
389
454
|
when :array
|
|
390
455
|
f_attrs << ' t="array"'
|
|
391
|
-
f_attrs << %( ref="#{value.ref}") if value.ref
|
|
456
|
+
f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref
|
|
392
457
|
when :shared
|
|
393
458
|
f_attrs << ' t="shared"'
|
|
394
|
-
f_attrs << %( si="#{value.shared_index}") if value.shared_index
|
|
395
|
-
f_attrs << %( ref="#{value.ref}") if value.ref
|
|
459
|
+
f_attrs << %( si="#{escape_xml(value.shared_index)}") if value.shared_index
|
|
460
|
+
f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref
|
|
396
461
|
end
|
|
397
462
|
end
|
|
398
463
|
f_attrs << ' ca="1"' if value.calculate_always?
|
|
@@ -428,12 +493,12 @@ module Xlsxrb
|
|
|
428
493
|
case f_type.to_sym
|
|
429
494
|
when :array
|
|
430
495
|
f_attrs << ' t="array"'
|
|
431
|
-
f_attrs << %( ref="#{value[:ref]}") if value[:ref]
|
|
496
|
+
f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref]
|
|
432
497
|
when :shared
|
|
433
498
|
f_attrs << ' t="shared"'
|
|
434
499
|
si = value[:si] || value[:shared_index]
|
|
435
|
-
f_attrs << %( si="#{si}") if si
|
|
436
|
-
f_attrs << %( ref="#{value[:ref]}") if value[:ref]
|
|
500
|
+
f_attrs << %( si="#{escape_xml(si)}") if si
|
|
501
|
+
f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref]
|
|
437
502
|
end
|
|
438
503
|
end
|
|
439
504
|
f_attrs << ' ca="1"' if value[:calculate_always]
|
|
@@ -482,7 +547,7 @@ module Xlsxrb
|
|
|
482
547
|
buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"'
|
|
483
548
|
write_inline_rich_text(buf, value)
|
|
484
549
|
else
|
|
485
|
-
buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"inlineStr\"><is><t>#{
|
|
550
|
+
buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"inlineStr\"><is><t>#{escape_text(value.to_s)}</t></is></c>"
|
|
486
551
|
end
|
|
487
552
|
end
|
|
488
553
|
end
|
|
@@ -505,13 +570,13 @@ module Xlsxrb
|
|
|
505
570
|
buf << ' s="' << row_style_id.to_s << '" customFormat="1"' if row_style_id.is_a?(Integer)
|
|
506
571
|
if attrs
|
|
507
572
|
if attrs[:height]
|
|
508
|
-
buf << ' ht="' << attrs[:height]
|
|
573
|
+
buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"'
|
|
509
574
|
elsif attrs[:custom_height]
|
|
510
575
|
buf << ' customHeight="1"'
|
|
511
576
|
end
|
|
512
577
|
buf << ' hidden="1"' if attrs[:hidden]
|
|
513
578
|
buf << ' collapsed="1"' if attrs[:collapsed]
|
|
514
|
-
buf << ' outlineLevel="' << attrs[:outline_level]
|
|
579
|
+
buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level]
|
|
515
580
|
end
|
|
516
581
|
buf << ">"
|
|
517
582
|
|
|
@@ -521,215 +586,7 @@ module Xlsxrb
|
|
|
521
586
|
max_len = [max_len, styles_len].max
|
|
522
587
|
end
|
|
523
588
|
|
|
524
|
-
|
|
525
|
-
while col_index < max_len
|
|
526
|
-
value = col_index < values.length ? values[col_index] : nil
|
|
527
|
-
cell_style_id = single_style_id
|
|
528
|
-
if is_styles_collection
|
|
529
|
-
style_name = if styles.is_a?(Array)
|
|
530
|
-
col_index < styles.length ? styles[col_index] : nil
|
|
531
|
-
else
|
|
532
|
-
styles[col_index]
|
|
533
|
-
end
|
|
534
|
-
cell_style_id = resolve_style_id(style_name, style_map)
|
|
535
|
-
end
|
|
536
|
-
|
|
537
|
-
if cell_style_id.nil?
|
|
538
|
-
col_style = @column_styles ? @column_styles[col_index] : nil
|
|
539
|
-
col_style_id = resolve_style_id(col_style, style_map)
|
|
540
|
-
style_id = row_style_id || col_style_id
|
|
541
|
-
else
|
|
542
|
-
style_id = cell_style_id
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
col_ref = COLUMN_LETTERS[col_index] || column_letter(col_index)
|
|
546
|
-
|
|
547
|
-
if value.nil?
|
|
548
|
-
buf << '<c r="' << col_ref << row_num_str << '" s="' << cell_style_id.to_s << '"/>' if cell_style_id
|
|
549
|
-
col_index += 1
|
|
550
|
-
next
|
|
551
|
-
end
|
|
552
|
-
|
|
553
|
-
raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" if value.is_a?(Float) && !value.finite?
|
|
554
|
-
|
|
555
|
-
# Fast path: unstyled numbers, booleans, and simple strings (majority of cells)
|
|
556
|
-
if style_id.nil? && !value.is_a?(Xlsxrb::Elements::Formula) && !value.is_a?(Hash)
|
|
557
|
-
case value
|
|
558
|
-
when Integer
|
|
559
|
-
val_str = INTEGER_STRINGS[value] || value.to_s
|
|
560
|
-
buf << '<c r="' << col_ref << row_num_str << '"><v>' << val_str << "</v></c>"
|
|
561
|
-
col_index += 1
|
|
562
|
-
next
|
|
563
|
-
when Float
|
|
564
|
-
buf << '<c r="' << col_ref << row_num_str << '"><v>' << value.to_s << "</v></c>"
|
|
565
|
-
col_index += 1
|
|
566
|
-
next
|
|
567
|
-
when String
|
|
568
|
-
if sst_index
|
|
569
|
-
idx = sst_index[value]
|
|
570
|
-
unless idx
|
|
571
|
-
sst << value
|
|
572
|
-
idx = sst.size - 1
|
|
573
|
-
sst_index[value] = idx
|
|
574
|
-
end
|
|
575
|
-
idx_str = INTEGER_STRINGS[idx] || idx.to_s
|
|
576
|
-
buf << '<c r="' << col_ref << row_num_str << '" t="s"><v>' << idx_str << "</v></c>"
|
|
577
|
-
else
|
|
578
|
-
buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_xml(value) << "</t></is></c>"
|
|
579
|
-
end
|
|
580
|
-
col_index += 1
|
|
581
|
-
next
|
|
582
|
-
when true
|
|
583
|
-
buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>1</v></c>'
|
|
584
|
-
col_index += 1
|
|
585
|
-
next
|
|
586
|
-
when false
|
|
587
|
-
buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>'
|
|
588
|
-
col_index += 1
|
|
589
|
-
next
|
|
590
|
-
when Time, DateTime
|
|
591
|
-
buf << '<c r="' << col_ref << row_num_str << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904).to_s << "</v></c>"
|
|
592
|
-
col_index += 1
|
|
593
|
-
next
|
|
594
|
-
when Date
|
|
595
|
-
buf << '<c r="' << col_ref << row_num_str << '"><v>' << Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904).to_s << "</v></c>"
|
|
596
|
-
col_index += 1
|
|
597
|
-
next
|
|
598
|
-
end
|
|
599
|
-
end
|
|
600
|
-
|
|
601
|
-
# General path: styled, formula, rich text, or complex cell
|
|
602
|
-
formula_expr = nil
|
|
603
|
-
formula_ca = false
|
|
604
|
-
formula_type = nil
|
|
605
|
-
formula_ref = nil
|
|
606
|
-
formula_si = nil
|
|
607
|
-
xml_val = value
|
|
608
|
-
type = nil
|
|
609
|
-
|
|
610
|
-
case value
|
|
611
|
-
when Xlsxrb::Elements::Formula
|
|
612
|
-
formula_expr = value.expression
|
|
613
|
-
formula_ca = value.calculate_always?
|
|
614
|
-
formula_type = value.type
|
|
615
|
-
formula_ref = value.ref
|
|
616
|
-
formula_si = value.shared_index
|
|
617
|
-
xml_val = value.cached_value
|
|
618
|
-
case value.cached_value
|
|
619
|
-
when String
|
|
620
|
-
type = "str"
|
|
621
|
-
when true
|
|
622
|
-
type = "b"
|
|
623
|
-
xml_val = "1"
|
|
624
|
-
when false
|
|
625
|
-
type = "b"
|
|
626
|
-
xml_val = "0"
|
|
627
|
-
when Xlsxrb::Elements::CellError
|
|
628
|
-
type = "e"
|
|
629
|
-
xml_val = value.cached_value.code
|
|
630
|
-
end
|
|
631
|
-
when Hash
|
|
632
|
-
if value.key?(:formula)
|
|
633
|
-
formula_expr = value[:formula] || value[:expression]
|
|
634
|
-
formula_ca = value[:calculate_always] || false
|
|
635
|
-
formula_type = value[:type]
|
|
636
|
-
formula_ref = value[:ref]
|
|
637
|
-
formula_si = value[:si] || value[:shared_index]
|
|
638
|
-
xml_val = if value.key?(:value)
|
|
639
|
-
value[:value]
|
|
640
|
-
elsif value.key?(:cached_value)
|
|
641
|
-
value[:cached_value]
|
|
642
|
-
end
|
|
643
|
-
case xml_val
|
|
644
|
-
when String
|
|
645
|
-
type = "str"
|
|
646
|
-
when true
|
|
647
|
-
type = "b"
|
|
648
|
-
xml_val = "1"
|
|
649
|
-
when false
|
|
650
|
-
type = "b"
|
|
651
|
-
xml_val = "0"
|
|
652
|
-
when Xlsxrb::Elements::CellError
|
|
653
|
-
type = "e"
|
|
654
|
-
xml_val = xml_val.code
|
|
655
|
-
end
|
|
656
|
-
end
|
|
657
|
-
when String, Xlsxrb::Elements::RichText
|
|
658
|
-
if sst_index
|
|
659
|
-
idx = sst_index[value]
|
|
660
|
-
unless idx
|
|
661
|
-
sst << value
|
|
662
|
-
idx = sst.size - 1
|
|
663
|
-
sst_index[value] = idx
|
|
664
|
-
end
|
|
665
|
-
xml_val = idx
|
|
666
|
-
type = "s"
|
|
667
|
-
else
|
|
668
|
-
xml_val = value
|
|
669
|
-
type = "inlineStr"
|
|
670
|
-
end
|
|
671
|
-
when true
|
|
672
|
-
xml_val = "1"
|
|
673
|
-
type = "b"
|
|
674
|
-
when false
|
|
675
|
-
xml_val = "0"
|
|
676
|
-
type = "b"
|
|
677
|
-
when Time, DateTime
|
|
678
|
-
xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
|
|
679
|
-
when Date
|
|
680
|
-
xml_val = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
|
|
681
|
-
when BigDecimal
|
|
682
|
-
xml_val = value.to_s("F")
|
|
683
|
-
when Xlsxrb::Elements::CellError
|
|
684
|
-
xml_val = value.code
|
|
685
|
-
type = "e"
|
|
686
|
-
end
|
|
687
|
-
|
|
688
|
-
raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite?
|
|
689
|
-
|
|
690
|
-
formula_expr = formula_expr[1..] if formula_expr&.start_with?("=")
|
|
691
|
-
|
|
692
|
-
buf << '<c r="' << col_ref << row_num_str << '"'
|
|
693
|
-
buf << ' s="' << style_id.to_s << '"' if style_id
|
|
694
|
-
buf << ' t="' << type << '"' if type
|
|
695
|
-
if formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
|
|
696
|
-
f_attrs = +""
|
|
697
|
-
if formula_type
|
|
698
|
-
case formula_type.to_sym
|
|
699
|
-
when :array
|
|
700
|
-
f_attrs << ' t="array"'
|
|
701
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
702
|
-
when :shared
|
|
703
|
-
f_attrs << ' t="shared"'
|
|
704
|
-
f_attrs << %( si="#{formula_si}") if formula_si
|
|
705
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
706
|
-
end
|
|
707
|
-
end
|
|
708
|
-
f_attrs << ' ca="1"' if formula_ca
|
|
709
|
-
if formula_expr && !formula_expr.empty?
|
|
710
|
-
buf << "><f#{f_attrs}>" << escape_xml(formula_expr) << "</f>"
|
|
711
|
-
else
|
|
712
|
-
buf << "><f#{f_attrs}/>"
|
|
713
|
-
end
|
|
714
|
-
if xml_val.nil?
|
|
715
|
-
buf << "<v>0</v></c>"
|
|
716
|
-
else
|
|
717
|
-
v_str = xml_val.to_s
|
|
718
|
-
v_str = escape_xml(v_str) if type == "str" || xml_val.is_a?(String)
|
|
719
|
-
buf << "<v>" << v_str << "</v></c>"
|
|
720
|
-
end
|
|
721
|
-
elsif type == "inlineStr"
|
|
722
|
-
if xml_val.is_a?(Xlsxrb::Elements::RichText)
|
|
723
|
-
write_inline_rich_text(buf, xml_val)
|
|
724
|
-
else
|
|
725
|
-
buf << "><is><t>" << escape_xml(xml_val.to_s) << "</t></is></c>"
|
|
726
|
-
end
|
|
727
|
-
else
|
|
728
|
-
buf << "><v>" << xml_val.to_s << "</v></c>"
|
|
729
|
-
end
|
|
730
|
-
|
|
731
|
-
col_index += 1
|
|
732
|
-
end
|
|
589
|
+
write_styled_cells(buf, values, max_len, row_num_str, styles, is_styles_collection, single_style_id, row_style_id, style_map, sst, sst_index)
|
|
733
590
|
|
|
734
591
|
buf << "</row>"
|
|
735
592
|
return unless buf.bytesize >= 32_768
|
|
@@ -795,12 +652,223 @@ module Xlsxrb
|
|
|
795
652
|
|
|
796
653
|
private
|
|
797
654
|
|
|
655
|
+
# Writes the cells of a row that has cell styles, row attributes, or column styles.
|
|
656
|
+
# It is not inlined in write_row_values because YJIT ran the same loop there about
|
|
657
|
+
# 1.6 times slower, after the row attribute handling.
|
|
658
|
+
def write_styled_cells(buf, values, max_len, row_num_str, styles, is_styles_collection, single_style_id, row_style_id, style_map, sst, sst_index)
|
|
659
|
+
style_attrs = @style_attrs
|
|
660
|
+
column_ids = @column_styles && column_style_ids(style_map)
|
|
661
|
+
col_index = 0
|
|
662
|
+
while col_index < max_len
|
|
663
|
+
# Indexing past the end of values or styles (Array or Hash) yields nil.
|
|
664
|
+
value = values[col_index]
|
|
665
|
+
cell_style_id = is_styles_collection ? resolve_style_id(styles[col_index], style_map) : single_style_id
|
|
666
|
+
style_id = cell_style_id || row_style_id || (column_ids && column_ids[col_index])
|
|
667
|
+
col_ref = COLUMN_LETTERS[col_index] || column_letter(col_index)
|
|
668
|
+
col_index += 1
|
|
669
|
+
|
|
670
|
+
if value.nil?
|
|
671
|
+
buf << '<c r="' << col_ref << row_num_str << '" s="' << cell_style_id.to_s << '"/>' if cell_style_id
|
|
672
|
+
next
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
# '" s="<id>' to append after the cell reference, or "" for an unstyled cell
|
|
676
|
+
s_attr = style_id ? (style_attrs[style_id] ||= -%(" s="#{style_id})) : ""
|
|
677
|
+
case value
|
|
678
|
+
when Integer
|
|
679
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << (value >= 0 && value < 65_536 ? INTEGER_STRINGS[value] : value.to_s) << "</v></c>"
|
|
680
|
+
when Float
|
|
681
|
+
raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" unless value.finite?
|
|
682
|
+
|
|
683
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << value.to_s << "</v></c>"
|
|
684
|
+
when String
|
|
685
|
+
DslHelpers.check_cell_text_length!(value) if @strict_excel_mode && value.bytesize > DslHelpers::MAX_CELL_TEXT_LENGTH
|
|
686
|
+
|
|
687
|
+
if sst_index
|
|
688
|
+
idx = sst_index[value]
|
|
689
|
+
unless idx
|
|
690
|
+
sst << value
|
|
691
|
+
idx = sst.size - 1
|
|
692
|
+
sst_index[value] = idx
|
|
693
|
+
end
|
|
694
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="s"><v>' << (INTEGER_STRINGS[idx] || idx.to_s) << "</v></c>"
|
|
695
|
+
else
|
|
696
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="inlineStr"><is><t>' << escape_text(value) << "</t></is></c>"
|
|
697
|
+
end
|
|
698
|
+
when true
|
|
699
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="b"><v>1</v></c>'
|
|
700
|
+
when false
|
|
701
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="b"><v>0</v></c>'
|
|
702
|
+
when Time, DateTime
|
|
703
|
+
if style_id.nil? && style_map && (time_style_id = style_map["__xlsxrb_time"]).is_a?(Integer)
|
|
704
|
+
s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id})
|
|
705
|
+
end
|
|
706
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904).to_s << "</v></c>"
|
|
707
|
+
when Date
|
|
708
|
+
if style_id.nil? && style_map && (date_style_id = style_map["__xlsxrb_date"]).is_a?(Integer)
|
|
709
|
+
s_attr = style_attrs[date_style_id] ||= -%(" s="#{date_style_id})
|
|
710
|
+
end
|
|
711
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904).to_s << "</v></c>"
|
|
712
|
+
when TimeLike
|
|
713
|
+
if style_id.nil?
|
|
714
|
+
time_style_id = style_map && style_map["__xlsxrb_time"]
|
|
715
|
+
elsif @time_like_style_resolver
|
|
716
|
+
time_style_id = @time_like_style_resolver.call(style_id)
|
|
717
|
+
end
|
|
718
|
+
s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id}) if time_style_id.is_a?(Integer)
|
|
719
|
+
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904).to_s << "</v></c>"
|
|
720
|
+
else
|
|
721
|
+
write_complex_cell(buf, col_ref, row_num_str, value, style_id, sst, sst_index)
|
|
722
|
+
end
|
|
723
|
+
end
|
|
724
|
+
end
|
|
725
|
+
|
|
726
|
+
# The style id of each column style, indexed by column, resolved once instead of
|
|
727
|
+
# for every cell. It is resolved again when the style map is another Hash, or after
|
|
728
|
+
# set_column_style or forget_column_style_ids.
|
|
729
|
+
#: (Hash[untyped, Integer]? style_map) -> Array[Integer?]
|
|
730
|
+
def column_style_ids(style_map)
|
|
731
|
+
ids = @column_style_ids
|
|
732
|
+
return ids if ids && @column_style_ids_map.equal?(style_map)
|
|
733
|
+
|
|
734
|
+
ids = []
|
|
735
|
+
@column_styles&.each { |col_index, style| ids[col_index] = resolve_style_id(style, style_map) }
|
|
736
|
+
@column_style_ids_map = style_map
|
|
737
|
+
@column_style_ids = ids
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
# Writes a cell for write_styled_cells whose value is a formula, rich text, BigDecimal,
|
|
741
|
+
# cell error, or another value that write_styled_cells does not write itself.
|
|
742
|
+
def write_complex_cell(buf, col_ref, row_num_str, value, style_id, sst, sst_index)
|
|
743
|
+
formula_expr = nil
|
|
744
|
+
formula_ca = false
|
|
745
|
+
formula_type = nil
|
|
746
|
+
formula_ref = nil
|
|
747
|
+
formula_si = nil
|
|
748
|
+
xml_val = value
|
|
749
|
+
type = nil
|
|
750
|
+
|
|
751
|
+
case value
|
|
752
|
+
when Xlsxrb::Elements::Formula
|
|
753
|
+
formula_expr = value.expression
|
|
754
|
+
formula_ca = value.calculate_always?
|
|
755
|
+
formula_type = value.type
|
|
756
|
+
formula_ref = value.ref
|
|
757
|
+
formula_si = value.shared_index
|
|
758
|
+
xml_val = value.cached_value
|
|
759
|
+
case value.cached_value
|
|
760
|
+
when String
|
|
761
|
+
type = "str"
|
|
762
|
+
when true
|
|
763
|
+
type = "b"
|
|
764
|
+
xml_val = "1"
|
|
765
|
+
when false
|
|
766
|
+
type = "b"
|
|
767
|
+
xml_val = "0"
|
|
768
|
+
when Xlsxrb::Elements::CellError
|
|
769
|
+
type = "e"
|
|
770
|
+
xml_val = value.cached_value.code
|
|
771
|
+
end
|
|
772
|
+
when Hash
|
|
773
|
+
if value.key?(:formula)
|
|
774
|
+
formula_expr = value[:formula] || value[:expression]
|
|
775
|
+
formula_ca = value[:calculate_always] || false
|
|
776
|
+
formula_type = value[:type]
|
|
777
|
+
formula_ref = value[:ref]
|
|
778
|
+
formula_si = value[:si] || value[:shared_index]
|
|
779
|
+
xml_val = if value.key?(:value)
|
|
780
|
+
value[:value]
|
|
781
|
+
elsif value.key?(:cached_value)
|
|
782
|
+
value[:cached_value]
|
|
783
|
+
end
|
|
784
|
+
case xml_val
|
|
785
|
+
when String
|
|
786
|
+
type = "str"
|
|
787
|
+
when true
|
|
788
|
+
type = "b"
|
|
789
|
+
xml_val = "1"
|
|
790
|
+
when false
|
|
791
|
+
type = "b"
|
|
792
|
+
xml_val = "0"
|
|
793
|
+
when Xlsxrb::Elements::CellError
|
|
794
|
+
type = "e"
|
|
795
|
+
xml_val = xml_val.code
|
|
796
|
+
end
|
|
797
|
+
end
|
|
798
|
+
when Xlsxrb::Elements::RichText
|
|
799
|
+
if sst_index
|
|
800
|
+
idx = sst_index[value]
|
|
801
|
+
unless idx
|
|
802
|
+
sst << value
|
|
803
|
+
idx = sst.size - 1
|
|
804
|
+
sst_index[value] = idx
|
|
805
|
+
end
|
|
806
|
+
xml_val = idx
|
|
807
|
+
type = "s"
|
|
808
|
+
else
|
|
809
|
+
xml_val = value
|
|
810
|
+
type = "inlineStr"
|
|
811
|
+
end
|
|
812
|
+
when BigDecimal
|
|
813
|
+
xml_val = value.to_s("F")
|
|
814
|
+
when Xlsxrb::Elements::CellError
|
|
815
|
+
xml_val = value.code
|
|
816
|
+
type = "e"
|
|
817
|
+
end
|
|
818
|
+
|
|
819
|
+
raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite?
|
|
820
|
+
|
|
821
|
+
formula_expr = formula_expr[1..] if formula_expr&.start_with?("=")
|
|
822
|
+
|
|
823
|
+
buf << '<c r="' << col_ref << row_num_str << '"'
|
|
824
|
+
buf << ' s="' << style_id.to_s << '"' if style_id
|
|
825
|
+
buf << ' t="' << type << '"' if type
|
|
826
|
+
if formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
|
|
827
|
+
f_attrs = +""
|
|
828
|
+
if formula_type
|
|
829
|
+
case formula_type.to_sym
|
|
830
|
+
when :array
|
|
831
|
+
f_attrs << ' t="array"'
|
|
832
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
833
|
+
when :shared
|
|
834
|
+
f_attrs << ' t="shared"'
|
|
835
|
+
f_attrs << %( si="#{escape_xml(formula_si)}") if formula_si
|
|
836
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
837
|
+
end
|
|
838
|
+
end
|
|
839
|
+
f_attrs << ' ca="1"' if formula_ca
|
|
840
|
+
if formula_expr && !formula_expr.empty?
|
|
841
|
+
buf << "><f#{f_attrs}>" << escape_xml(formula_expr) << "</f>"
|
|
842
|
+
else
|
|
843
|
+
buf << "><f#{f_attrs}/>"
|
|
844
|
+
end
|
|
845
|
+
if xml_val.nil?
|
|
846
|
+
buf << "<v>0</v></c>"
|
|
847
|
+
else
|
|
848
|
+
v_str = xml_val.to_s
|
|
849
|
+
v_str = escape_text(v_str) if type == "str" || xml_val.is_a?(String)
|
|
850
|
+
buf << "<v>" << v_str << "</v></c>"
|
|
851
|
+
end
|
|
852
|
+
elsif type == "inlineStr"
|
|
853
|
+
if xml_val.is_a?(Xlsxrb::Elements::RichText)
|
|
854
|
+
write_inline_rich_text(buf, xml_val)
|
|
855
|
+
else
|
|
856
|
+
buf << "><is><t>" << escape_text(xml_val.to_s) << "</t></is></c>"
|
|
857
|
+
end
|
|
858
|
+
else
|
|
859
|
+
buf << "><v>" << xml_val.to_s << "</v></c>"
|
|
860
|
+
end
|
|
861
|
+
end
|
|
862
|
+
|
|
798
863
|
def resolve_style_id(style, style_map)
|
|
799
864
|
return nil if style.nil?
|
|
865
|
+
# Without names to look up, an index is the style id.
|
|
866
|
+
return style if style_map.nil? && style.is_a?(Integer)
|
|
800
867
|
|
|
801
868
|
if style_map
|
|
802
869
|
mapped = style_map[style] || style_map[style.to_s]
|
|
803
|
-
|
|
870
|
+
# A style id is written into an attribute as it is, so only an Integer is one.
|
|
871
|
+
return mapped if mapped.is_a?(Integer)
|
|
804
872
|
end
|
|
805
873
|
|
|
806
874
|
return 0 if [:default, :none, "default", "none"].include?(style)
|
|
@@ -1704,7 +1772,7 @@ module Xlsxrb
|
|
|
1704
1772
|
buf << "<u/>"
|
|
1705
1773
|
else
|
|
1706
1774
|
mapped_u = StyleBuilder::UNDERLINE_MAP[u_val.to_sym] || u_val
|
|
1707
|
-
buf << '<u val="' << mapped_u
|
|
1775
|
+
buf << '<u val="' << escape_xml(mapped_u) << '"/>'
|
|
1708
1776
|
end
|
|
1709
1777
|
end
|
|
1710
1778
|
va_val = font[:vert_align] || font[:vertical_align]
|
|
@@ -1712,45 +1780,45 @@ module Xlsxrb
|
|
|
1712
1780
|
va_val ||= "subscript" if font[:subscript]
|
|
1713
1781
|
if va_val
|
|
1714
1782
|
mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val
|
|
1715
|
-
buf << '<vertAlign val="' << mapped_va
|
|
1783
|
+
buf << '<vertAlign val="' << escape_xml(mapped_va) << '"/>'
|
|
1716
1784
|
end
|
|
1717
1785
|
font_sz = font[:sz] || font[:size]
|
|
1718
|
-
buf << '<sz val="' << font_sz
|
|
1786
|
+
buf << '<sz val="' << escape_xml(font_sz) << '"/>' if font_sz
|
|
1719
1787
|
if font[:color]
|
|
1720
1788
|
if font[:color].is_a?(Hash)
|
|
1721
1789
|
c_hash = font[:color]
|
|
1722
1790
|
if c_hash[:rgb]
|
|
1723
1791
|
norm = StyleBuilder.normalize_color(c_hash[:rgb], alpha: true) || c_hash[:rgb].to_s
|
|
1724
|
-
buf << '<color rgb="' << norm << '"/>'
|
|
1792
|
+
buf << '<color rgb="' << escape_xml(norm) << '"/>'
|
|
1725
1793
|
elsif c_hash[:theme]
|
|
1726
|
-
tint_attr = c_hash[:tint] ? %( tint="#{c_hash[:tint]}") : ""
|
|
1727
|
-
buf << '<color theme="' << c_hash[:theme]
|
|
1794
|
+
tint_attr = c_hash[:tint] ? %( tint="#{escape_xml(c_hash[:tint])}") : ""
|
|
1795
|
+
buf << '<color theme="' << escape_xml(c_hash[:theme]) << '"' << tint_attr << "/>"
|
|
1728
1796
|
elsif c_hash[:indexed]
|
|
1729
|
-
buf << '<color indexed="' << c_hash[:indexed]
|
|
1797
|
+
buf << '<color indexed="' << escape_xml(c_hash[:indexed]) << '"/>'
|
|
1730
1798
|
elsif c_hash[:auto]
|
|
1731
1799
|
buf << '<color auto="1"/>'
|
|
1732
1800
|
end
|
|
1733
1801
|
else
|
|
1734
1802
|
raw_color = font[:color]
|
|
1735
1803
|
norm_color = StyleBuilder.normalize_color(raw_color, alpha: true) || raw_color.to_s
|
|
1736
|
-
buf << '<color rgb="' << norm_color << '"/>'
|
|
1804
|
+
buf << '<color rgb="' << escape_xml(norm_color) << '"/>'
|
|
1737
1805
|
end
|
|
1738
1806
|
elsif font[:theme]
|
|
1739
|
-
tint_attr = font[:tint] ? %( tint="#{font[:tint]}") : ""
|
|
1740
|
-
buf << '<color theme="' << font[:theme]
|
|
1807
|
+
tint_attr = font[:tint] ? %( tint="#{escape_xml(font[:tint])}") : ""
|
|
1808
|
+
buf << '<color theme="' << escape_xml(font[:theme]) << '"' << tint_attr << "/>"
|
|
1741
1809
|
elsif font[:indexed]
|
|
1742
|
-
buf << '<color indexed="' << font[:indexed]
|
|
1810
|
+
buf << '<color indexed="' << escape_xml(font[:indexed]) << '"/>'
|
|
1743
1811
|
elsif font[:auto]
|
|
1744
1812
|
buf << '<color auto="1"/>'
|
|
1745
1813
|
end
|
|
1746
1814
|
buf << '<rFont val="' << escape_xml(font[:name]) << '"/>' if font[:name]
|
|
1747
|
-
buf << '<family val="' << font[:family]
|
|
1748
|
-
buf << '<scheme val="' << font[:scheme]
|
|
1815
|
+
buf << '<family val="' << escape_xml(font[:family]) << '"/>' if font[:family]
|
|
1816
|
+
buf << '<scheme val="' << escape_xml(font[:scheme]) << '"/>' if font[:scheme]
|
|
1749
1817
|
buf << "</rPr><t>"
|
|
1750
1818
|
else
|
|
1751
1819
|
buf << "<r><t>"
|
|
1752
1820
|
end
|
|
1753
|
-
buf <<
|
|
1821
|
+
buf << escape_text(text.to_s) << "</t></r>"
|
|
1754
1822
|
end
|
|
1755
1823
|
buf << "</is></c>"
|
|
1756
1824
|
end
|
|
@@ -1766,6 +1834,17 @@ module Xlsxrb
|
|
|
1766
1834
|
str = str.gsub(INVALID_XML_CHARS_RE, "") if str.match?(INVALID_XML_CHARS_RE)
|
|
1767
1835
|
str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
|
|
1768
1836
|
end
|
|
1837
|
+
|
|
1838
|
+
# Escapes cell text: encodes it as ST_Xstring (XmlBuilder.encode_xstring), so that
|
|
1839
|
+
# text that looks like _xHHHH_ and characters XML 1.0 cannot hold are read back as
|
|
1840
|
+
# written, then escapes the XML special characters.
|
|
1841
|
+
def escape_text(value)
|
|
1842
|
+
str = value.to_s
|
|
1843
|
+
str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
|
|
1844
|
+
# Most text has neither; the checks cost less than encode_xstring's own.
|
|
1845
|
+
str = XmlBuilder.encode_xstring(str) if str.include?("_x") || str.match?(INVALID_XML_CHARS_RE)
|
|
1846
|
+
str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
|
|
1847
|
+
end
|
|
1769
1848
|
end
|
|
1770
1849
|
end
|
|
1771
1850
|
end
|