xlsxrb 0.1.18 → 0.2.0
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 +163 -0
- data/README.md +34 -11
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
- data/lib/xlsxrb/chart_builder.rb +251 -386
- data/lib/xlsxrb/dsl_helpers.rb +120 -67
- data/lib/xlsxrb/elements/cell.rb +37 -157
- data/lib/xlsxrb/elements/column.rb +11 -19
- data/lib/xlsxrb/elements/coordinate_access.rb +15 -86
- data/lib/xlsxrb/elements/image.rb +5 -72
- data/lib/xlsxrb/elements/metadata.rb +463 -817
- data/lib/xlsxrb/elements/row.rb +27 -145
- data/lib/xlsxrb/elements/styles.rb +30 -88
- data/lib/xlsxrb/elements/types.rb +6 -99
- data/lib/xlsxrb/elements/workbook.rb +19 -126
- data/lib/xlsxrb/elements/worksheet.rb +43 -314
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -1
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -7
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +32 -17
- data/lib/xlsxrb/ooxml/reader.rb +49 -51
- data/lib/xlsxrb/ooxml/styles_parser.rb +26 -2
- data/lib/xlsxrb/ooxml/utils.rb +2 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +172 -93
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +65 -47
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +53 -14
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
- data/lib/xlsxrb/ooxml/writer.rb +105 -41
- data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
- data/lib/xlsxrb/ooxml/zip_reader.rb +65 -15
- data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
- data/lib/xlsxrb/stream_row.rb +12 -116
- data/lib/xlsxrb/stream_sheet.rb +14 -261
- data/lib/xlsxrb/stream_writer.rb +623 -1476
- data/lib/xlsxrb/style_builder.rb +376 -593
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +192 -226
- data/lib/xlsxrb/worksheet_builder.rb +456 -1324
- data/lib/xlsxrb.rb +266 -1083
- data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
- data/sig/generated/xlsxrb/dsl_helpers.rbs +64 -16
- data/sig/generated/xlsxrb/elements/cell.rbs +12 -76
- data/sig/generated/xlsxrb/elements/column.rbs +0 -7
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +5 -41
- data/sig/generated/xlsxrb/elements/image.rbs +3 -29
- data/sig/generated/xlsxrb/elements/metadata.rbs +138 -151
- data/sig/generated/xlsxrb/elements/row.rbs +10 -53
- data/sig/generated/xlsxrb/elements/styles.rbs +1 -39
- data/sig/generated/xlsxrb/elements/workbook.rbs +14 -78
- data/sig/generated/xlsxrb/elements/worksheet.rbs +5 -121
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +0 -4
- data/sig/generated/xlsxrb/ooxml/reader.rbs +0 -3
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +12 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +5 -3
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +22 -7
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
- data/sig/generated/xlsxrb/stream_row.rbs +7 -42
- data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
- data/sig/generated/xlsxrb/stream_writer.rbs +457 -976
- data/sig/generated/xlsxrb/style_builder.rbs +206 -242
- data/sig/generated/xlsxrb/workbook_builder.rbs +84 -132
- data/sig/generated/xlsxrb/worksheet_builder.rbs +309 -685
- data/sig/generated/xlsxrb.rbs +43 -190
- data/sig/rexml.rbs +8 -0
- metadata +3 -5
- data/lib/xlsxrb/utils.rb +0 -251
- data/sig/generated/xlsxrb/utils.rbs +0 -200
data/lib/xlsxrb.rb
CHANGED
|
@@ -35,7 +35,6 @@ require_relative "xlsxrb/worksheet_builder"
|
|
|
35
35
|
require_relative "xlsxrb/workbook_builder"
|
|
36
36
|
require_relative "xlsxrb/number_formatter"
|
|
37
37
|
require_relative "xlsxrb/stream_writer"
|
|
38
|
-
require_relative "xlsxrb/utils"
|
|
39
38
|
|
|
40
39
|
# Modern, streaming-capable, low-memory XLSX reading and writing library for Ruby.
|
|
41
40
|
#
|
|
@@ -50,34 +49,33 @@ module Xlsxrb
|
|
|
50
49
|
# Maximum number of columns supported by Excel / OpenXML.
|
|
51
50
|
MAX_COLUMN = 16_384
|
|
52
51
|
|
|
53
|
-
# @deprecated Use {MAX_COLUMN} instead. Will be removed in 0.2.0.
|
|
54
|
-
MAX_COL = MAX_COLUMN
|
|
55
|
-
deprecate_constant :MAX_COL
|
|
56
|
-
|
|
57
52
|
# Maximum number of individual cell/range references allowed in a single data validation element by Excel.
|
|
58
53
|
MAX_REFS_PER_VALIDATION = 32_767
|
|
59
54
|
|
|
60
|
-
#
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
# <workbookProtection> attributes and the Elements::WorkbookProtection members they load into.
|
|
56
|
+
WORKBOOK_PROTECTION_ATTRIBUTES = {
|
|
57
|
+
"workbookPassword" => %i[workbook_password string],
|
|
58
|
+
"lockStructure" => %i[lock_structure bool],
|
|
59
|
+
"lockWindows" => %i[lock_windows bool],
|
|
60
|
+
"lockRevision" => %i[lock_revision bool],
|
|
61
|
+
"revisionsPassword" => %i[revisions_password string],
|
|
62
|
+
"workbookAlgorithmName" => %i[algorithm_name string],
|
|
63
|
+
"workbookHashValue" => %i[hash_value string],
|
|
64
|
+
"workbookSaltValue" => %i[salt_value string],
|
|
65
|
+
"workbookSpinCount" => %i[spin_count int],
|
|
66
|
+
"revisionsAlgorithmName" => %i[revisions_algorithm_name string],
|
|
67
|
+
"revisionsHashValue" => %i[revisions_hash_value string],
|
|
68
|
+
"revisionsSaltValue" => %i[revisions_salt_value string],
|
|
69
|
+
"revisionsSpinCount" => %i[revisions_spin_count int]
|
|
70
|
+
}.transform_values(&:freeze).freeze
|
|
71
|
+
private_constant :WORKBOOK_PROTECTION_ATTRIBUTES
|
|
72
|
+
|
|
73
|
+
# ZIP deflate level used when no compression_level is given.
|
|
74
|
+
# See docs/adr/0009-default-zip-compression-level.md.
|
|
75
|
+
DEFAULT_COMPRESSION_LEVEL = 1
|
|
66
76
|
|
|
67
77
|
# Base class for all exceptions raised by Xlsxrb.
|
|
68
|
-
|
|
69
|
-
class Error < StandardError
|
|
70
|
-
DIV0 = Elements::CellError::DIV0
|
|
71
|
-
NA = Elements::CellError::NA
|
|
72
|
-
NAME = Elements::CellError::NAME
|
|
73
|
-
NULL = Elements::CellError::NULL
|
|
74
|
-
NUM = Elements::CellError::NUM
|
|
75
|
-
REF = Elements::CellError::REF
|
|
76
|
-
VALUE = Elements::CellError::VALUE
|
|
77
|
-
GETTING_DATA = Elements::CellError::GETTING_DATA
|
|
78
|
-
|
|
79
|
-
deprecate_constant :DIV0, :NA, :NAME, :NULL, :NUM, :REF, :VALUE, :GETTING_DATA
|
|
80
|
-
end
|
|
78
|
+
class Error < StandardError; end
|
|
81
79
|
|
|
82
80
|
# Raised when parsing malformed or invalid XML structures.
|
|
83
81
|
class ParseError < Error; end
|
|
@@ -214,19 +212,16 @@ module Xlsxrb
|
|
|
214
212
|
# @param type [Symbol, String, nil] Formula type (:shared, :array, etc.).
|
|
215
213
|
# @param ref [String, nil] Target cell or range reference (e.g. "A1:C1").
|
|
216
214
|
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
217
|
-
# @param si [Integer, nil] Alias for shared_index.
|
|
218
215
|
# @return [Elements::Formula]
|
|
219
216
|
# @api public
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
def self.formula(expression, cached_value: nil, type: nil, ref: nil, shared_index: nil, si: nil)
|
|
223
|
-
s_idx = shared_index || si
|
|
217
|
+
#: (String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?) -> Elements::Formula
|
|
218
|
+
def self.formula(expression, cached_value: nil, type: nil, ref: nil, shared_index: nil)
|
|
224
219
|
Elements::Formula.new(
|
|
225
220
|
expression: expression,
|
|
226
221
|
cached_value: cached_value,
|
|
227
|
-
type: type || (
|
|
222
|
+
type: type || (shared_index ? :shared : nil),
|
|
228
223
|
ref: ref,
|
|
229
|
-
shared_index:
|
|
224
|
+
shared_index: shared_index,
|
|
230
225
|
calculate_always: cached_value.nil? || nil
|
|
231
226
|
)
|
|
232
227
|
end
|
|
@@ -241,52 +236,44 @@ module Xlsxrb
|
|
|
241
236
|
#
|
|
242
237
|
# @param expression [String, nil] The formula expression (nil for follower cells).
|
|
243
238
|
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
244
|
-
# @param si [Integer, nil] Legacy alias for shared_index.
|
|
245
239
|
# @param ref [String, nil] Target range for master cell (e.g. "B2:B10").
|
|
246
240
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
247
241
|
# @return [Elements::Formula]
|
|
248
242
|
# @api public
|
|
249
|
-
#: (?String? expression, ?shared_index: Integer?, ?
|
|
250
|
-
def self.shared_formula(expression = nil, shared_index: nil,
|
|
251
|
-
|
|
252
|
-
s_idx = shared_index || si
|
|
253
|
-
raise ArgumentError, "shared_index is required for shared formula" if s_idx.nil?
|
|
243
|
+
#: (?String? expression, ?shared_index: Integer?, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil)) -> Elements::Formula
|
|
244
|
+
def self.shared_formula(expression = nil, shared_index: nil, ref: nil, cached_value: nil)
|
|
245
|
+
raise ArgumentError, "shared_index is required for shared formula" if shared_index.nil?
|
|
254
246
|
|
|
255
247
|
Elements::Formula.new(
|
|
256
248
|
expression: expression || "",
|
|
257
249
|
cached_value: cached_value,
|
|
258
250
|
type: :shared,
|
|
259
251
|
ref: ref,
|
|
260
|
-
shared_index:
|
|
252
|
+
shared_index: shared_index,
|
|
261
253
|
calculate_always: cached_value.nil? || nil
|
|
262
254
|
)
|
|
263
255
|
end
|
|
264
|
-
# rubocop:enable Naming/MethodParameterName
|
|
265
256
|
|
|
266
257
|
# Creates an array or dynamic array {Elements::Formula} object.
|
|
267
258
|
#
|
|
268
259
|
# @example Create a multi-cell array formula
|
|
269
|
-
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", "A1:C1", cached_value: 0)
|
|
270
260
|
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", ref: "A1:C1", cached_value: 0)
|
|
271
261
|
#
|
|
272
262
|
# @example Create a dynamic array formula
|
|
273
|
-
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", "D1")
|
|
263
|
+
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", ref: "D1")
|
|
274
264
|
#
|
|
275
265
|
# @param expression [String] The formula expression without leading '='.
|
|
276
|
-
# @param pos_ref [String, nil] Target cell or range reference (positional).
|
|
277
266
|
# @param ref [String, nil] Target cell or range reference (keyword).
|
|
278
267
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
279
268
|
# @return [Elements::Formula]
|
|
280
269
|
# @api public
|
|
281
|
-
#: (String expression, ?
|
|
282
|
-
def self.array_formula(expression,
|
|
283
|
-
warn "[Xlsxrb DEPRECATION] Passing positional ref to Xlsxrb.array_formula is deprecated and will be removed in v0.2.0. Use ref: keyword argument instead.", uplevel: 1 unless pos_ref.nil?
|
|
284
|
-
target_ref = ref || pos_ref
|
|
270
|
+
#: (String expression, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) -> Elements::Formula
|
|
271
|
+
def self.array_formula(expression, ref: nil, cached_value: nil, **attrs)
|
|
285
272
|
Elements::Formula.new(
|
|
286
273
|
expression: expression,
|
|
287
274
|
cached_value: cached_value,
|
|
288
275
|
type: :array,
|
|
289
|
-
ref:
|
|
276
|
+
ref: ref,
|
|
290
277
|
calculate_always: cached_value.nil? || nil,
|
|
291
278
|
**attrs
|
|
292
279
|
)
|
|
@@ -367,13 +354,6 @@ module Xlsxrb
|
|
|
367
354
|
NumberFormatter.format(value, format_code, date1904: date1904)
|
|
368
355
|
end
|
|
369
356
|
|
|
370
|
-
# @deprecated Use {.format_value} instead. Will be removed in 0.2.0.
|
|
371
|
-
#: (untyped value, ?String? format_code, ?date1904: bool) -> String
|
|
372
|
-
def self.format(value, format_code = nil, date1904: false)
|
|
373
|
-
warn "[DEPRECATION] Xlsxrb.format is deprecated; use Xlsxrb.format_value instead.", uplevel: 1
|
|
374
|
-
format_value(value, format_code, date1904: date1904)
|
|
375
|
-
end
|
|
376
|
-
|
|
377
357
|
# Converts a Date or Time to an Excel numeric serial number (1900 or 1904 system).
|
|
378
358
|
#
|
|
379
359
|
# @example
|
|
@@ -385,7 +365,7 @@ module Xlsxrb
|
|
|
385
365
|
# @api public
|
|
386
366
|
#: (Date | Time date_or_time, ?date1904: bool) -> (Integer | Float)
|
|
387
367
|
def self.date_to_serial(date_or_time, date1904: false)
|
|
388
|
-
if date_or_time.is_a?(Time)
|
|
368
|
+
if date_or_time.is_a?(Time) || date_or_time.is_a?(DateTime)
|
|
389
369
|
Ooxml::Utils.datetime_to_serial(date_or_time, date1904: date1904)
|
|
390
370
|
else
|
|
391
371
|
Ooxml::Utils.date_to_serial(date_or_time, date1904: date1904)
|
|
@@ -432,20 +412,13 @@ module Xlsxrb
|
|
|
432
412
|
# @api public
|
|
433
413
|
#: (Time | Date time, ?date1904: bool) -> Float
|
|
434
414
|
def self.time_to_serial(time, date1904: false)
|
|
435
|
-
if time.is_a?(Time)
|
|
415
|
+
if time.is_a?(Time) || time.is_a?(DateTime)
|
|
436
416
|
Ooxml::Utils.datetime_to_serial(time, date1904: date1904)
|
|
437
417
|
else
|
|
438
418
|
Ooxml::Utils.date_to_serial(time, date1904: date1904).to_f
|
|
439
419
|
end
|
|
440
420
|
end
|
|
441
421
|
|
|
442
|
-
# @deprecated Use {.time_to_serial} instead. Will be removed in 0.2.0.
|
|
443
|
-
#: (Time | Date time, ?date1904: bool) -> Float
|
|
444
|
-
def self.datetime_to_serial(time, date1904: false)
|
|
445
|
-
warn "[DEPRECATION] Xlsxrb.datetime_to_serial is deprecated; use Xlsxrb.time_to_serial instead.", uplevel: 1
|
|
446
|
-
time_to_serial(time, date1904: date1904)
|
|
447
|
-
end
|
|
448
|
-
|
|
449
422
|
# Converts an Excel serial number to a Time (UTC).
|
|
450
423
|
#
|
|
451
424
|
# @example
|
|
@@ -460,13 +433,6 @@ module Xlsxrb
|
|
|
460
433
|
Ooxml::Utils.serial_to_datetime(serial, date1904: date1904)
|
|
461
434
|
end
|
|
462
435
|
|
|
463
|
-
# @deprecated Use {.serial_to_time} instead. Will be removed in 0.2.0.
|
|
464
|
-
#: (Numeric serial, ?date1904: bool) -> Time
|
|
465
|
-
def self.serial_to_datetime(serial, date1904: false)
|
|
466
|
-
warn "[DEPRECATION] Xlsxrb.serial_to_datetime is deprecated; use Xlsxrb.serial_to_time instead.", uplevel: 1
|
|
467
|
-
serial_to_time(serial, date1904: date1904)
|
|
468
|
-
end
|
|
469
|
-
|
|
470
436
|
# Converts a cell reference string (e.g. "A1", "BC100") to 0-based [row, col] coordinates.
|
|
471
437
|
#
|
|
472
438
|
# @example
|
|
@@ -613,27 +579,21 @@ module Xlsxrb
|
|
|
613
579
|
# Expands a formula by shifting relative cell references from a master cell to a target cell or by offset amounts.
|
|
614
580
|
#
|
|
615
581
|
# @example Shift formula down one row
|
|
616
|
-
# Xlsxrb.expand_formula("A2*2", 1, 0) #=> "A3*2"
|
|
582
|
+
# Xlsxrb.expand_formula("A2*2", row_delta: 1, column_delta: 0) #=> "A3*2"
|
|
617
583
|
# Xlsxrb.expand_formula("A2*2", from_ref: "B2", to_ref: "B3") #=> "A3*2"
|
|
618
584
|
#
|
|
619
585
|
# @example Shift formula with absolute references
|
|
620
|
-
# Xlsxrb.expand_formula("$A$1+A2", 2, 0) #=> "$A$1+A4"
|
|
586
|
+
# Xlsxrb.expand_formula("$A$1+A2", row_delta: 2, column_delta: 0) #=> "$A$1+A4"
|
|
621
587
|
#
|
|
622
588
|
# @param formula [String, nil] Formula expression to expand.
|
|
623
|
-
# @param offset_row [Integer, nil] Optional row shift amount (positive = down, negative = up).
|
|
624
|
-
# @param offset_col [Integer, nil] Optional column shift amount (positive = right, negative = left).
|
|
625
589
|
# @param from_ref [String, nil] Master cell reference (e.g. "B2").
|
|
626
590
|
# @param to_ref [String, nil] Target cell reference (e.g. "B3").
|
|
627
591
|
# @param row_delta [Integer, nil] Row shift amount (positive = down, negative = up).
|
|
628
592
|
# @param column_delta [Integer, nil] Column shift amount (positive = right, negative = left).
|
|
629
|
-
# @param col_delta [Integer, nil] Deprecated alias for column_delta.
|
|
630
593
|
# @return [String, nil] Expanded formula expression.
|
|
631
594
|
# @api public
|
|
632
|
-
#: (String? formula, ?
|
|
633
|
-
def self.expand_formula(formula,
|
|
634
|
-
warn "[Xlsxrb DEPRECATION] Passing positional offset arguments to expand_formula is deprecated; use row_delta: and column_delta: instead.", uplevel: 1 unless offset_row.nil? && offset_col.nil?
|
|
635
|
-
warn "[Xlsxrb DEPRECATION] Keyword col_delta: in expand_formula is deprecated; use column_delta: instead.", uplevel: 1 unless col_delta.nil?
|
|
636
|
-
|
|
595
|
+
#: (String? formula, ?from_ref: String?, ?to_ref: String?, ?row_delta: Integer?, ?column_delta: Integer?) -> String?
|
|
596
|
+
def self.expand_formula(formula, from_ref: nil, to_ref: nil, row_delta: nil, column_delta: nil)
|
|
637
597
|
return formula if formula.nil? || formula.empty?
|
|
638
598
|
|
|
639
599
|
if from_ref && to_ref
|
|
@@ -644,8 +604,8 @@ module Xlsxrb
|
|
|
644
604
|
rd = t_coords[0] - f_coords[0]
|
|
645
605
|
cd = t_coords[1] - f_coords[1]
|
|
646
606
|
else
|
|
647
|
-
rd = row_delta ||
|
|
648
|
-
cd = column_delta ||
|
|
607
|
+
rd = row_delta || 0
|
|
608
|
+
cd = column_delta || 0
|
|
649
609
|
end
|
|
650
610
|
|
|
651
611
|
return formula if rd.zero? && cd.zero?
|
|
@@ -712,13 +672,6 @@ module Xlsxrb
|
|
|
712
672
|
Colors.to_hex(color, alpha: alpha)
|
|
713
673
|
end
|
|
714
674
|
|
|
715
|
-
# @deprecated Use {.to_hex_color} instead. Will be removed in 0.2.0.
|
|
716
|
-
#: (Integer | String | Symbol | nil color, ?alpha: bool) -> String?
|
|
717
|
-
def self.color_to_hex(color, alpha: false)
|
|
718
|
-
warn "[DEPRECATION] Xlsxrb.color_to_hex is deprecated; use Xlsxrb.to_hex_color instead.", uplevel: 1
|
|
719
|
-
to_hex_color(color, alpha: alpha)
|
|
720
|
-
end
|
|
721
|
-
|
|
722
675
|
# Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters.
|
|
723
676
|
# Replaces forbidden characters (\ / ? * [ ] : and control characters),
|
|
724
677
|
# replaces leading/trailing apostrophes, and replaces reserved 'History' with 'History_'.
|
|
@@ -736,38 +689,11 @@ module Xlsxrb
|
|
|
736
689
|
DslHelpers.sanitize_sheet_name(name, replace_char: replace_char)
|
|
737
690
|
end
|
|
738
691
|
|
|
739
|
-
# @deprecated Use {.sanitize_sheet_name} instead. Will be removed in 0.2.0.
|
|
740
|
-
#: (untyped name, ?replace_char: String) -> String
|
|
741
|
-
def self.sheet_name(name, replace_char: "_")
|
|
742
|
-
warn "[DEPRECATION] Xlsxrb.sheet_name is deprecated; use Xlsxrb.sanitize_sheet_name instead.", uplevel: 1
|
|
743
|
-
sanitize_sheet_name(name, replace_char: replace_char)
|
|
744
|
-
end
|
|
745
|
-
|
|
746
692
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
747
693
|
#
|
|
748
694
|
# Sheets and rows are streamed lazily with O(1) constant memory. If a block is given,
|
|
749
|
-
# yields each {StreamSheet} sequentially. Call {#load}
|
|
750
|
-
#
|
|
751
|
-
#
|
|
752
|
-
# @overload read(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, &block)
|
|
753
|
-
# Yields each {StreamSheet} sequentially in streaming mode.
|
|
754
|
-
# @param source [String, IO, StringIO] File path, binary content string, or readable IO stream.
|
|
755
|
-
# @param password [String, nil] Optional password to decrypt password-protected XLSX files.
|
|
756
|
-
# @param trim_empty_rows [Boolean] Whether to omit trailing empty rows during sheet iteration.
|
|
757
|
-
# @param pad_empty_rows [Boolean] Whether to yield empty rows for skipped row numbers.
|
|
758
|
-
# @param pad_empty_cells [Boolean] Whether to pad missing cells within rows.
|
|
759
|
-
# @yield [sheet]
|
|
760
|
-
# @yieldparam sheet [StreamSheet] The streaming worksheet object.
|
|
761
|
-
# @return [void]
|
|
762
|
-
#
|
|
763
|
-
# @overload read(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false)
|
|
764
|
-
# Returns a lazy {Elements::Workbook} object.
|
|
765
|
-
# @param source [String, IO, StringIO] File path, binary content string, or readable IO stream.
|
|
766
|
-
# @param password [String, nil] Optional password to decrypt password-protected XLSX files.
|
|
767
|
-
# @param trim_empty_rows [Boolean] Whether to omit trailing empty rows during sheet iteration.
|
|
768
|
-
# @param pad_empty_rows [Boolean] Whether to yield empty rows for skipped row numbers.
|
|
769
|
-
# @param pad_empty_cells [Boolean] Whether to pad missing cells within rows.
|
|
770
|
-
# @return [Elements::Workbook]
|
|
695
|
+
# yields each {StreamSheet} sequentially. Call {StreamSheet#load} to convert a sheet to
|
|
696
|
+
# an in-memory representation for coordinate random access (`sheet["A1"]`).
|
|
771
697
|
#
|
|
772
698
|
# @example Streaming read across sheets and rows (O(1) constant memory)
|
|
773
699
|
# Xlsxrb.read("large.xlsx") do |sheet|
|
|
@@ -777,51 +703,84 @@ module Xlsxrb
|
|
|
777
703
|
# end
|
|
778
704
|
# end
|
|
779
705
|
#
|
|
780
|
-
# @
|
|
781
|
-
#
|
|
782
|
-
#
|
|
783
|
-
#
|
|
784
|
-
#
|
|
785
|
-
#
|
|
786
|
-
# Reads an XLSX file or IO stream. When called with a block, yields each {StreamSheet}
|
|
787
|
-
# for low-memory streaming iteration. When called without a block, returns an {Elements::Workbook}
|
|
788
|
-
# containing {StreamSheet} instances.
|
|
789
|
-
#
|
|
790
|
-
# @deprecated Calling {Xlsxrb.read} without a block is deprecated; use {Xlsxrb.load} for in-memory random access. Will be removed in 0.2.0.
|
|
706
|
+
# @param source [String, IO, StringIO] File path, binary content string, or readable IO stream.
|
|
707
|
+
# @param password [String, nil] Optional password to decrypt password-protected XLSX files.
|
|
708
|
+
# @param trim_empty_rows [Boolean] Whether to omit trailing empty rows during sheet iteration.
|
|
709
|
+
# @param pad_empty_rows [Boolean] Whether to yield empty rows for skipped row numbers.
|
|
710
|
+
# @param pad_empty_cells [Boolean] Whether to pad missing cells within rows.
|
|
791
711
|
# @param max_uncompressed_size [Integer, nil] Maximum allowed uncompressed size in bytes per entry (ZIP bomb defense).
|
|
712
|
+
# @yield [sheet]
|
|
713
|
+
# @yieldparam sheet [StreamSheet] The streaming worksheet object.
|
|
714
|
+
# @return [Enumerator<StreamSheet>, void]
|
|
792
715
|
# @raise [ZipBombError] If an uncompressed entry exceeds max_uncompressed_size.
|
|
793
716
|
# @raise [EncryptedFileError] If the file is encrypted and no password was supplied.
|
|
794
717
|
# @raise [InvalidPasswordError] If the supplied password is incorrect.
|
|
795
718
|
# @raise [ParseError] If the spreadsheet XML structure is invalid.
|
|
796
719
|
# @api public
|
|
797
720
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) { (StreamSheet) -> void } -> void
|
|
798
|
-
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) ->
|
|
721
|
+
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Enumerator[StreamSheet, void]
|
|
799
722
|
def self.read(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil, &)
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
)
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
723
|
+
prepared_source = prepare_source_io(source, password)
|
|
724
|
+
return to_enum(:read, source, password: password, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells, max_uncompressed_size: max_uncompressed_size) unless block_given?
|
|
725
|
+
|
|
726
|
+
zip_options = {}
|
|
727
|
+
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
728
|
+
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
729
|
+
|
|
730
|
+
begin
|
|
731
|
+
workbook_path = resolve_workbook_entry(zip_reader)
|
|
732
|
+
wb_dir = File.dirname(workbook_path)
|
|
733
|
+
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
734
|
+
rels_xml = zip_reader.read_entry(wb_rels_name)
|
|
735
|
+
rels, rel_types = parse_workbook_relationships(rels_xml)
|
|
736
|
+
|
|
737
|
+
sst_entry = resolve_part_entry(zip_reader, rels, "sharedStrings", "sharedStrings.xml", wb_dir)
|
|
738
|
+
shared_strings_xml = sst_entry ? zip_reader.read_entry(sst_entry) : nil
|
|
739
|
+
shared_strings = shared_strings_xml ? Ooxml::SharedStringsParser.parse(shared_strings_xml) : []
|
|
740
|
+
|
|
741
|
+
styles_entry = resolve_part_entry(zip_reader, rels, "styles", "styles.xml", wb_dir)
|
|
742
|
+
styles_xml = styles_entry ? zip_reader.read_entry(styles_entry) : nil
|
|
743
|
+
styles = styles_xml ? Ooxml::StylesParser.parse(styles_xml) : {}
|
|
744
|
+
|
|
745
|
+
workbook_xml = zip_reader.read_entry(workbook_path)
|
|
746
|
+
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
747
|
+
workbook_sheets = wb_parsed[:sheets]
|
|
748
|
+
is_date1904 = wb_parsed[:date1904]
|
|
749
|
+
|
|
750
|
+
ws_idx = 0
|
|
751
|
+
workbook_sheets.each do |sheet_info|
|
|
752
|
+
r_id = sheet_info[:r_id]
|
|
753
|
+
target = rels[r_id]
|
|
754
|
+
rel_type = rel_types[r_id]
|
|
755
|
+
next if non_worksheet_relationship?(rel_type, target)
|
|
756
|
+
|
|
757
|
+
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
758
|
+
current_sheet_idx = ws_idx
|
|
759
|
+
ws_idx += 1
|
|
760
|
+
next unless sheet_path
|
|
761
|
+
|
|
762
|
+
chunk_supplier = ->(&blk) { zip_reader.each_entry_chunk(sheet_path, &blk) }
|
|
763
|
+
|
|
764
|
+
stream_sheet = StreamSheet.new(
|
|
765
|
+
sheet_info[:name],
|
|
766
|
+
chunk_supplier,
|
|
767
|
+
shared_strings,
|
|
768
|
+
styles,
|
|
769
|
+
zip_reader: zip_reader,
|
|
770
|
+
entry_name: sheet_path,
|
|
771
|
+
state: sheet_info[:state] || :visible,
|
|
772
|
+
date1904: is_date1904,
|
|
773
|
+
trim_empty_rows: trim_empty_rows,
|
|
774
|
+
pad_empty_rows: pad_empty_rows,
|
|
775
|
+
pad_empty_cells: pad_empty_cells,
|
|
776
|
+
defined_names: wb_parsed[:defined_names] || [],
|
|
777
|
+
sheet_index: current_sheet_idx
|
|
778
|
+
)
|
|
779
|
+
yield stream_sheet
|
|
814
780
|
end
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
source,
|
|
819
|
-
password: password,
|
|
820
|
-
trim_empty_rows: trim_empty_rows,
|
|
821
|
-
pad_empty_rows: pad_empty_rows,
|
|
822
|
-
pad_empty_cells: pad_empty_cells,
|
|
823
|
-
max_uncompressed_size: max_uncompressed_size
|
|
824
|
-
)
|
|
781
|
+
nil
|
|
782
|
+
ensure
|
|
783
|
+
zip_reader&.close
|
|
825
784
|
end
|
|
826
785
|
end
|
|
827
786
|
|
|
@@ -911,7 +870,7 @@ module Xlsxrb
|
|
|
911
870
|
wb_props = {}
|
|
912
871
|
wb_props[:date1904] = true if is_date1904
|
|
913
872
|
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
914
|
-
wb_props[:
|
|
873
|
+
wb_props[:active_sheet_index] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
915
874
|
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
916
875
|
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
917
876
|
wb_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
@@ -924,8 +883,16 @@ module Xlsxrb
|
|
|
924
883
|
prot_match = workbook_xml.match(%r{<workbookProtection\b([^>]*)/>})
|
|
925
884
|
if prot_match
|
|
926
885
|
prot = {}
|
|
927
|
-
|
|
928
|
-
|
|
886
|
+
prot_match[1].scan(/([A-Za-z]+)="([^"]*)"/) do |attr, value|
|
|
887
|
+
key, kind = WORKBOOK_PROTECTION_ATTRIBUTES[attr]
|
|
888
|
+
next unless key
|
|
889
|
+
|
|
890
|
+
prot[key] = case kind
|
|
891
|
+
when :bool then %w[1 true].include?(value)
|
|
892
|
+
when :int then value.to_i
|
|
893
|
+
else value
|
|
894
|
+
end
|
|
895
|
+
end
|
|
929
896
|
wb_unmapped[:workbook_protection] = prot unless prot.empty?
|
|
930
897
|
end
|
|
931
898
|
end
|
|
@@ -963,130 +930,6 @@ module Xlsxrb
|
|
|
963
930
|
end
|
|
964
931
|
end
|
|
965
932
|
|
|
966
|
-
# Internal reader helper returning a Workbook with StreamSheet instances without deprecation warning.
|
|
967
|
-
# @api private
|
|
968
|
-
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Elements::Workbook
|
|
969
|
-
def self._read_workbook(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil)
|
|
970
|
-
prepared_source = prepare_source_io(source, password)
|
|
971
|
-
zip_options = {}
|
|
972
|
-
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
973
|
-
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
974
|
-
|
|
975
|
-
workbook_path = resolve_workbook_entry(zip_reader)
|
|
976
|
-
wb_dir = File.dirname(workbook_path)
|
|
977
|
-
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
978
|
-
rels_xml = zip_reader.read_entry(wb_rels_name)
|
|
979
|
-
rels, rel_types = parse_workbook_relationships(rels_xml)
|
|
980
|
-
|
|
981
|
-
sst_entry = resolve_part_entry(zip_reader, rels, "sharedStrings", "sharedStrings.xml", wb_dir)
|
|
982
|
-
shared_strings_xml = sst_entry ? zip_reader.read_entry(sst_entry) : nil
|
|
983
|
-
shared_strings = shared_strings_xml ? Ooxml::SharedStringsParser.parse(shared_strings_xml) : []
|
|
984
|
-
|
|
985
|
-
styles_entry = resolve_part_entry(zip_reader, rels, "styles", "styles.xml", wb_dir)
|
|
986
|
-
styles_xml = styles_entry ? zip_reader.read_entry(styles_entry) : nil
|
|
987
|
-
styles = styles_xml ? Ooxml::StylesParser.parse(styles_xml) : {}
|
|
988
|
-
|
|
989
|
-
workbook_xml = zip_reader.read_entry(workbook_path)
|
|
990
|
-
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
991
|
-
workbook_sheets = wb_parsed[:sheets]
|
|
992
|
-
is_date1904 = wb_parsed[:date1904]
|
|
993
|
-
|
|
994
|
-
non_worksheet_sheets = []
|
|
995
|
-
ws_idx = 0
|
|
996
|
-
sheets = workbook_sheets.filter_map do |sheet_info|
|
|
997
|
-
r_id = sheet_info[:r_id]
|
|
998
|
-
target = rels[r_id]
|
|
999
|
-
rel_type = rel_types[r_id]
|
|
1000
|
-
if non_worksheet_relationship?(rel_type, target)
|
|
1001
|
-
non_worksheet_sheets << sheet_info.merge(target: target, type: rel_type)
|
|
1002
|
-
next
|
|
1003
|
-
end
|
|
1004
|
-
|
|
1005
|
-
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
1006
|
-
current_sheet_idx = ws_idx
|
|
1007
|
-
ws_idx += 1
|
|
1008
|
-
next unless sheet_path
|
|
1009
|
-
|
|
1010
|
-
chunk_supplier = ->(&blk) { zip_reader.each_entry_chunk(sheet_path, &blk) }
|
|
1011
|
-
|
|
1012
|
-
StreamSheet.new(
|
|
1013
|
-
sheet_info[:name],
|
|
1014
|
-
chunk_supplier,
|
|
1015
|
-
shared_strings,
|
|
1016
|
-
styles,
|
|
1017
|
-
zip_reader: zip_reader,
|
|
1018
|
-
entry_name: sheet_path,
|
|
1019
|
-
state: sheet_info[:state] || :visible,
|
|
1020
|
-
date1904: is_date1904,
|
|
1021
|
-
trim_empty_rows: trim_empty_rows,
|
|
1022
|
-
pad_empty_rows: pad_empty_rows,
|
|
1023
|
-
pad_empty_cells: pad_empty_cells,
|
|
1024
|
-
defined_names: wb_parsed[:defined_names] || [],
|
|
1025
|
-
sheet_index: current_sheet_idx
|
|
1026
|
-
)
|
|
1027
|
-
end
|
|
1028
|
-
|
|
1029
|
-
wb_unmapped = {}
|
|
1030
|
-
wb_props = {}
|
|
1031
|
-
wb_props[:date1904] = true if is_date1904
|
|
1032
|
-
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1033
|
-
wb_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1034
|
-
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
1035
|
-
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
1036
|
-
wb_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
1037
|
-
wb_props[:full_calc_on_load] = wb_parsed[:full_calc_on_load] unless wb_parsed[:full_calc_on_load].nil?
|
|
1038
|
-
wb_props[:code_name] = wb_parsed[:code_name] if wb_parsed[:code_name]
|
|
1039
|
-
wb_unmapped[:workbook_properties] = wb_props unless wb_props.empty?
|
|
1040
|
-
wb_unmapped[:non_worksheet_sheets] = non_worksheet_sheets unless non_worksheet_sheets.empty?
|
|
1041
|
-
|
|
1042
|
-
if workbook_xml&.include?("<workbookProtection")
|
|
1043
|
-
prot_match = workbook_xml.match(%r{<workbookProtection\b([^>]*)/>})
|
|
1044
|
-
if prot_match
|
|
1045
|
-
prot = {}
|
|
1046
|
-
prot[:lock_structure] = true if prot_match[1].include?('lockStructure="1"') || prot_match[1].include?('lockStructure="true"')
|
|
1047
|
-
prot[:lock_windows] = true if prot_match[1].include?('lockWindows="1"') || prot_match[1].include?('lockWindows="true"')
|
|
1048
|
-
wb_unmapped[:workbook_protection] = prot unless prot.empty?
|
|
1049
|
-
end
|
|
1050
|
-
end
|
|
1051
|
-
|
|
1052
|
-
core_xml = zip_reader.read_entry("docProps/core.xml")
|
|
1053
|
-
if core_xml && !core_xml.empty?
|
|
1054
|
-
c_listener = Ooxml::Reader::CorePropertiesListener.new
|
|
1055
|
-
Ooxml::XmlParser.parse(core_xml, c_listener)
|
|
1056
|
-
wb_unmapped[:core_properties] = c_listener.properties unless c_listener.properties.empty?
|
|
1057
|
-
end
|
|
1058
|
-
|
|
1059
|
-
app_xml = zip_reader.read_entry("docProps/app.xml")
|
|
1060
|
-
if app_xml && !app_xml.empty?
|
|
1061
|
-
a_listener = Ooxml::Reader::AppPropertiesListener.new
|
|
1062
|
-
Ooxml::XmlParser.parse(app_xml, a_listener)
|
|
1063
|
-
wb_unmapped[:app_properties] = a_listener.properties unless a_listener.properties.empty?
|
|
1064
|
-
end
|
|
1065
|
-
|
|
1066
|
-
custom_xml = zip_reader.read_entry("docProps/custom.xml")
|
|
1067
|
-
if custom_xml && !custom_xml.empty?
|
|
1068
|
-
cu_listener = Ooxml::Reader::CustomPropertiesListener.new
|
|
1069
|
-
Ooxml::XmlParser.parse(custom_xml, cu_listener)
|
|
1070
|
-
wb_unmapped[:custom_properties] = cu_listener.properties unless cu_listener.properties.empty?
|
|
1071
|
-
end
|
|
1072
|
-
defined_names = wb_parsed[:defined_names] || []
|
|
1073
|
-
Elements::Workbook.new(
|
|
1074
|
-
sheets: sheets,
|
|
1075
|
-
shared_strings: shared_strings,
|
|
1076
|
-
styles: styles,
|
|
1077
|
-
unmapped_data: wb_unmapped,
|
|
1078
|
-
defined_names: defined_names
|
|
1079
|
-
)
|
|
1080
|
-
end
|
|
1081
|
-
private_class_method :_read_workbook
|
|
1082
|
-
|
|
1083
|
-
# @deprecated Use {.read} instead. Will be removed in 0.2.0.
|
|
1084
|
-
#: (*untyped, **untyped) ?{ (*untyped) -> untyped } -> untyped
|
|
1085
|
-
def self.stream(...)
|
|
1086
|
-
warn "[DEPRECATION] Xlsxrb.stream is deprecated; use Xlsxrb.read instead.", uplevel: 1
|
|
1087
|
-
read(...)
|
|
1088
|
-
end
|
|
1089
|
-
|
|
1090
933
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
1091
934
|
#
|
|
1092
935
|
# @overload write(?target, password: nil, encryption_mode: :standard, strict_excel_mode: true, &block)
|
|
@@ -1138,16 +981,16 @@ module Xlsxrb
|
|
|
1138
981
|
# When set to `false`, strings are emitted directly as inline strings (`<c t="inlineStr">`), providing flat
|
|
1139
982
|
# $O(1)$ constant memory during streaming write. Disabling shared strings is recommended when streaming
|
|
1140
983
|
# large datasets where cells contain unique text (e.g. log exports, transaction details).
|
|
1141
|
-
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION
|
|
1142
|
-
#
|
|
1143
|
-
#
|
|
1144
|
-
#: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol, ?strict_excel_mode: bool, ?
|
|
1145
|
-
def self.write(target_or_workbook = OMITTED_TARGET, workbook_or_nil = nil, password: nil, encryption_mode: :standard, strict_excel_mode: true,
|
|
984
|
+
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION
|
|
985
|
+
# for zlib's level 6). Defaults to {DEFAULT_COMPRESSION_LEVEL}. Higher levels write smaller files more slowly;
|
|
986
|
+
# level 0 stores entries without compression. See docs/adr/0009-default-zip-compression-level.md.
|
|
987
|
+
#: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol, ?strict_excel_mode: bool, ?default_font: untyped?, ?shared_strings: bool, ?compression_level: Integer | nil) ?{ (StreamWriter) -> void } -> untyped
|
|
988
|
+
def self.write(target_or_workbook = OMITTED_TARGET, workbook_or_nil = nil, password: nil, encryption_mode: :standard, strict_excel_mode: true, default_font: nil, shared_strings: true, compression_level: DEFAULT_COMPRESSION_LEVEL, &)
|
|
1146
989
|
if block_given?
|
|
1147
990
|
if target_or_workbook.equal?(OMITTED_TARGET)
|
|
1148
991
|
io = StringIO.new
|
|
1149
992
|
io.binmode
|
|
1150
|
-
write(io, password: password, encryption_mode: encryption_mode, strict_excel_mode: strict_excel_mode,
|
|
993
|
+
write(io, password: password, encryption_mode: encryption_mode, strict_excel_mode: strict_excel_mode, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level, &)
|
|
1151
994
|
return io.string.b
|
|
1152
995
|
end
|
|
1153
996
|
|
|
@@ -1157,7 +1000,7 @@ module Xlsxrb
|
|
|
1157
1000
|
if password && !password.empty?
|
|
1158
1001
|
buf = StringIO.new
|
|
1159
1002
|
buf.binmode
|
|
1160
|
-
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode,
|
|
1003
|
+
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
1161
1004
|
begin
|
|
1162
1005
|
yield stream_writer
|
|
1163
1006
|
stream_writer.close
|
|
@@ -1172,7 +1015,7 @@ module Xlsxrb
|
|
|
1172
1015
|
stream_writer.cleanup!
|
|
1173
1016
|
end
|
|
1174
1017
|
else
|
|
1175
|
-
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode,
|
|
1018
|
+
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
1176
1019
|
begin
|
|
1177
1020
|
yield stream_writer
|
|
1178
1021
|
stream_writer.close
|
|
@@ -1237,7 +1080,7 @@ module Xlsxrb
|
|
|
1237
1080
|
style_idx = col.unmapped_data&.[](:style_index) || col.unmapped_data&.[](:style) || (col.respond_to?(:style_index) && col.style_index)
|
|
1238
1081
|
min = col.unmapped_data&.[](:min)
|
|
1239
1082
|
max = col.unmapped_data&.[](:max)
|
|
1240
|
-
{ index: col.index, width: col.width, hidden: col.hidden
|
|
1083
|
+
{ index: col.index, width: col.width, hidden: col.hidden?, custom_width: col.custom_width?, outline_level: col.outline_level, style_index: style_idx, min: min, max: max }
|
|
1241
1084
|
# simplecov:enable
|
|
1242
1085
|
end
|
|
1243
1086
|
sd = { name: ws.name, rows: ws.rows, columns: columns }
|
|
@@ -1273,7 +1116,13 @@ module Xlsxrb
|
|
|
1273
1116
|
sd[:freeze_pane] = ws.freeze_pane.respond_to?(:to_h) ? ws.freeze_pane.to_h : ws.freeze_pane if ws.respond_to?(:freeze_pane) && ws.freeze_pane
|
|
1274
1117
|
sd[:split_pane] = ws.unmapped_data.dig(:facade, :split_pane) || (ws.split_pane.respond_to?(:to_h) ? ws.split_pane.to_h : ws.split_pane) if ws.respond_to?(:split_pane) && ws.split_pane
|
|
1275
1118
|
sd[:sheet_view] = ws.sheet_view.respond_to?(:to_h) ? ws.sheet_view.to_h : ws.sheet_view if ws.respond_to?(:sheet_view) && ws.sheet_view && !ws.sheet_view.empty?
|
|
1276
|
-
|
|
1119
|
+
if ws.respond_to?(:selection) && ws.selection
|
|
1120
|
+
sd[:selection] = if ws.selection.is_a?(Array)
|
|
1121
|
+
ws.selection.map { |sel| sel.respond_to?(:to_h) ? sel.to_h : sel }
|
|
1122
|
+
else
|
|
1123
|
+
ws.selection.respond_to?(:to_h) ? ws.selection.to_h : ws.selection
|
|
1124
|
+
end
|
|
1125
|
+
end
|
|
1277
1126
|
sd[:selection] ||= ws.sheet_view[:selection] if ws.respond_to?(:sheet_view) && ws.sheet_view.is_a?(Hash) && ws.sheet_view[:selection]
|
|
1278
1127
|
sd[:page_setup] = ws.page_setup.respond_to?(:to_h) ? ws.page_setup.to_h : ws.page_setup if ws.respond_to?(:page_setup) && ws.page_setup && !ws.page_setup.empty?
|
|
1279
1128
|
sd[:page_margins] = ws.page_margins.respond_to?(:to_h) ? ws.page_margins.to_h : ws.page_margins if ws.respond_to?(:page_margins) && ws.page_margins && !ws.page_margins.empty?
|
|
@@ -1283,7 +1132,7 @@ module Xlsxrb
|
|
|
1283
1132
|
sd[:tables] = ws.tables.map { |t| t.respond_to?(:to_h) ? t.to_h : t } if ws.respond_to?(:tables) && ws.tables && !ws.tables.empty?
|
|
1284
1133
|
sd[:merge_cells] = ws.merged_cells if ws.respond_to?(:merged_cells) && ws.merged_cells && !ws.merged_cells.empty?
|
|
1285
1134
|
sd[:auto_filter] = ws.auto_filter if ws.respond_to?(:auto_filter) && ws.auto_filter
|
|
1286
|
-
sd[:filter_columns] = ws.unmapped_data.dig(:facade, :filter_columns) || (ws.filter_columns.transform_values { |fc| fc.respond_to?(:
|
|
1135
|
+
sd[:filter_columns] = ws.unmapped_data.dig(:facade, :filter_columns) || (ws.filter_columns.transform_values { |fc| fc.respond_to?(:to_spec) ? fc.to_spec : fc } if ws.respond_to?(:filter_columns) && ws.filter_columns && !ws.filter_columns.empty?)
|
|
1287
1136
|
sd[:sort_state] = ws.unmapped_data.dig(:facade, :sort_state) || (ws.sort_state.respond_to?(:to_h) ? ws.sort_state.to_h : ws.sort_state) if ws.respond_to?(:sort_state) && ws.sort_state
|
|
1288
1137
|
sd[:pivot_tables] = ws.pivot_tables.map { |pt| pt.respond_to?(:to_h) ? pt.to_h : pt } if ws.respond_to?(:pivot_tables) && ws.pivot_tables && !ws.pivot_tables.empty?
|
|
1289
1138
|
sd[:shapes] = ws.shapes.map { |s| s.respond_to?(:to_h) ? s.to_h : s } if ws.respond_to?(:shapes) && ws.shapes && !ws.shapes.empty?
|
|
@@ -1294,8 +1143,6 @@ module Xlsxrb
|
|
|
1294
1143
|
end
|
|
1295
1144
|
if ws.respond_to?(:column_breaks) && ws.column_breaks && !ws.column_breaks.empty?
|
|
1296
1145
|
sd[:column_breaks] = ws.column_breaks.map { |c| c.respond_to?(:to_h) ? c.to_h : c }
|
|
1297
|
-
elsif ws.respond_to?(:col_breaks) && ws.col_breaks && !ws.col_breaks.empty?
|
|
1298
|
-
sd[:column_breaks] = ws.col_breaks.map { |c| c.respond_to?(:to_h) ? c.to_h : c }
|
|
1299
1146
|
end
|
|
1300
1147
|
sd[:background_image] = ws.background_image if ws.respond_to?(:background_image) && ws.background_image
|
|
1301
1148
|
if ws.respond_to?(:images) && ws.images && !ws.images.empty?
|
|
@@ -1307,10 +1154,8 @@ module Xlsxrb
|
|
|
1307
1154
|
opts[:file_data] = img.data
|
|
1308
1155
|
opts[:ext] = ext
|
|
1309
1156
|
opts[:from_column] = img.from_column
|
|
1310
|
-
opts[:from_col] = img.from_column
|
|
1311
1157
|
opts[:from_row] = img.from_row
|
|
1312
1158
|
opts[:to_column] = img.to_column
|
|
1313
|
-
opts[:to_col] = img.to_column
|
|
1314
1159
|
opts[:to_row] = img.to_row
|
|
1315
1160
|
opts
|
|
1316
1161
|
else
|
|
@@ -1329,13 +1174,19 @@ module Xlsxrb
|
|
|
1329
1174
|
|
|
1330
1175
|
# Extract workbook-level facade metadata
|
|
1331
1176
|
wb_facade = workbook.unmapped_data[:facade] || {}
|
|
1332
|
-
wb_properties = workbook.unmapped_data[:workbook_properties] || wb_facade[:workbook_properties]
|
|
1177
|
+
wb_properties = (workbook.unmapped_data[:workbook_properties] || wb_facade[:workbook_properties] || {}).merge(
|
|
1178
|
+
workbook.respond_to?(:workbook_properties) && workbook.workbook_properties ? workbook.workbook_properties.to_h : {}
|
|
1179
|
+
)
|
|
1333
1180
|
wb_core = (workbook.respond_to?(:core_properties) && workbook.core_properties) || wb_facade[:core_properties]
|
|
1334
1181
|
wb_app = (workbook.respond_to?(:app_properties) && workbook.app_properties) || wb_facade[:app_properties]
|
|
1335
1182
|
wb_custom = (workbook.respond_to?(:custom_properties) && workbook.custom_properties) || wb_facade[:custom_properties]
|
|
1336
1183
|
wb_prot = (workbook.respond_to?(:workbook_protection) && workbook.workbook_protection) || wb_facade[:workbook_protection]
|
|
1337
1184
|
raw_dns = wb_facade[:defined_names] || (workbook.respond_to?(:defined_names) && !workbook.defined_names.empty? ? workbook.defined_names : nil)
|
|
1338
|
-
defined_names =
|
|
1185
|
+
defined_names = if raw_dns
|
|
1186
|
+
raw_dns.map { |dn| dn.is_a?(Elements::MetadataRecord) ? dn.to_h : dn.dup }
|
|
1187
|
+
else
|
|
1188
|
+
[]
|
|
1189
|
+
end
|
|
1339
1190
|
|
|
1340
1191
|
# Auto-generate _xlnm.Print_Area and _xlnm.Print_Titles from worksheet definitions if not already present
|
|
1341
1192
|
workbook.sheets.each_with_index do |ws, idx|
|
|
@@ -1349,7 +1200,7 @@ module Xlsxrb
|
|
|
1349
1200
|
pt = ws.print_titles
|
|
1350
1201
|
pt = pt.to_h if pt.is_a?(Elements::MetadataRecord)
|
|
1351
1202
|
parts = []
|
|
1352
|
-
col_val = pt[:columns]
|
|
1203
|
+
col_val = pt[:columns]
|
|
1353
1204
|
parts << "'#{sheet_name}'!$#{col_val.sub(":", ":$")}" if col_val
|
|
1354
1205
|
parts << "'#{sheet_name}'!$#{pt[:rows].sub(":", ":$")}" if pt[:rows]
|
|
1355
1206
|
defined_names << { name: "_xlnm.Print_Titles", value: parts.join(","), local_sheet_id: idx } unless parts.empty?
|
|
@@ -1397,288 +1248,6 @@ module Xlsxrb
|
|
|
1397
1248
|
end
|
|
1398
1249
|
end
|
|
1399
1250
|
|
|
1400
|
-
# Serializes an in-memory workbook to a destination target (path or IO).
|
|
1401
|
-
#
|
|
1402
|
-
# @deprecated Use {.write} instead. Will be removed in 0.2.0.
|
|
1403
|
-
# @param target [String, IO, StringIO] Target destination.
|
|
1404
|
-
# @param workbook [Elements::Workbook] The workbook instance to write.
|
|
1405
|
-
# @param shared_strings [Boolean] Whether to intern strings into sharedStrings.xml.
|
|
1406
|
-
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9).
|
|
1407
|
-
# @param password [String, nil] Optional encryption password.
|
|
1408
|
-
# @param encryption_mode [Symbol] Encryption algorithm (:standard or :agile).
|
|
1409
|
-
# @return [void]
|
|
1410
|
-
# @api public
|
|
1411
|
-
#: (String | IO | StringIO target, Elements::Workbook workbook, ?shared_strings: bool, ?compression_level: Integer?, ?password: String?, ?encryption_mode: Symbol) -> void
|
|
1412
|
-
def self.write_workbook(target, workbook, shared_strings: true, compression_level: nil, password: nil, encryption_mode: :standard)
|
|
1413
|
-
warn "[Xlsxrb DEPRECATION] Xlsxrb.write_workbook is deprecated and will be removed in v0.2.0. Use Xlsxrb.write instead.", uplevel: 1
|
|
1414
|
-
write(target, workbook, password: password, encryption_mode: encryption_mode, shared_strings: shared_strings, compression_level: compression_level || Zlib::DEFAULT_COMPRESSION)
|
|
1415
|
-
end
|
|
1416
|
-
|
|
1417
|
-
# Serializes an in-memory workbook to a binary XLSX data string.
|
|
1418
|
-
#
|
|
1419
|
-
# @deprecated Use {.write} instead. Will be removed in 0.2.0.
|
|
1420
|
-
# @param workbook [Elements::Workbook] The workbook instance to serialize.
|
|
1421
|
-
# @param shared_strings [Boolean] Whether to intern strings into sharedStrings.xml.
|
|
1422
|
-
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9).
|
|
1423
|
-
# @param password [String, nil] Optional encryption password.
|
|
1424
|
-
# @param encryption_mode [Symbol] Encryption algorithm (:standard or :agile).
|
|
1425
|
-
# @return [String] Binary XLSX data.
|
|
1426
|
-
# @api public
|
|
1427
|
-
#: (Elements::Workbook workbook, ?shared_strings: bool, ?compression_level: Integer?, ?password: String?, ?encryption_mode: Symbol) -> String
|
|
1428
|
-
def self.workbook_to_data(workbook, shared_strings: true, compression_level: nil, password: nil, encryption_mode: :standard)
|
|
1429
|
-
warn "[Xlsxrb DEPRECATION] Xlsxrb.workbook_to_data is deprecated and will be removed in v0.2.0. Use Xlsxrb.write instead.", uplevel: 1
|
|
1430
|
-
write(workbook, password: password, encryption_mode: encryption_mode, shared_strings: shared_strings, compression_level: compression_level || Zlib::DEFAULT_COMPRESSION)
|
|
1431
|
-
end
|
|
1432
|
-
|
|
1433
|
-
# Modifies an existing XLSX file in-memory using an immutable transformation block.
|
|
1434
|
-
#
|
|
1435
|
-
# Reads the workbook, passes it to the block, and writes the resulting workbook.
|
|
1436
|
-
# If no target is specified, the source file is overwritten in-place.
|
|
1437
|
-
#
|
|
1438
|
-
# @example Modify a template and save to new file
|
|
1439
|
-
# Xlsxrb.modify("template.xlsx", "output.xlsx") do |workbook|
|
|
1440
|
-
# workbook.update_sheet("Sheet1") do |sheet|
|
|
1441
|
-
# sheet.update_cell("B1", value: "Updated Title")
|
|
1442
|
-
# .update_cell("B2", value: 100)
|
|
1443
|
-
# end
|
|
1444
|
-
# end
|
|
1445
|
-
#
|
|
1446
|
-
# @param source [String, IO, StringIO] The source file path or IO object.
|
|
1447
|
-
# @param target [String, IO, StringIO, nil] Target file path or IO object (overwrites source if nil).
|
|
1448
|
-
# @param password [String, nil] Optional password for decrypting encrypted source files.
|
|
1449
|
-
# @deprecated Use {Xlsxrb.read} + {Xlsxrb.write} for streaming transformations or {Xlsxrb.write} DSL for report generation. Will be removed in 0.2.0.
|
|
1450
|
-
# @param new_password [String, nil] Optional password for encrypting the output file (defaults to password).
|
|
1451
|
-
# @yield [workbook]
|
|
1452
|
-
# @yieldparam workbook [Elements::Workbook] The loaded workbook.
|
|
1453
|
-
# @yieldreturn [Elements::Workbook] The modified workbook.
|
|
1454
|
-
# @return [Elements::Workbook] The modified workbook returned by the block.
|
|
1455
|
-
# @api public
|
|
1456
|
-
#: (untyped source, ?untyped target, ?password: String?, ?new_password: String?, **untyped opts) ?{ (Elements::Workbook) -> untyped } -> untyped
|
|
1457
|
-
def self.modify(source, target = nil, password: nil, new_password: nil, **opts)
|
|
1458
|
-
warn "[DEPRECATION] Xlsxrb.modify is deprecated and will be removed in 0.2.0; use Xlsxrb.read + Xlsxrb.write for streaming transformations or Xlsxrb.write DSL for report generation.", uplevel: 1
|
|
1459
|
-
raise Error, "source is required" if source.nil?
|
|
1460
|
-
raise Error, "block is required" unless block_given?
|
|
1461
|
-
|
|
1462
|
-
prepared_source = prepare_source_io(source, password)
|
|
1463
|
-
in_place = target.nil? || target == source || (source.is_a?(String) && target.is_a?(String) && File.file?(target) && File.identical?(source, target))
|
|
1464
|
-
temp_target_path = nil
|
|
1465
|
-
|
|
1466
|
-
begin
|
|
1467
|
-
write_dest = if in_place && source.is_a?(String)
|
|
1468
|
-
temp_target = Tempfile.new(["xlsxrb_mod", ".xlsx"], File.dirname(source))
|
|
1469
|
-
temp_target_path = temp_target.path
|
|
1470
|
-
temp_target.close
|
|
1471
|
-
temp_target_path
|
|
1472
|
-
elsif in_place && source.is_a?(StringIO)
|
|
1473
|
-
StringIO.new
|
|
1474
|
-
else
|
|
1475
|
-
target || source
|
|
1476
|
-
end
|
|
1477
|
-
|
|
1478
|
-
replaced_entries = {}
|
|
1479
|
-
structural_change = false
|
|
1480
|
-
block_result = nil
|
|
1481
|
-
|
|
1482
|
-
zip_options = {}
|
|
1483
|
-
zip_options[:max_uncompressed_size] = opts[:max_uncompressed_size] if opts[:max_uncompressed_size]
|
|
1484
|
-
|
|
1485
|
-
Ooxml::ZipReader.open(prepared_source, **zip_options) do |zip_reader|
|
|
1486
|
-
workbook_path = resolve_workbook_entry(zip_reader)
|
|
1487
|
-
wb_dir = File.dirname(workbook_path)
|
|
1488
|
-
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
1489
|
-
rels_xml = zip_reader.read_entry(wb_rels_name)
|
|
1490
|
-
rels, rel_types = parse_workbook_relationships(rels_xml)
|
|
1491
|
-
|
|
1492
|
-
sst_entry = resolve_part_entry(zip_reader, rels, "sharedStrings", "sharedStrings.xml", wb_dir)
|
|
1493
|
-
shared_strings_xml = sst_entry ? zip_reader.read_entry(sst_entry) : nil
|
|
1494
|
-
shared_strings = shared_strings_xml ? Ooxml::SharedStringsParser.parse(shared_strings_xml) : []
|
|
1495
|
-
sst_index = {}
|
|
1496
|
-
shared_strings.each_with_index { |s, idx| sst_index[s] ||= idx }
|
|
1497
|
-
initial_sst_size = shared_strings.size
|
|
1498
|
-
|
|
1499
|
-
styles_entry = resolve_part_entry(zip_reader, rels, "styles", "styles.xml", wb_dir)
|
|
1500
|
-
styles_xml = styles_entry ? zip_reader.read_entry(styles_entry) : nil
|
|
1501
|
-
styles = styles_xml ? Ooxml::StylesParser.parse(styles_xml) : {}
|
|
1502
|
-
|
|
1503
|
-
workbook_xml = zip_reader.read_entry(workbook_path)
|
|
1504
|
-
wb_parsed = workbook_xml ? Ooxml::WorkbookParser.parse_with_properties(workbook_xml) : { sheets: [], date1904: false, defined_names: [] }
|
|
1505
|
-
workbook_sheets = wb_parsed[:sheets]
|
|
1506
|
-
|
|
1507
|
-
sheet_entry_map = {}
|
|
1508
|
-
ws_idx = 0
|
|
1509
|
-
loaded_sheets = workbook_sheets.filter_map do |sheet_info|
|
|
1510
|
-
r_id = sheet_info[:r_id]
|
|
1511
|
-
target_path = rels[r_id]
|
|
1512
|
-
next if non_worksheet_relationship?(rel_types[r_id], target_path)
|
|
1513
|
-
|
|
1514
|
-
sheet_path = resolve_sheet_entry(zip_reader, target_path, wb_dir, ws_idx)
|
|
1515
|
-
current_ws_idx = ws_idx
|
|
1516
|
-
ws_idx += 1
|
|
1517
|
-
next unless sheet_path
|
|
1518
|
-
|
|
1519
|
-
sheet_entry_map[sheet_info[:name]] = sheet_path
|
|
1520
|
-
sheet_xml = zip_reader.read_entry(sheet_path) || ""
|
|
1521
|
-
sheet_print_area = extract_sheet_print_area(wb_parsed[:defined_names], sheet_info[:name], current_ws_idx)
|
|
1522
|
-
sheet_print_titles = extract_sheet_print_titles(wb_parsed[:defined_names], sheet_info[:name], current_ws_idx)
|
|
1523
|
-
build_worksheet(
|
|
1524
|
-
sheet_info[:name],
|
|
1525
|
-
sheet_xml,
|
|
1526
|
-
shared_strings,
|
|
1527
|
-
styles,
|
|
1528
|
-
state: sheet_info[:state] || :visible,
|
|
1529
|
-
zip_reader: zip_reader,
|
|
1530
|
-
entry_name: sheet_path,
|
|
1531
|
-
print_area: sheet_print_area,
|
|
1532
|
-
print_titles: sheet_print_titles,
|
|
1533
|
-
trim_empty_rows: opts.fetch(:trim_empty_rows, false),
|
|
1534
|
-
pad_empty_rows: opts.fetch(:pad_empty_rows, false),
|
|
1535
|
-
pad_empty_cells: opts.fetch(:pad_empty_cells, false)
|
|
1536
|
-
)
|
|
1537
|
-
end
|
|
1538
|
-
|
|
1539
|
-
mod_unmapped = {}
|
|
1540
|
-
mod_props = {}
|
|
1541
|
-
mod_props[:date1904] = true if wb_parsed[:date1904]
|
|
1542
|
-
mod_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1543
|
-
mod_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
1544
|
-
mod_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
1545
|
-
mod_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
1546
|
-
mod_props[:full_calc_on_load] = wb_parsed[:full_calc_on_load] unless wb_parsed[:full_calc_on_load].nil?
|
|
1547
|
-
mod_unmapped[:workbook_properties] = mod_props unless mod_props.empty?
|
|
1548
|
-
|
|
1549
|
-
original_workbook = Elements::Workbook.new(
|
|
1550
|
-
sheets: loaded_sheets,
|
|
1551
|
-
shared_strings: shared_strings,
|
|
1552
|
-
styles: styles,
|
|
1553
|
-
unmapped_data: mod_unmapped,
|
|
1554
|
-
defined_names: wb_parsed[:defined_names] || []
|
|
1555
|
-
)
|
|
1556
|
-
mutable_shared_strings = shared_strings.dup
|
|
1557
|
-
|
|
1558
|
-
block_result = yield original_workbook
|
|
1559
|
-
if block_result.is_a?(Elements::Workbook)
|
|
1560
|
-
result_workbook = block_result
|
|
1561
|
-
else
|
|
1562
|
-
warn "[DEPRECATION] Xlsxrb.modify block must return an Elements::Workbook; returning other values is deprecated and will raise TypeError in version 0.2.0.", uplevel: 1
|
|
1563
|
-
result_workbook = original_workbook
|
|
1564
|
-
end
|
|
1565
|
-
|
|
1566
|
-
write_password = new_password.nil? ? password : new_password
|
|
1567
|
-
|
|
1568
|
-
structural_change = result_workbook.sheets.size != original_workbook.sheets.size ||
|
|
1569
|
-
result_workbook.sheets.map(&:name) != original_workbook.sheets.map(&:name)
|
|
1570
|
-
|
|
1571
|
-
if structural_change
|
|
1572
|
-
write_target = target || source
|
|
1573
|
-
write_opts = opts.slice(:encryption_mode, :compression_level, :shared_strings)
|
|
1574
|
-
write(write_target, result_workbook, password: write_password, **write_opts)
|
|
1575
|
-
else
|
|
1576
|
-
result_workbook.sheets.each_with_index do |new_sheet, idx|
|
|
1577
|
-
orig_sheet = original_workbook.sheets[idx]
|
|
1578
|
-
next if new_sheet.equal?(orig_sheet) || new_sheet == orig_sheet
|
|
1579
|
-
|
|
1580
|
-
entry_name = sheet_entry_map[new_sheet.name] || "xl/worksheets/sheet#{idx + 1}.xml"
|
|
1581
|
-
orig_sheet_xml = zip_reader.read_entry(entry_name) || ""
|
|
1582
|
-
|
|
1583
|
-
new_sheet_data = serialize_sheet_data_xml(
|
|
1584
|
-
new_sheet.rows,
|
|
1585
|
-
mutable_shared_strings,
|
|
1586
|
-
sst_index,
|
|
1587
|
-
use_sst: !shared_strings_xml.nil?
|
|
1588
|
-
)
|
|
1589
|
-
updated_sheet_xml = replace_sheet_data(orig_sheet_xml, new_sheet_data)
|
|
1590
|
-
updated_sheet_xml = update_dimension(updated_sheet_xml, new_sheet.rows)
|
|
1591
|
-
|
|
1592
|
-
all_hyperlinks = new_sheet.hyperlinks.dup
|
|
1593
|
-
new_sheet.rows.each do |row|
|
|
1594
|
-
row.cells.each do |cell|
|
|
1595
|
-
all_hyperlinks[cell.ref] ||= cell.hyperlink if cell.hyperlink
|
|
1596
|
-
end
|
|
1597
|
-
end
|
|
1598
|
-
|
|
1599
|
-
if all_hyperlinks != orig_sheet.hyperlinks
|
|
1600
|
-
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
1601
|
-
orig_rels_xml = replaced_entries[rels_path] || zip_reader.read_entry(rels_path)
|
|
1602
|
-
updated_sheet_xml, updated_rels_xml = update_sheet_hyperlinks(
|
|
1603
|
-
updated_sheet_xml,
|
|
1604
|
-
orig_rels_xml,
|
|
1605
|
-
all_hyperlinks
|
|
1606
|
-
)
|
|
1607
|
-
replaced_entries[rels_path] = updated_rels_xml if updated_rels_xml
|
|
1608
|
-
end
|
|
1609
|
-
|
|
1610
|
-
all_comments = new_sheet.comments.dup
|
|
1611
|
-
new_sheet.rows.each do |row|
|
|
1612
|
-
row.cells.each do |cell|
|
|
1613
|
-
if cell.comment && all_comments.none? { |c| (c[:ref] || c[:cell])&.to_s&.upcase == cell.ref.upcase }
|
|
1614
|
-
entry = cell.comment.is_a?(Hash) ? cell.comment.merge(ref: cell.ref) : { ref: cell.ref, text: cell.comment }
|
|
1615
|
-
all_comments << entry
|
|
1616
|
-
end
|
|
1617
|
-
end
|
|
1618
|
-
end
|
|
1619
|
-
|
|
1620
|
-
if all_comments != orig_sheet.comments
|
|
1621
|
-
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
1622
|
-
orig_rels_xml = replaced_entries[rels_path] || zip_reader.read_entry(rels_path)
|
|
1623
|
-
updated_sheet_xml, updated_rels_xml = update_sheet_comments(
|
|
1624
|
-
updated_sheet_xml,
|
|
1625
|
-
rels_path,
|
|
1626
|
-
orig_rels_xml,
|
|
1627
|
-
all_comments,
|
|
1628
|
-
zip_reader,
|
|
1629
|
-
idx,
|
|
1630
|
-
entry_name,
|
|
1631
|
-
replaced_entries
|
|
1632
|
-
)
|
|
1633
|
-
replaced_entries[rels_path] = updated_rels_xml if updated_rels_xml
|
|
1634
|
-
end
|
|
1635
|
-
|
|
1636
|
-
replaced_entries[entry_name] = updated_sheet_xml
|
|
1637
|
-
end
|
|
1638
|
-
|
|
1639
|
-
if mutable_shared_strings.size > initial_sst_size && shared_strings_xml
|
|
1640
|
-
new_strings = mutable_shared_strings[initial_sst_size..]
|
|
1641
|
-
sst_path = sst_entry || (wb_dir == "." ? "sharedStrings.xml" : "#{wb_dir}/sharedStrings.xml")
|
|
1642
|
-
replaced_entries[sst_path] = append_to_shared_strings_xml(
|
|
1643
|
-
shared_strings_xml,
|
|
1644
|
-
new_strings
|
|
1645
|
-
)
|
|
1646
|
-
end
|
|
1647
|
-
|
|
1648
|
-
perform_modify_write(
|
|
1649
|
-
write_dest,
|
|
1650
|
-
zip_reader,
|
|
1651
|
-
replaced_entries,
|
|
1652
|
-
password: write_password
|
|
1653
|
-
)
|
|
1654
|
-
end
|
|
1655
|
-
end
|
|
1656
|
-
|
|
1657
|
-
if in_place && !structural_change
|
|
1658
|
-
if source.is_a?(String) && temp_target_path
|
|
1659
|
-
FileUtils.mv(temp_target_path, source)
|
|
1660
|
-
temp_target_path = nil
|
|
1661
|
-
elsif source.is_a?(StringIO) && write_dest.is_a?(StringIO)
|
|
1662
|
-
source.string = write_dest.string
|
|
1663
|
-
elsif source.respond_to?(:write) && source.respond_to?(:seek) && write_dest.is_a?(StringIO)
|
|
1664
|
-
source.seek(0)
|
|
1665
|
-
source.write(write_dest.string)
|
|
1666
|
-
source.truncate(source.pos) if source.respond_to?(:truncate)
|
|
1667
|
-
source.rewind
|
|
1668
|
-
end
|
|
1669
|
-
end
|
|
1670
|
-
block_result
|
|
1671
|
-
ensure
|
|
1672
|
-
if temp_target_path && File.exist?(temp_target_path)
|
|
1673
|
-
begin
|
|
1674
|
-
File.unlink(temp_target_path)
|
|
1675
|
-
rescue SystemCallError
|
|
1676
|
-
nil
|
|
1677
|
-
end
|
|
1678
|
-
end
|
|
1679
|
-
end
|
|
1680
|
-
end
|
|
1681
|
-
|
|
1682
1251
|
# Builds an in-memory {Elements::Workbook} using a declarative DSL.
|
|
1683
1252
|
#
|
|
1684
1253
|
# @example Build an in-memory workbook
|
|
@@ -1690,17 +1259,16 @@ module Xlsxrb
|
|
|
1690
1259
|
# end
|
|
1691
1260
|
#
|
|
1692
1261
|
# @param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specification limits.
|
|
1693
|
-
# @param escape_formulas [Boolean] Whether to treat strings starting with formula prefixes as plain text.
|
|
1694
1262
|
# @param default_font [Hash, nil] Optional default font options.
|
|
1695
1263
|
# @yield [builder]
|
|
1696
1264
|
# @yieldparam builder [Xlsxrb::WorkbookBuilder]
|
|
1697
1265
|
# @return [Elements::Workbook] The compiled in-memory workbook.
|
|
1698
1266
|
# @api public
|
|
1699
|
-
#: (?strict_excel_mode: bool, ?
|
|
1700
|
-
def self.build(strict_excel_mode: true,
|
|
1267
|
+
#: (?strict_excel_mode: bool, ?default_font: untyped?) ?{ (WorkbookBuilder) -> void } -> Elements::Workbook
|
|
1268
|
+
def self.build(strict_excel_mode: true, default_font: nil)
|
|
1701
1269
|
raise Error, "block is required" unless block_given?
|
|
1702
1270
|
|
|
1703
|
-
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode,
|
|
1271
|
+
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode, default_font: default_font)
|
|
1704
1272
|
yield builder
|
|
1705
1273
|
builder.build
|
|
1706
1274
|
end
|
|
@@ -1721,7 +1289,8 @@ module Xlsxrb
|
|
|
1721
1289
|
return "xl/workbook.xml" if zip_reader.entry?("xl/workbook.xml")
|
|
1722
1290
|
return "workbook.xml" if zip_reader.entry?("workbook.xml")
|
|
1723
1291
|
|
|
1724
|
-
|
|
1292
|
+
# A ZIP archive without a workbook part, such as an OpenDocument spreadsheet (.ods) renamed to .xlsx.
|
|
1293
|
+
raise ParseError, "not an XLSX workbook: the ZIP archive has no workbook part (xl/workbook.xml)"
|
|
1725
1294
|
end
|
|
1726
1295
|
|
|
1727
1296
|
def parse_workbook_relationships(rels_xml)
|
|
@@ -1786,10 +1355,9 @@ module Xlsxrb
|
|
|
1786
1355
|
fallbacks.find { |f| zip_reader.entry?(f) }
|
|
1787
1356
|
end
|
|
1788
1357
|
|
|
1789
|
-
#: (String name, String? sheet_xml, Array[String] shared_strings, untyped styles, ?state: Symbol, ?zip_reader: Ooxml::ZipReader?, ?entry_name: String?, ?hyperlinks: Hash[String, untyped]?, ?comments: Array[Hash[Symbol, untyped]]?, ?images: Array[Elements::Image]?, ?date1904: bool, ?dimension: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?merged_cells: Array[String]?, ?auto_filter: String?, ?tables: Array[Hash[Symbol, untyped]]?, ?shapes: Array[Hash[Symbol, untyped]]?, ?freeze_pane: Hash[Symbol, untyped]?, ?page_setup: Hash[Symbol, untyped]?, ?page_margins: Hash[Symbol, untyped]?, ?header_footer: Hash[Symbol, untyped]?, ?print_options: Hash[Symbol, untyped]?, ?sheet_protection: Hash[Symbol, untyped]?, ?sheet_view: Hash[Symbol, untyped]?, ?sheet_properties: Hash[Symbol, untyped]?, ?sheet_format: Hash[Symbol, untyped]?, ?row_breaks: Array[untyped]?, ?column_breaks: Array[untyped]?, ?col_breaks: Array[untyped]?, ?protected_ranges: Array[Hash[Symbol, untyped]]?, ?cell_watches: Array[String]?, ?ignored_errors: Array[Hash[Symbol, untyped]]?, ?phonetic_properties: Hash[Symbol, untyped]?, ?selection: (Hash[Symbol, untyped] | Array[Hash[Symbol, untyped]])?, ?sheet_calc_properties: Hash[Symbol, untyped]?, ?
|
|
1790
|
-
def build_worksheet(name, sheet_xml, shared_strings, styles, state: :visible, zip_reader: nil, entry_name: nil, hyperlinks: nil, comments: nil, images: nil, date1904: false, dimension: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, merged_cells: nil, auto_filter: nil, tables: nil, shapes: nil, freeze_pane: nil, page_setup: nil, page_margins: nil, header_footer: nil, print_options: nil, sheet_protection: nil, sheet_view: nil, sheet_properties: nil, sheet_format: nil, row_breaks: nil, column_breaks: nil,
|
|
1358
|
+
#: (String name, String? sheet_xml, Array[String] shared_strings, untyped styles, ?state: Symbol, ?zip_reader: Ooxml::ZipReader?, ?entry_name: String?, ?hyperlinks: Hash[String, untyped]?, ?comments: Array[Hash[Symbol, untyped]]?, ?images: Array[Elements::Image]?, ?date1904: bool, ?dimension: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?merged_cells: Array[String]?, ?auto_filter: String?, ?tables: Array[Hash[Symbol, untyped]]?, ?shapes: Array[Hash[Symbol, untyped]]?, ?freeze_pane: Hash[Symbol, untyped]?, ?page_setup: Hash[Symbol, untyped]?, ?page_margins: Hash[Symbol, untyped]?, ?header_footer: Hash[Symbol, untyped]?, ?print_options: Hash[Symbol, untyped]?, ?sheet_protection: Hash[Symbol, untyped]?, ?sheet_view: Hash[Symbol, untyped]?, ?sheet_properties: Hash[Symbol, untyped]?, ?sheet_format: Hash[Symbol, untyped]?, ?row_breaks: Array[untyped]?, ?column_breaks: Array[untyped]?, ?col_breaks: Array[untyped]?, ?protected_ranges: Array[Hash[Symbol, untyped]]?, ?cell_watches: Array[String]?, ?ignored_errors: Array[Hash[Symbol, untyped]]?, ?phonetic_properties: Hash[Symbol, untyped]?, ?selection: (Hash[Symbol, untyped] | Array[Hash[Symbol, untyped]])?, ?sheet_calc_properties: Hash[Symbol, untyped]?, ?background_image: Hash[Symbol, untyped]?, ?threaded_comments: Array[Hash[Symbol, untyped]]?, ?columns: Array[Elements::Column]?, ?pivot_tables: Array[Hash[Symbol, untyped]]?, ?split_pane: Hash[Symbol, untyped]?, ?filter_columns: Hash[Integer, Hash[Symbol, untyped]]?, ?sort_state: Hash[Symbol, untyped]?, ?header_footer_images: (Array[Elements::HeaderFooterImage] | Array[Hash[Symbol, untyped]])?, ?charts: (Array[Elements::Chart] | Array[Hash[Symbol, untyped]])?, ?sparkline_groups: (Array[Elements::SparklineGroup] | Array[Hash[Symbol, untyped]])?, ?print_area: String?, ?print_titles: (Elements::PrintTitles | Hash[Symbol, untyped])?) -> Elements::Worksheet
|
|
1359
|
+
def build_worksheet(name, sheet_xml, shared_strings, styles, state: :visible, zip_reader: nil, entry_name: nil, hyperlinks: nil, comments: nil, images: nil, date1904: false, dimension: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, merged_cells: nil, auto_filter: nil, tables: nil, shapes: nil, freeze_pane: nil, page_setup: nil, page_margins: nil, header_footer: nil, print_options: nil, sheet_protection: nil, sheet_view: nil, sheet_properties: nil, sheet_format: nil, row_breaks: nil, column_breaks: nil, protected_ranges: nil, cell_watches: nil, ignored_errors: nil, phonetic_properties: nil, selection: nil, sheet_calc_properties: nil, background_image: nil, threaded_comments: nil, columns: nil, pivot_tables: nil, split_pane: nil, filter_columns: nil, sort_state: nil, header_footer_images: nil, charts: nil, sparkline_groups: nil, print_area: nil, print_titles: nil)
|
|
1791
1360
|
sheet_dimension = dimension || sheet_xml&.slice(/<(?:[a-zA-Z0-9_]+:)?dimension\b[^>]*\bref=["']([^"']+)["']/, 1)
|
|
1792
|
-
resolved_calc_pr = sheet_calc_properties || sheet_calc_pr
|
|
1793
1361
|
if sheet_xml.nil? || sheet_xml.empty?
|
|
1794
1362
|
return Elements::Worksheet.new(
|
|
1795
1363
|
name: name, state: state, styles: styles, images: images || [], date1904: date1904,
|
|
@@ -1806,9 +1374,9 @@ module Xlsxrb
|
|
|
1806
1374
|
header_footer_images: header_footer_images || [],
|
|
1807
1375
|
page_setup: page_setup || {}, page_margins: page_margins, header_footer: header_footer || {},
|
|
1808
1376
|
print_options: print_options || {}, sheet_protection: sheet_protection, sheet_view: sheet_view || {},
|
|
1809
|
-
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [], column_breaks: column_breaks ||
|
|
1377
|
+
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [], column_breaks: column_breaks || [],
|
|
1810
1378
|
protected_ranges: protected_ranges || [], cell_watches: cell_watches || [], ignored_errors: ignored_errors || [],
|
|
1811
|
-
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties:
|
|
1379
|
+
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties: sheet_calc_properties,
|
|
1812
1380
|
background_image: background_image,
|
|
1813
1381
|
threaded_comments: threaded_comments || []
|
|
1814
1382
|
)
|
|
@@ -1921,7 +1489,7 @@ module Xlsxrb
|
|
|
1921
1489
|
{}
|
|
1922
1490
|
end
|
|
1923
1491
|
|
|
1924
|
-
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? ||
|
|
1492
|
+
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? || column_breaks.nil?
|
|
1925
1493
|
if sheet_xml.match?(/<(?:pageSetup|pageMargins|headerFooter|printOptions|rowBreaks|colBreaks)\b/)
|
|
1926
1494
|
pg_listener = Ooxml::Reader::PrintPageListener.new
|
|
1927
1495
|
Ooxml::XmlParser.parse(sheet_xml, pg_listener)
|
|
@@ -1931,15 +1499,13 @@ module Xlsxrb
|
|
|
1931
1499
|
header_footer: pg_listener.header_footer,
|
|
1932
1500
|
print_options: pg_listener.print_options,
|
|
1933
1501
|
row_breaks: pg_listener.row_breaks,
|
|
1934
|
-
column_breaks: pg_listener.column_breaks
|
|
1935
|
-
col_breaks: pg_listener.col_breaks
|
|
1502
|
+
column_breaks: pg_listener.column_breaks
|
|
1936
1503
|
}
|
|
1937
1504
|
else
|
|
1938
|
-
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], column_breaks: []
|
|
1505
|
+
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], column_breaks: [] }
|
|
1939
1506
|
end
|
|
1940
1507
|
else
|
|
1941
|
-
|
|
1942
|
-
{ page_margins: page_margins, page_setup: page_setup, header_footer: header_footer, print_options: print_options, row_breaks: row_breaks, column_breaks: cb, col_breaks: cb }
|
|
1508
|
+
{ page_margins: page_margins, page_setup: page_setup, header_footer: header_footer, print_options: print_options, row_breaks: row_breaks, column_breaks: column_breaks }
|
|
1943
1509
|
end
|
|
1944
1510
|
|
|
1945
1511
|
sheet_prot_ranges = protected_ranges || if sheet_xml.include?("<protectedRanges")
|
|
@@ -2015,13 +1581,13 @@ module Xlsxrb
|
|
|
2015
1581
|
sheet_properties: sheet_props,
|
|
2016
1582
|
sheet_format: sheet_fmt,
|
|
2017
1583
|
row_breaks: row_breaks || print_data[:row_breaks] || [],
|
|
2018
|
-
column_breaks: column_breaks ||
|
|
1584
|
+
column_breaks: column_breaks || print_data[:column_breaks] || [],
|
|
2019
1585
|
protected_ranges: sheet_prot_ranges,
|
|
2020
1586
|
cell_watches: sheet_watches,
|
|
2021
1587
|
ignored_errors: sheet_ignored_errors,
|
|
2022
1588
|
phonetic_properties: sheet_phonetic,
|
|
2023
1589
|
selection: sheet_selection,
|
|
2024
|
-
sheet_calc_properties:
|
|
1590
|
+
sheet_calc_properties: sheet_calc_properties,
|
|
2025
1591
|
background_image: background_image || (sheet_xml.include?("<picture") ? resolve_background_image(sheet_xml, zip_reader: zip_reader, entry_name: entry_name) : nil),
|
|
2026
1592
|
threaded_comments: sheet_threaded_comments
|
|
2027
1593
|
)
|
|
@@ -2087,7 +1653,7 @@ module Xlsxrb
|
|
|
2087
1653
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2088
1654
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2089
1655
|
|
|
2090
|
-
parser =
|
|
1656
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2091
1657
|
listener = Ooxml::Reader::RelsListener.new
|
|
2092
1658
|
parser.listen(listener)
|
|
2093
1659
|
parser.parse
|
|
@@ -2101,11 +1667,47 @@ module Xlsxrb
|
|
|
2101
1667
|
xml = zip_reader.read_entry(comments_path)
|
|
2102
1668
|
return [] if xml.nil? || xml.empty?
|
|
2103
1669
|
|
|
2104
|
-
c_parser =
|
|
1670
|
+
c_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2105
1671
|
c_listener = Ooxml::Reader::CommentsListener.new
|
|
2106
1672
|
c_parser.listen(c_listener)
|
|
2107
1673
|
c_parser.parse
|
|
2108
|
-
c_listener.comments
|
|
1674
|
+
comments = c_listener.comments
|
|
1675
|
+
notes = vml_notes(zip_reader, listener.relationships, base_dir)
|
|
1676
|
+
unless notes.empty?
|
|
1677
|
+
comments.each do |c|
|
|
1678
|
+
note = notes[c[:ref].to_s.upcase]
|
|
1679
|
+
next unless note
|
|
1680
|
+
|
|
1681
|
+
c[:visible] = true if note[:visible]
|
|
1682
|
+
c[:anchor] = note[:anchor] if note[:anchor]
|
|
1683
|
+
end
|
|
1684
|
+
end
|
|
1685
|
+
comments
|
|
1686
|
+
end
|
|
1687
|
+
|
|
1688
|
+
# Reads the comment notes of the sheet's legacy VML drawing, keyed by cell reference:
|
|
1689
|
+
# whether each is shown permanently (<x:Visible/>) and its <x:Anchor> box position.
|
|
1690
|
+
#: (Ooxml::ZipReader zip_reader, Array[Hash[Symbol, untyped]] relationships, String base_dir) -> Hash[String, Hash[Symbol, untyped]]
|
|
1691
|
+
def vml_notes(zip_reader, relationships, base_dir)
|
|
1692
|
+
rel = relationships.find { |r| r[:type]&.end_with?("/vmlDrawing") }
|
|
1693
|
+
vml = rel && zip_reader.read_entry(resolve_relative_path(base_dir, rel[:target]))
|
|
1694
|
+
return {} if vml.nil?
|
|
1695
|
+
|
|
1696
|
+
vml.split("</v:shape>").each_with_object({}) do |shape, notes|
|
|
1697
|
+
next unless shape.include?('ObjectType="Note"')
|
|
1698
|
+
|
|
1699
|
+
row = shape[%r{<x:Row>\s*(\d+)\s*</x:Row>}, 1]
|
|
1700
|
+
col = shape[%r{<x:Column>\s*(\d+)\s*</x:Column>}, 1]
|
|
1701
|
+
next unless row && col
|
|
1702
|
+
|
|
1703
|
+
anchor = shape[%r{<x:Anchor>([\d,\s]+)</x:Anchor>}, 1]&.split(",")&.map(&:to_i)
|
|
1704
|
+
# The writer's default box reads back as no anchor, as it was built.
|
|
1705
|
+
anchor = nil if anchor&.size != 8 || anchor == Elements::Comment.default_anchor(col.to_i, row.to_i)
|
|
1706
|
+
notes["#{Elements::Cell._column_letter(col.to_i)}#{row.to_i + 1}"] = {
|
|
1707
|
+
visible: shape.include?("<x:Visible"),
|
|
1708
|
+
anchor: anchor
|
|
1709
|
+
}
|
|
1710
|
+
end
|
|
2109
1711
|
end
|
|
2110
1712
|
|
|
2111
1713
|
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Hash[Symbol, untyped]]
|
|
@@ -2116,7 +1718,7 @@ module Xlsxrb
|
|
|
2116
1718
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2117
1719
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2118
1720
|
|
|
2119
|
-
parser =
|
|
1721
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2120
1722
|
listener = Ooxml::Reader::RelsListener.new
|
|
2121
1723
|
parser.listen(listener)
|
|
2122
1724
|
parser.parse
|
|
@@ -2130,7 +1732,7 @@ module Xlsxrb
|
|
|
2130
1732
|
xml = zip_reader.read_entry(tc_path)
|
|
2131
1733
|
return [] if xml.nil? || xml.empty?
|
|
2132
1734
|
|
|
2133
|
-
tc_parser =
|
|
1735
|
+
tc_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2134
1736
|
tc_listener = Ooxml::Reader::ThreadedCommentsListener.new
|
|
2135
1737
|
tc_parser.listen(tc_listener)
|
|
2136
1738
|
tc_parser.parse
|
|
@@ -2151,7 +1753,7 @@ module Xlsxrb
|
|
|
2151
1753
|
if xml.nil? || xml.empty?
|
|
2152
1754
|
wb_rels_xml = zip_reader.read_entry("xl/_rels/workbook.xml.rels")
|
|
2153
1755
|
if wb_rels_xml && !wb_rels_xml.empty?
|
|
2154
|
-
parser =
|
|
1756
|
+
parser = Ooxml::XmlParser.sax2_parser(wb_rels_xml)
|
|
2155
1757
|
listener = Ooxml::Reader::RelsListener.new
|
|
2156
1758
|
parser.listen(listener)
|
|
2157
1759
|
parser.parse
|
|
@@ -2164,7 +1766,7 @@ module Xlsxrb
|
|
|
2164
1766
|
end
|
|
2165
1767
|
return {} if xml.nil? || xml.empty?
|
|
2166
1768
|
|
|
2167
|
-
p_parser =
|
|
1769
|
+
p_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2168
1770
|
p_listener = Ooxml::Reader::PersonListListener.new
|
|
2169
1771
|
p_parser.listen(p_listener)
|
|
2170
1772
|
p_parser.parse
|
|
@@ -2179,7 +1781,7 @@ module Xlsxrb
|
|
|
2179
1781
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2180
1782
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2181
1783
|
|
|
2182
|
-
parser =
|
|
1784
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2183
1785
|
listener = Ooxml::Reader::RelsListener.new
|
|
2184
1786
|
parser.listen(listener)
|
|
2185
1787
|
parser.parse
|
|
@@ -2197,7 +1799,7 @@ module Xlsxrb
|
|
|
2197
1799
|
drawing_rels_xml = zip_reader.read_entry(drawing_rels_path)
|
|
2198
1800
|
drawing_rels = {}
|
|
2199
1801
|
if drawing_rels_xml && !drawing_rels_xml.empty?
|
|
2200
|
-
d_parser =
|
|
1802
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_rels_xml)
|
|
2201
1803
|
d_listener = Ooxml::Reader::RelsListener.new
|
|
2202
1804
|
d_parser.listen(d_listener)
|
|
2203
1805
|
d_parser.parse
|
|
@@ -2206,7 +1808,7 @@ module Xlsxrb
|
|
|
2206
1808
|
end
|
|
2207
1809
|
end
|
|
2208
1810
|
|
|
2209
|
-
p_parser =
|
|
1811
|
+
p_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2210
1812
|
img_listener = Ooxml::Reader::DrawingImagesListener.new
|
|
2211
1813
|
p_parser.listen(img_listener)
|
|
2212
1814
|
p_parser.parse
|
|
@@ -2259,7 +1861,7 @@ module Xlsxrb
|
|
|
2259
1861
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2260
1862
|
return nil if rels_xml.nil? || rels_xml.empty?
|
|
2261
1863
|
|
|
2262
|
-
parser =
|
|
1864
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2263
1865
|
listener = Ooxml::Reader::RelsListener.new
|
|
2264
1866
|
parser.listen(listener)
|
|
2265
1867
|
parser.parse
|
|
@@ -2284,10 +1886,10 @@ module Xlsxrb
|
|
|
2284
1886
|
end
|
|
2285
1887
|
|
|
2286
1888
|
{
|
|
1889
|
+
r_id: picture_rid,
|
|
1890
|
+
path: target_path,
|
|
1891
|
+
extension: ext,
|
|
2287
1892
|
data: data.b,
|
|
2288
|
-
file_data: data.b,
|
|
2289
|
-
ext: ext,
|
|
2290
|
-
filename: File.basename(target_path),
|
|
2291
1893
|
content_type: mime
|
|
2292
1894
|
}.freeze
|
|
2293
1895
|
end
|
|
@@ -2300,7 +1902,7 @@ module Xlsxrb
|
|
|
2300
1902
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2301
1903
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2302
1904
|
|
|
2303
|
-
parser =
|
|
1905
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2304
1906
|
listener = Ooxml::Reader::RelsListener.new
|
|
2305
1907
|
parser.listen(listener)
|
|
2306
1908
|
parser.parse
|
|
@@ -2314,7 +1916,7 @@ module Xlsxrb
|
|
|
2314
1916
|
tbl_xml = zip_reader.read_entry(tbl_path)
|
|
2315
1917
|
next if tbl_xml.nil? || tbl_xml.empty?
|
|
2316
1918
|
|
|
2317
|
-
t_parser =
|
|
1919
|
+
t_parser = Ooxml::XmlParser.sax2_parser(tbl_xml)
|
|
2318
1920
|
t_listener = Ooxml::Reader::TableListener.new
|
|
2319
1921
|
t_parser.listen(t_listener)
|
|
2320
1922
|
t_parser.parse
|
|
@@ -2330,7 +1932,7 @@ module Xlsxrb
|
|
|
2330
1932
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2331
1933
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2332
1934
|
|
|
2333
|
-
parser =
|
|
1935
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2334
1936
|
listener = Ooxml::Reader::RelsListener.new
|
|
2335
1937
|
parser.listen(listener)
|
|
2336
1938
|
parser.parse
|
|
@@ -2343,7 +1945,7 @@ module Xlsxrb
|
|
|
2343
1945
|
drawing_xml = zip_reader.read_entry(drawing_path)
|
|
2344
1946
|
return [] if drawing_xml.nil? || drawing_xml.empty?
|
|
2345
1947
|
|
|
2346
|
-
d_parser =
|
|
1948
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2347
1949
|
d_listener = Ooxml::Reader::DrawingShapesListener.new
|
|
2348
1950
|
d_parser.listen(d_listener)
|
|
2349
1951
|
d_parser.parse
|
|
@@ -2358,7 +1960,7 @@ module Xlsxrb
|
|
|
2358
1960
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2359
1961
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2360
1962
|
|
|
2361
|
-
parser =
|
|
1963
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2362
1964
|
listener = Ooxml::Reader::RelsListener.new
|
|
2363
1965
|
parser.listen(listener)
|
|
2364
1966
|
parser.parse
|
|
@@ -2372,7 +1974,7 @@ module Xlsxrb
|
|
|
2372
1974
|
pt_xml = zip_reader.read_entry(pt_path)
|
|
2373
1975
|
next if pt_xml.nil? || pt_xml.empty?
|
|
2374
1976
|
|
|
2375
|
-
p_parser =
|
|
1977
|
+
p_parser = Ooxml::XmlParser.sax2_parser(pt_xml)
|
|
2376
1978
|
p_listener = Ooxml::Reader::PivotTableListener.new
|
|
2377
1979
|
p_parser.listen(p_listener)
|
|
2378
1980
|
p_parser.parse
|
|
@@ -2382,7 +1984,7 @@ module Xlsxrb
|
|
|
2382
1984
|
pt_rels_path = pt_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2383
1985
|
pt_rels_xml = zip_reader.read_entry(pt_rels_path)
|
|
2384
1986
|
if pt_rels_xml && !pt_rels_xml.empty?
|
|
2385
|
-
c_parser =
|
|
1987
|
+
c_parser = Ooxml::XmlParser.sax2_parser(pt_rels_xml)
|
|
2386
1988
|
c_listener = Ooxml::Reader::RelsListener.new
|
|
2387
1989
|
c_parser.listen(c_listener)
|
|
2388
1990
|
c_parser.parse
|
|
@@ -2391,7 +1993,7 @@ module Xlsxrb
|
|
|
2391
1993
|
c_path = resolve_relative_path(File.dirname(pt_path), cache_rel[:target])
|
|
2392
1994
|
c_xml = zip_reader.read_entry(c_path)
|
|
2393
1995
|
if c_xml && !c_xml.empty?
|
|
2394
|
-
cd_parser =
|
|
1996
|
+
cd_parser = Ooxml::XmlParser.sax2_parser(c_xml)
|
|
2395
1997
|
cd_listener = Ooxml::Reader::PivotCacheDefinitionListener.new
|
|
2396
1998
|
cd_parser.listen(cd_listener)
|
|
2397
1999
|
cd_parser.parse
|
|
@@ -2400,7 +2002,7 @@ module Xlsxrb
|
|
|
2400
2002
|
cd_rels_path = c_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2401
2003
|
cd_rels_xml = zip_reader.read_entry(cd_rels_path)
|
|
2402
2004
|
if cd_rels_xml && !cd_rels_xml.empty?
|
|
2403
|
-
rec_parser =
|
|
2005
|
+
rec_parser = Ooxml::XmlParser.sax2_parser(cd_rels_xml)
|
|
2404
2006
|
rec_rels_listener = Ooxml::Reader::RelsListener.new
|
|
2405
2007
|
rec_parser.listen(rec_rels_listener)
|
|
2406
2008
|
rec_parser.parse
|
|
@@ -2409,7 +2011,7 @@ module Xlsxrb
|
|
|
2409
2011
|
rec_path = resolve_relative_path(File.dirname(c_path), rec_rel[:target])
|
|
2410
2012
|
rec_xml = zip_reader.read_entry(rec_path)
|
|
2411
2013
|
if rec_xml && !rec_xml.empty?
|
|
2412
|
-
r_parser =
|
|
2014
|
+
r_parser = Ooxml::XmlParser.sax2_parser(rec_xml)
|
|
2413
2015
|
r_listener = Ooxml::Reader::PivotCacheRecordsListener.new
|
|
2414
2016
|
r_parser.listen(r_listener)
|
|
2415
2017
|
r_parser.parse
|
|
@@ -2434,7 +2036,7 @@ module Xlsxrb
|
|
|
2434
2036
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2435
2037
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2436
2038
|
|
|
2437
|
-
parser =
|
|
2039
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2438
2040
|
listener = Ooxml::Reader::RelsListener.new
|
|
2439
2041
|
parser.listen(listener)
|
|
2440
2042
|
parser.parse
|
|
@@ -2451,7 +2053,7 @@ module Xlsxrb
|
|
|
2451
2053
|
drawing_rels_xml = zip_reader.read_entry(drawing_rels_path)
|
|
2452
2054
|
drawing_rels = {}
|
|
2453
2055
|
if drawing_rels_xml && !drawing_rels_xml.empty?
|
|
2454
|
-
d_parser =
|
|
2056
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_rels_xml)
|
|
2455
2057
|
d_listener = Ooxml::Reader::RelsListener.new
|
|
2456
2058
|
d_parser.listen(d_listener)
|
|
2457
2059
|
d_parser.parse
|
|
@@ -2460,7 +2062,7 @@ module Xlsxrb
|
|
|
2460
2062
|
end
|
|
2461
2063
|
end
|
|
2462
2064
|
|
|
2463
|
-
d_parser =
|
|
2065
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2464
2066
|
dc_listener = Ooxml::Reader::DrawingChartsListener.new
|
|
2465
2067
|
d_parser.listen(dc_listener)
|
|
2466
2068
|
d_parser.parse
|
|
@@ -2474,7 +2076,7 @@ module Xlsxrb
|
|
|
2474
2076
|
chart_path = resolve_relative_path(drawing_dir, target)
|
|
2475
2077
|
chart_xml = zip_reader.read_entry(chart_path)
|
|
2476
2078
|
if chart_xml && !chart_xml.empty?
|
|
2477
|
-
cp =
|
|
2079
|
+
cp = Ooxml::XmlParser.sax2_parser(chart_xml)
|
|
2478
2080
|
cl = Ooxml::Reader::ChartTypeListener.new
|
|
2479
2081
|
cp.listen(cl)
|
|
2480
2082
|
cp.parse
|
|
@@ -2680,10 +2282,14 @@ module Xlsxrb
|
|
|
2680
2282
|
end
|
|
2681
2283
|
end
|
|
2682
2284
|
end
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
chart[:
|
|
2686
|
-
chart[:
|
|
2285
|
+
from_col = chart.delete(:from_col)
|
|
2286
|
+
to_col = chart.delete(:to_col)
|
|
2287
|
+
chart[:from_column] ||= from_col if from_col
|
|
2288
|
+
chart[:to_column] ||= to_col if to_col
|
|
2289
|
+
rid = chart.delete(:rid)
|
|
2290
|
+
chart[:r_id] ||= rid if rid
|
|
2291
|
+
ctype = chart.delete(:chart_type)
|
|
2292
|
+
chart[:type] ||= ctype if ctype
|
|
2687
2293
|
Elements::Chart.new(**chart)
|
|
2688
2294
|
end
|
|
2689
2295
|
end
|
|
@@ -2693,7 +2299,7 @@ module Xlsxrb
|
|
|
2693
2299
|
return [] if sheet_xml.nil? || !sheet_xml.include?("sparklineGroup")
|
|
2694
2300
|
|
|
2695
2301
|
listener = Ooxml::Reader::SparklineGroupsListener.new
|
|
2696
|
-
parser =
|
|
2302
|
+
parser = Ooxml::XmlParser.sax2_parser(sheet_xml)
|
|
2697
2303
|
parser.listen(listener)
|
|
2698
2304
|
parser.parse
|
|
2699
2305
|
listener.groups.map do |grp|
|
|
@@ -2714,7 +2320,7 @@ module Xlsxrb
|
|
|
2714
2320
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2715
2321
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2716
2322
|
|
|
2717
|
-
parser =
|
|
2323
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2718
2324
|
listener = Ooxml::Reader::RelsListener.new
|
|
2719
2325
|
parser.listen(listener)
|
|
2720
2326
|
parser.parse
|
|
@@ -2731,7 +2337,7 @@ module Xlsxrb
|
|
|
2731
2337
|
vml_rels_xml = zip_reader.read_entry(vml_rels_path)
|
|
2732
2338
|
vml_rels = {}
|
|
2733
2339
|
if vml_rels_xml && !vml_rels_xml.empty?
|
|
2734
|
-
vr_parser =
|
|
2340
|
+
vr_parser = Ooxml::XmlParser.sax2_parser(vml_rels_xml)
|
|
2735
2341
|
vr_listener = Ooxml::Reader::RelsListener.new
|
|
2736
2342
|
vr_parser.listen(vr_listener)
|
|
2737
2343
|
vr_parser.parse
|
|
@@ -2740,7 +2346,7 @@ module Xlsxrb
|
|
|
2740
2346
|
end
|
|
2741
2347
|
end
|
|
2742
2348
|
|
|
2743
|
-
vml_parser =
|
|
2349
|
+
vml_parser = Ooxml::XmlParser.sax2_parser(vml_xml)
|
|
2744
2350
|
hf_listener = Ooxml::Reader::VmlHeaderFooterListener.new
|
|
2745
2351
|
vml_parser.listen(hf_listener)
|
|
2746
2352
|
vml_parser.parse
|
|
@@ -2756,7 +2362,6 @@ module Xlsxrb
|
|
|
2756
2362
|
next unless img_data
|
|
2757
2363
|
|
|
2758
2364
|
img_data = img_data.b
|
|
2759
|
-
filename = File.basename(media_path)
|
|
2760
2365
|
ext = File.extname(media_path).delete_prefix(".").downcase
|
|
2761
2366
|
content_type = case ext
|
|
2762
2367
|
when "png" then "image/png"
|
|
@@ -2771,9 +2376,7 @@ module Xlsxrb
|
|
|
2771
2376
|
position: shape[:id],
|
|
2772
2377
|
r_id: rel_id,
|
|
2773
2378
|
path: media_path,
|
|
2774
|
-
filename: filename,
|
|
2775
2379
|
extension: ext,
|
|
2776
|
-
ext: ext,
|
|
2777
2380
|
data: img_data,
|
|
2778
2381
|
content_type: content_type
|
|
2779
2382
|
)
|
|
@@ -2822,7 +2425,7 @@ module Xlsxrb
|
|
|
2822
2425
|
|
|
2823
2426
|
return nil if rows.nil? && cols.nil?
|
|
2824
2427
|
|
|
2825
|
-
Elements::PrintTitles.new(rows: rows, columns: cols
|
|
2428
|
+
Elements::PrintTitles.new(rows: rows, columns: cols)
|
|
2826
2429
|
end
|
|
2827
2430
|
|
|
2828
2431
|
#: (untyped defined_name, String? sheet_name, Integer? sheet_index) -> bool
|
|
@@ -2889,7 +2492,7 @@ module Xlsxrb
|
|
|
2889
2492
|
{}
|
|
2890
2493
|
end
|
|
2891
2494
|
|
|
2892
|
-
raw_cells = raw_row[:cells]
|
|
2495
|
+
raw_cells = raw_row.respond_to?(:cells) ? raw_row.cells : raw_row[:cells]
|
|
2893
2496
|
cells = raw_cells.map do |rc|
|
|
2894
2497
|
if rc.is_a?(Elements::Cell)
|
|
2895
2498
|
hl = hyperlinks_map[rc.ref]
|
|
@@ -2909,7 +2512,11 @@ module Xlsxrb
|
|
|
2909
2512
|
end
|
|
2910
2513
|
else
|
|
2911
2514
|
parsed = Elements::Cell._parse_ref(rc[:ref]) if rc[:ref]
|
|
2912
|
-
row_idx =
|
|
2515
|
+
row_idx = if parsed
|
|
2516
|
+
parsed[0]
|
|
2517
|
+
else
|
|
2518
|
+
(raw_row.respond_to?(:index) ? raw_row.index : raw_row[:index])
|
|
2519
|
+
end
|
|
2913
2520
|
col_idx = parsed ? parsed[1] : 0
|
|
2914
2521
|
ref = rc[:ref] || "#{Elements::Cell._column_letter(col_idx)}#{row_idx + 1}"
|
|
2915
2522
|
ref_key = ref.upcase
|
|
@@ -2943,20 +2550,24 @@ module Xlsxrb
|
|
|
2943
2550
|
)
|
|
2944
2551
|
end
|
|
2945
2552
|
end
|
|
2946
|
-
attrs = raw_row[:attrs] || {}
|
|
2947
|
-
|
|
2948
|
-
if
|
|
2553
|
+
attrs = raw_row.respond_to?(:attributes) ? raw_row.attributes : (raw_row[:attrs] || {})
|
|
2554
|
+
row_idx = raw_row.respond_to?(:index) ? raw_row.index : raw_row[:index]
|
|
2555
|
+
row_source = if raw_row.respond_to?(:source)
|
|
2556
|
+
raw_row.source
|
|
2557
|
+
else
|
|
2558
|
+
(raw_row.is_a?(Hash) ? raw_row[:source] : nil)
|
|
2559
|
+
end
|
|
2560
|
+
row_errors = Elements::Row.validate(row_idx, cells)
|
|
2561
|
+
if !row_errors.empty? && row_source
|
|
2949
2562
|
# simplecov:disable
|
|
2950
2563
|
# Edge case / untested delegation block
|
|
2951
2564
|
row_errors = row_errors.map do |err|
|
|
2952
|
-
"#{err} (at #{
|
|
2565
|
+
"#{err} (at #{row_source[:part]} row #{row_source[:row] + 1})"
|
|
2953
2566
|
# simplecov:enable
|
|
2954
2567
|
end
|
|
2955
2568
|
end
|
|
2956
|
-
row_unmapped = {}
|
|
2957
|
-
row_unmapped[:style_index] = attrs[:style_index] if attrs[:style_index]
|
|
2958
2569
|
Elements::Row.new(
|
|
2959
|
-
index:
|
|
2570
|
+
index: row_idx,
|
|
2960
2571
|
cells: cells,
|
|
2961
2572
|
height: attrs[:height],
|
|
2962
2573
|
hidden: attrs[:hidden] || false,
|
|
@@ -2964,7 +2575,7 @@ module Xlsxrb
|
|
|
2964
2575
|
collapsed: attrs[:collapsed] || false,
|
|
2965
2576
|
outline_level: attrs[:outline_level],
|
|
2966
2577
|
style_index: attrs[:style_index],
|
|
2967
|
-
unmapped_data:
|
|
2578
|
+
unmapped_data: Elements::EMPTY_HASH,
|
|
2968
2579
|
errors: row_errors
|
|
2969
2580
|
)
|
|
2970
2581
|
end
|
|
@@ -2994,7 +2605,12 @@ module Xlsxrb
|
|
|
2994
2605
|
source
|
|
2995
2606
|
end
|
|
2996
2607
|
else
|
|
2997
|
-
|
|
2608
|
+
# Not XLSX or encrypted bytes and not an existing file: report the path rather than
|
|
2609
|
+
# parsing the path string itself as ZIP content.
|
|
2610
|
+
raise Errno::EISDIR, source if File.directory?(source)
|
|
2611
|
+
|
|
2612
|
+
shown = source.length > 200 || source.include?("\n") ? "#{source[0, 60].inspect}... (#{source.bytesize} bytes)" : source
|
|
2613
|
+
raise Errno::ENOENT, shown
|
|
2998
2614
|
end
|
|
2999
2615
|
elsif source.respond_to?(:read) && source.respond_to?(:pos) && source.respond_to?(:seek)
|
|
3000
2616
|
begin
|
|
@@ -3015,438 +2631,5 @@ module Xlsxrb
|
|
|
3015
2631
|
source
|
|
3016
2632
|
end
|
|
3017
2633
|
end
|
|
3018
|
-
|
|
3019
|
-
#: (untyped dest, Ooxml::ZipReader zip_reader, Hash[String, String] replaced_entries, ?password: String?) -> void
|
|
3020
|
-
def perform_modify_write(dest, zip_reader, replaced_entries, password: nil)
|
|
3021
|
-
norm_replaced = {}
|
|
3022
|
-
replaced_entries.each do |k, v|
|
|
3023
|
-
norm_replaced[k.tr("\\", "/").downcase] = [k.tr("\\", "/"), v]
|
|
3024
|
-
end
|
|
3025
|
-
|
|
3026
|
-
write_zip = lambda do |zip_out|
|
|
3027
|
-
Ooxml::ZipWriter.open(zip_out) do |zip_writer|
|
|
3028
|
-
written_keys = Set.new
|
|
3029
|
-
|
|
3030
|
-
zip_reader.entry_names.each do |name|
|
|
3031
|
-
norm_name = name.tr("\\", "/").downcase
|
|
3032
|
-
next if written_keys.include?(norm_name)
|
|
3033
|
-
|
|
3034
|
-
written_keys << norm_name
|
|
3035
|
-
canonical_path = name.tr("\\", "/")
|
|
3036
|
-
|
|
3037
|
-
if norm_replaced.key?(norm_name)
|
|
3038
|
-
_orig_key, content = norm_replaced[norm_name]
|
|
3039
|
-
zip_writer.add_entry(canonical_path, content)
|
|
3040
|
-
else
|
|
3041
|
-
zip_reader.copy_to_writer(name, zip_writer, target_path: canonical_path)
|
|
3042
|
-
end
|
|
3043
|
-
end
|
|
3044
|
-
|
|
3045
|
-
norm_replaced.each do |norm_name, (canonical_name, content)|
|
|
3046
|
-
next if written_keys.include?(norm_name)
|
|
3047
|
-
|
|
3048
|
-
written_keys << norm_name
|
|
3049
|
-
zip_writer.add_entry(canonical_name, content)
|
|
3050
|
-
end
|
|
3051
|
-
end
|
|
3052
|
-
end
|
|
3053
|
-
|
|
3054
|
-
if password && !password.empty?
|
|
3055
|
-
buf = StringIO.new
|
|
3056
|
-
buf.binmode
|
|
3057
|
-
write_zip.call(buf)
|
|
3058
|
-
encrypted_bytes = Ooxml::Crypto.encrypt(buf.string.b, password)
|
|
3059
|
-
if dest.is_a?(String)
|
|
3060
|
-
File.binwrite(dest, encrypted_bytes)
|
|
3061
|
-
elsif dest.respond_to?(:write)
|
|
3062
|
-
dest.write(encrypted_bytes)
|
|
3063
|
-
end
|
|
3064
|
-
else
|
|
3065
|
-
write_zip.call(dest)
|
|
3066
|
-
end
|
|
3067
|
-
end
|
|
3068
|
-
|
|
3069
|
-
#: (String sst_xml, Array[untyped] new_strings) -> String
|
|
3070
|
-
def append_to_shared_strings_xml(sst_xml, new_strings)
|
|
3071
|
-
return sst_xml if new_strings.nil? || new_strings.empty?
|
|
3072
|
-
|
|
3073
|
-
sst_match = sst_xml.match(/<([a-zA-Z0-9_]+:)?sst\b([^>]*)>/)
|
|
3074
|
-
prefix_with_colon = sst_match ? (sst_match[1] || "") : ""
|
|
3075
|
-
si_tag = "#{prefix_with_colon}si"
|
|
3076
|
-
t_tag = "#{prefix_with_colon}t"
|
|
3077
|
-
|
|
3078
|
-
new_si_tags = +""
|
|
3079
|
-
new_strings.each do |str|
|
|
3080
|
-
s = str.to_s
|
|
3081
|
-
new_si_tags << "<#{si_tag}><#{t_tag}>#{Ooxml::XmlBuilder.escape(s)}</#{t_tag}></#{si_tag}>"
|
|
3082
|
-
end
|
|
3083
|
-
|
|
3084
|
-
close_tag = "</#{prefix_with_colon}sst>"
|
|
3085
|
-
sst_close = sst_xml.rindex(close_tag)
|
|
3086
|
-
updated_xml = if sst_close
|
|
3087
|
-
prefix = sst_xml[0...sst_close]
|
|
3088
|
-
suffix = sst_xml[sst_close..]
|
|
3089
|
-
"#{prefix}#{new_si_tags}#{suffix}"
|
|
3090
|
-
else
|
|
3091
|
-
self_closing = sst_xml.match(%r{<#{Regexp.escape(prefix_with_colon)}sst\b[^>]*/>})
|
|
3092
|
-
return sst_xml unless self_closing
|
|
3093
|
-
|
|
3094
|
-
sc_idx = self_closing.begin(0)
|
|
3095
|
-
sc_end = self_closing.end(0)
|
|
3096
|
-
opening = self_closing[0].sub(%r{/>\z}, ">")
|
|
3097
|
-
prefix = sst_xml[0...sc_idx]
|
|
3098
|
-
suffix = sst_xml[sc_end..]
|
|
3099
|
-
"#{prefix}#{opening}#{new_si_tags}#{close_tag}#{suffix}"
|
|
3100
|
-
end
|
|
3101
|
-
|
|
3102
|
-
updated_xml.sub(/<#{Regexp.escape(prefix_with_colon)}sst\b([^>]*)>/) do
|
|
3103
|
-
attrs = Regexp.last_match(1)
|
|
3104
|
-
if attrs =~ /\bcount="(\d+)"/
|
|
3105
|
-
old_count = Regexp.last_match(1).to_i
|
|
3106
|
-
new_count = old_count + new_strings.size
|
|
3107
|
-
attrs = attrs.sub(/\bcount="\d+"/, "count=\"#{new_count}\"")
|
|
3108
|
-
else
|
|
3109
|
-
attrs = "#{attrs} count=\"#{new_strings.size}\""
|
|
3110
|
-
end
|
|
3111
|
-
if attrs =~ /\buniqueCount="(\d+)"/
|
|
3112
|
-
old_ucount = Regexp.last_match(1).to_i
|
|
3113
|
-
new_ucount = old_ucount + new_strings.size
|
|
3114
|
-
attrs = attrs.sub(/\buniqueCount="\d+"/, "uniqueCount=\"#{new_ucount}\"")
|
|
3115
|
-
else
|
|
3116
|
-
attrs = "#{attrs} uniqueCount=\"#{new_strings.size}\""
|
|
3117
|
-
end
|
|
3118
|
-
"<#{prefix_with_colon}sst#{attrs}>"
|
|
3119
|
-
end
|
|
3120
|
-
end
|
|
3121
|
-
|
|
3122
|
-
#: (String sheet_xml, String new_sheet_data_xml) -> String
|
|
3123
|
-
def replace_sheet_data(sheet_xml, new_sheet_data_xml)
|
|
3124
|
-
orig_encoding = sheet_xml.encoding
|
|
3125
|
-
sheet_b = sheet_xml.b
|
|
3126
|
-
sd_term = sheet_b.index("sheetData")
|
|
3127
|
-
return sheet_xml unless sd_term
|
|
3128
|
-
|
|
3129
|
-
sheet_data_start = sheet_b.rindex("<", sd_term)
|
|
3130
|
-
return sheet_xml unless sheet_data_start
|
|
3131
|
-
|
|
3132
|
-
prefix_len = sd_term - (sheet_data_start + 1)
|
|
3133
|
-
tag_prefix = sheet_b.byteslice(sheet_data_start + 1, prefix_len).force_encoding(orig_encoding)
|
|
3134
|
-
|
|
3135
|
-
open_tag_end = sheet_b.index(">", sd_term)
|
|
3136
|
-
return sheet_xml unless open_tag_end
|
|
3137
|
-
|
|
3138
|
-
prefix = sheet_b.byteslice(0...sheet_data_start)
|
|
3139
|
-
suffix = if sheet_b.getbyte(open_tag_end - 1) == 47
|
|
3140
|
-
sheet_b.byteslice((open_tag_end + 1)..) || "".b
|
|
3141
|
-
else
|
|
3142
|
-
close_tag = "</#{tag_prefix}sheetData>".b
|
|
3143
|
-
close_tag_start = sheet_b.index(close_tag, open_tag_end)
|
|
3144
|
-
return sheet_xml unless close_tag_start
|
|
3145
|
-
|
|
3146
|
-
sheet_b.byteslice((close_tag_start + close_tag.bytesize)..) || "".b
|
|
3147
|
-
end
|
|
3148
|
-
|
|
3149
|
-
new_sheet_data_xml = new_sheet_data_xml.gsub(%r{</?(sheetData|row|c|v|f|is|t)\b}) { |m| m.sub(%r{(</?)}, "\\1#{tag_prefix}") } if tag_prefix && !tag_prefix.empty?
|
|
3150
|
-
|
|
3151
|
-
result = prefix << new_sheet_data_xml.b << suffix
|
|
3152
|
-
result.force_encoding(orig_encoding)
|
|
3153
|
-
end
|
|
3154
|
-
|
|
3155
|
-
#: (String sheet_xml, Array[Elements::Row] rows) -> String
|
|
3156
|
-
def update_dimension(sheet_xml, rows)
|
|
3157
|
-
return sheet_xml if rows.empty?
|
|
3158
|
-
|
|
3159
|
-
min_row = rows.first.index
|
|
3160
|
-
max_row = rows.last.index
|
|
3161
|
-
min_col = rows.map { |r| r.cells.first&.column_index }.compact.min || 0
|
|
3162
|
-
max_col = rows.map { |r| r.cells.last&.column_index }.compact.max || 0
|
|
3163
|
-
min_col_letter = Elements::Cell._column_letter(min_col)
|
|
3164
|
-
max_col_letter = Elements::Cell._column_letter(max_col)
|
|
3165
|
-
dim_ref = if min_row == max_row && min_col == max_col
|
|
3166
|
-
"#{min_col_letter}#{min_row + 1}"
|
|
3167
|
-
else
|
|
3168
|
-
"#{min_col_letter}#{min_row + 1}:#{max_col_letter}#{max_row + 1}"
|
|
3169
|
-
end
|
|
3170
|
-
|
|
3171
|
-
sheet_xml.sub(/<([a-zA-Z0-9_]+:)?dimension\b([^>]*)\bref="[^"]*"/, "<\\1dimension\\2ref=\"#{dim_ref}\"")
|
|
3172
|
-
end
|
|
3173
|
-
|
|
3174
|
-
#: (String sheet_xml, String? orig_rels_xml, Hash[String, untyped] new_hyperlinks) -> [String, String?]
|
|
3175
|
-
def update_sheet_hyperlinks(sheet_xml, orig_rels_xml, new_hyperlinks)
|
|
3176
|
-
existing_rels = []
|
|
3177
|
-
max_rid_num = 0
|
|
3178
|
-
if orig_rels_xml && !orig_rels_xml.empty?
|
|
3179
|
-
orig_rels_xml.scan(%r{<Relationship\b([^>]*)/?>}) do |m|
|
|
3180
|
-
attrs = m[0]
|
|
3181
|
-
id = attrs[/Id="([^"]*)"/, 1]
|
|
3182
|
-
type = attrs[/Type="([^"]*)"/, 1]
|
|
3183
|
-
target = attrs[/Target="([^"]*)"/, 1]
|
|
3184
|
-
target_mode = attrs[/TargetMode="([^"]*)"/, 1]
|
|
3185
|
-
if id =~ /\ArId(\d+)\z/
|
|
3186
|
-
num = Regexp.last_match(1).to_i
|
|
3187
|
-
max_rid_num = num if num > max_rid_num
|
|
3188
|
-
end
|
|
3189
|
-
existing_rels << { id: id, type: type, target: target, target_mode: target_mode } unless type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
|
|
3190
|
-
end
|
|
3191
|
-
end
|
|
3192
|
-
|
|
3193
|
-
sheet_rels = existing_rels.dup
|
|
3194
|
-
cur_rid = max_rid_num
|
|
3195
|
-
hl_elements = []
|
|
3196
|
-
|
|
3197
|
-
ws_prefix = sheet_xml[/<([a-zA-Z0-9_]+:)?worksheet\b/, 1] || ""
|
|
3198
|
-
|
|
3199
|
-
new_hyperlinks.each do |cell_ref, link|
|
|
3200
|
-
entry = link.is_a?(Hash) ? link.dup : { url: link.to_s }
|
|
3201
|
-
url = entry[:url]
|
|
3202
|
-
loc = entry[:location]
|
|
3203
|
-
if url&.start_with?("#")
|
|
3204
|
-
loc ||= url.delete_prefix("#")
|
|
3205
|
-
url = nil
|
|
3206
|
-
end
|
|
3207
|
-
display = entry[:display]
|
|
3208
|
-
tooltip = entry[:tooltip]
|
|
3209
|
-
|
|
3210
|
-
attrs = if url && !url.empty?
|
|
3211
|
-
cur_rid += 1
|
|
3212
|
-
rid = "rId#{cur_rid}"
|
|
3213
|
-
sheet_rels << {
|
|
3214
|
-
id: rid,
|
|
3215
|
-
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
|
3216
|
-
target: url,
|
|
3217
|
-
target_mode: "External"
|
|
3218
|
-
}
|
|
3219
|
-
%(ref="#{cell_ref}" r:id="#{rid}")
|
|
3220
|
-
elsif loc && !loc.empty?
|
|
3221
|
-
%(ref="#{cell_ref}" location="#{Ooxml::XmlBuilder.escape(loc)}")
|
|
3222
|
-
else
|
|
3223
|
-
next
|
|
3224
|
-
end
|
|
3225
|
-
|
|
3226
|
-
attrs << %( display="#{Ooxml::XmlBuilder.escape(display)}") if display
|
|
3227
|
-
attrs << %( tooltip="#{Ooxml::XmlBuilder.escape(tooltip)}") if tooltip
|
|
3228
|
-
hl_elements << "<#{ws_prefix}hyperlink #{attrs}/>"
|
|
3229
|
-
end
|
|
3230
|
-
|
|
3231
|
-
updated_sheet_xml = sheet_xml.dup
|
|
3232
|
-
root_tag = updated_sheet_xml[/\A\s*(?:<\?xml[^>]*\?>\s*)?<([a-zA-Z0-9_]+:)?worksheet\b[^>]*/, 0]
|
|
3233
|
-
updated_sheet_xml.sub!(/<([a-zA-Z0-9_]+:)?worksheet\b/, '\0 xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"') if hl_elements.any? && (root_tag.nil? || !root_tag.match?(/\bxmlns:r\s*=/))
|
|
3234
|
-
|
|
3235
|
-
new_hyperlinks_xml = hl_elements.empty? ? "" : "<#{ws_prefix}hyperlinks>#{hl_elements.join}</#{ws_prefix}hyperlinks>"
|
|
3236
|
-
|
|
3237
|
-
if updated_sheet_xml =~ /<([a-zA-Z0-9_]+:)?hyperlinks\b/
|
|
3238
|
-
updated_sheet_xml.sub!(%r{<(([a-zA-Z0-9_]+:)?hyperlinks)\b(?:[^>]*/>|[^>]*>.*?</\1>)}m) { new_hyperlinks_xml }
|
|
3239
|
-
elsif hl_elements.any?
|
|
3240
|
-
search_start = 0
|
|
3241
|
-
%w[sheetData customSheetViews mergeCells conditionalFormatting dataValidations].each do |tag|
|
|
3242
|
-
pattern = if ws_prefix.empty?
|
|
3243
|
-
%r{</#{tag}>|<#{tag}\b[^>]*/\s*>}
|
|
3244
|
-
else
|
|
3245
|
-
%r{</#{Regexp.escape(ws_prefix)}#{tag}>|<#{Regexp.escape(ws_prefix)}#{tag}\b[^>]*/\s*>}
|
|
3246
|
-
end
|
|
3247
|
-
if (m = updated_sheet_xml.match(pattern))
|
|
3248
|
-
end_pos = m.end(0)
|
|
3249
|
-
search_start = end_pos if end_pos > search_start
|
|
3250
|
-
end
|
|
3251
|
-
end
|
|
3252
|
-
|
|
3253
|
-
tags_sub = "printOptions|pageMargins|pageSetup|headerFooter|rowBreaks|colBreaks|customProperties|cellWatches|ignoredErrors|smartTags|drawing|legacyDrawing|legacyDrawingHF|picture|oleObjects|controls|webPublishItems|tableParts|extLst"
|
|
3254
|
-
insert_pattern = if ws_prefix.empty?
|
|
3255
|
-
%r{<(?:#{tags_sub})\b|</worksheet>}
|
|
3256
|
-
else
|
|
3257
|
-
%r{<#{Regexp.escape(ws_prefix)}(?:#{tags_sub})\b|</#{Regexp.escape(ws_prefix)}worksheet>}
|
|
3258
|
-
end
|
|
3259
|
-
match = updated_sheet_xml.match(insert_pattern, search_start)
|
|
3260
|
-
if match
|
|
3261
|
-
updated_sheet_xml.insert(match.begin(0), new_hyperlinks_xml)
|
|
3262
|
-
else
|
|
3263
|
-
updated_sheet_xml << new_hyperlinks_xml
|
|
3264
|
-
end
|
|
3265
|
-
end
|
|
3266
|
-
|
|
3267
|
-
updated_rels_xml = if sheet_rels.empty? && (orig_rels_xml.nil? || orig_rels_xml.empty?)
|
|
3268
|
-
nil
|
|
3269
|
-
else
|
|
3270
|
-
io = StringIO.new
|
|
3271
|
-
b = Ooxml::XmlBuilder.new(io)
|
|
3272
|
-
b.declaration
|
|
3273
|
-
b.open_tag("Relationships", { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" })
|
|
3274
|
-
sheet_rels.each do |rel|
|
|
3275
|
-
rel_attrs = { Id: rel[:id], Type: rel[:type], Target: rel[:target] }
|
|
3276
|
-
rel_attrs[:TargetMode] = rel[:target_mode] if rel[:target_mode]
|
|
3277
|
-
b.empty_tag("Relationship", rel_attrs)
|
|
3278
|
-
end
|
|
3279
|
-
b.close_tag("Relationships")
|
|
3280
|
-
io.string
|
|
3281
|
-
end
|
|
3282
|
-
|
|
3283
|
-
[updated_sheet_xml, updated_rels_xml]
|
|
3284
|
-
end
|
|
3285
|
-
|
|
3286
|
-
#: (String sheet_xml, String _rels_path, String? orig_rels_xml, Array[Hash[Symbol, untyped]] new_comments, Ooxml::ZipReader zip_reader, Integer sheet_idx, String entry_name, Hash[String, String] replaced_entries) -> [String, String?]
|
|
3287
|
-
def update_sheet_comments(sheet_xml, _rels_path, orig_rels_xml, new_comments, zip_reader, sheet_idx, entry_name, replaced_entries)
|
|
3288
|
-
existing_rels = []
|
|
3289
|
-
max_rid_num = 0
|
|
3290
|
-
if orig_rels_xml && !orig_rels_xml.empty?
|
|
3291
|
-
orig_rels_xml.scan(%r{<Relationship\b([^>]*)/?>}) do |m|
|
|
3292
|
-
attrs = m[0]
|
|
3293
|
-
id = attrs[/Id="([^"]*)"/, 1]
|
|
3294
|
-
type = attrs[/Type="([^"]*)"/, 1]
|
|
3295
|
-
target = attrs[/Target="([^"]*)"/, 1]
|
|
3296
|
-
target_mode = attrs[/TargetMode="([^"]*)"/, 1]
|
|
3297
|
-
if id =~ /\ArId(\d+)\z/
|
|
3298
|
-
num = Regexp.last_match(1).to_i
|
|
3299
|
-
max_rid_num = num if num > max_rid_num
|
|
3300
|
-
end
|
|
3301
|
-
existing_rels << { id: id, type: type, target: target, target_mode: target_mode }
|
|
3302
|
-
end
|
|
3303
|
-
end
|
|
3304
|
-
|
|
3305
|
-
comments_rel = existing_rels.find { |r| r[:type]&.end_with?("/comments") }
|
|
3306
|
-
vml_rel = existing_rels.find { |r| r[:type]&.end_with?("/vmlDrawing") }
|
|
3307
|
-
|
|
3308
|
-
writer = Ooxml::Writer.new
|
|
3309
|
-
new_comments.each do |c|
|
|
3310
|
-
writer.add_comment(c[:cell] || c[:ref], c[:text], author: c[:author] || "Author")
|
|
3311
|
-
end
|
|
3312
|
-
comments_xml = writer.send(:generate_comments_xml, writer.comments)
|
|
3313
|
-
vml_xml = writer.send(:generate_vml_drawing_xml, writer.comments)
|
|
3314
|
-
|
|
3315
|
-
updated_sheet_xml = sheet_xml.dup
|
|
3316
|
-
|
|
3317
|
-
if comments_rel
|
|
3318
|
-
base_dir = File.dirname(entry_name)
|
|
3319
|
-
comments_path = resolve_relative_path(base_dir, comments_rel[:target])
|
|
3320
|
-
replaced_entries[comments_path] = comments_xml
|
|
3321
|
-
if vml_rel
|
|
3322
|
-
vml_path = resolve_relative_path(base_dir, vml_rel[:target])
|
|
3323
|
-
replaced_entries[vml_path] = vml_xml
|
|
3324
|
-
end
|
|
3325
|
-
elsif new_comments.any?
|
|
3326
|
-
existing_comment_count = zip_reader.entry_names.count { |n| n =~ %r{\Axl/comments\d+\.xml\z} }
|
|
3327
|
-
comment_file_num = existing_comment_count + sheet_idx + 1
|
|
3328
|
-
comments_path = "xl/comments#{comment_file_num}.xml"
|
|
3329
|
-
vml_path = "xl/drawings/vmlDrawing#{comment_file_num}.vml"
|
|
3330
|
-
replaced_entries[comments_path] = comments_xml
|
|
3331
|
-
replaced_entries[vml_path] = vml_xml
|
|
3332
|
-
|
|
3333
|
-
max_rid_num += 1
|
|
3334
|
-
c_rid = "rId#{max_rid_num}"
|
|
3335
|
-
existing_rels << {
|
|
3336
|
-
id: c_rid,
|
|
3337
|
-
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
|
|
3338
|
-
target: "../comments#{comment_file_num}.xml"
|
|
3339
|
-
}
|
|
3340
|
-
|
|
3341
|
-
max_rid_num += 1
|
|
3342
|
-
vml_rid = "rId#{max_rid_num}"
|
|
3343
|
-
existing_rels << {
|
|
3344
|
-
id: vml_rid,
|
|
3345
|
-
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
|
|
3346
|
-
target: "../drawings/vmlDrawing#{comment_file_num}.vml"
|
|
3347
|
-
}
|
|
3348
|
-
|
|
3349
|
-
ws_prefix = updated_sheet_xml[/<([a-zA-Z0-9_]+:)?worksheet\b/, 1] || ""
|
|
3350
|
-
legacy_pattern = ws_prefix.empty? ? /<legacyDrawing\b/ : /<#{Regexp.escape(ws_prefix)}legacyDrawing\b/
|
|
3351
|
-
unless updated_sheet_xml =~ legacy_pattern
|
|
3352
|
-
legacy_tag = %(<#{ws_prefix}legacyDrawing r:id="#{vml_rid}"/>)
|
|
3353
|
-
search_start = 0
|
|
3354
|
-
%w[sheetData customSheetViews mergeCells conditionalFormatting dataValidations hyperlinks pageMargins pageSetup].each do |tag|
|
|
3355
|
-
pattern = if ws_prefix.empty?
|
|
3356
|
-
%r{</#{tag}>|<#{tag}\b[^>]*/\s*>}
|
|
3357
|
-
else
|
|
3358
|
-
%r{</#{Regexp.escape(ws_prefix)}#{tag}>|<#{Regexp.escape(ws_prefix)}#{tag}\b[^>]*/\s*>}
|
|
3359
|
-
end
|
|
3360
|
-
if (m = updated_sheet_xml.match(pattern))
|
|
3361
|
-
end_pos = m.end(0)
|
|
3362
|
-
search_start = end_pos if end_pos > search_start
|
|
3363
|
-
end
|
|
3364
|
-
end
|
|
3365
|
-
|
|
3366
|
-
tags_sub = "legacyDrawingHF|picture|oleObjects|controls|webPublishItems|tableParts|extLst"
|
|
3367
|
-
insert_pattern = if ws_prefix.empty?
|
|
3368
|
-
%r{<(?:#{tags_sub})\b|</worksheet>}
|
|
3369
|
-
else
|
|
3370
|
-
%r{<#{Regexp.escape(ws_prefix)}(?:#{tags_sub})\b|</#{Regexp.escape(ws_prefix)}worksheet>}
|
|
3371
|
-
end
|
|
3372
|
-
match = updated_sheet_xml.match(insert_pattern, search_start)
|
|
3373
|
-
if match
|
|
3374
|
-
updated_sheet_xml.insert(match.begin(0), legacy_tag)
|
|
3375
|
-
else
|
|
3376
|
-
updated_sheet_xml << legacy_tag
|
|
3377
|
-
end
|
|
3378
|
-
end
|
|
3379
|
-
|
|
3380
|
-
root_tag = updated_sheet_xml[/\A\s*(?:<\?xml[^>]*\?>\s*)?<([a-zA-Z0-9_]+:)?worksheet\b[^>]*/, 0]
|
|
3381
|
-
updated_sheet_xml.sub!(/<([a-zA-Z0-9_]+:)?worksheet\b/, '\0 xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"') if root_tag.nil? || !root_tag.match?(/\bxmlns:r\s*=/)
|
|
3382
|
-
|
|
3383
|
-
ct_xml = replaced_entries["[Content_Types].xml"] || zip_reader.read_entry("[Content_Types].xml")
|
|
3384
|
-
if ct_xml && !ct_xml.include?("comments#{comment_file_num}.xml")
|
|
3385
|
-
override_tag = %(<Override PartName="/#{comments_path}" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml"/>)
|
|
3386
|
-
ct_xml = ct_xml.sub(%r{</Types>}, "#{override_tag}</Types>")
|
|
3387
|
-
unless ct_xml.include?('Extension="vml"')
|
|
3388
|
-
default_vml = %(<Default Extension="vml" ContentType="application/vnd.openxmlformats-officedocument.vmlDrawing"/>)
|
|
3389
|
-
ct_xml = ct_xml.sub(%r{</Types>}, "#{default_vml}</Types>")
|
|
3390
|
-
end
|
|
3391
|
-
replaced_entries["[Content_Types].xml"] = ct_xml
|
|
3392
|
-
end
|
|
3393
|
-
end
|
|
3394
|
-
|
|
3395
|
-
io = StringIO.new
|
|
3396
|
-
b = Ooxml::XmlBuilder.new(io)
|
|
3397
|
-
b.declaration
|
|
3398
|
-
b.open_tag("Relationships", { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" })
|
|
3399
|
-
existing_rels.each do |rel|
|
|
3400
|
-
rel_attrs = { Id: rel[:id], Type: rel[:type], Target: rel[:target] }
|
|
3401
|
-
rel_attrs[:TargetMode] = rel[:target_mode] if rel[:target_mode]
|
|
3402
|
-
b.empty_tag("Relationship", rel_attrs)
|
|
3403
|
-
end
|
|
3404
|
-
b.close_tag("Relationships")
|
|
3405
|
-
|
|
3406
|
-
[updated_sheet_xml, io.string]
|
|
3407
|
-
end
|
|
3408
|
-
|
|
3409
|
-
#: (Array[Elements::Row] rows, Array[untyped] shared_strings, Hash[untyped, Integer] sst_index, ?use_sst: bool, ?date1904: bool) -> String
|
|
3410
|
-
def serialize_sheet_data_xml(rows, shared_strings, sst_index, use_sst: true, date1904: false)
|
|
3411
|
-
io = StringIO.new
|
|
3412
|
-
io.write("<sheetData>")
|
|
3413
|
-
ws = Ooxml::WorksheetWriter.new(io, date1904: date1904)
|
|
3414
|
-
ws.instance_variable_set(:@started, true)
|
|
3415
|
-
|
|
3416
|
-
rows.each do |row|
|
|
3417
|
-
attrs = {}
|
|
3418
|
-
attrs[:height] = row.height if row.height
|
|
3419
|
-
attrs[:hidden] = true if row.hidden
|
|
3420
|
-
attrs[:custom_height] = true if row.custom_height
|
|
3421
|
-
attrs[:outline_level] = row.outline_level if row.outline_level
|
|
3422
|
-
|
|
3423
|
-
if use_sst
|
|
3424
|
-
row.cells.each do |cell|
|
|
3425
|
-
val = cell.value
|
|
3426
|
-
next unless val.is_a?(String) || val.is_a?(Elements::RichText)
|
|
3427
|
-
next if sst_index.key?(val)
|
|
3428
|
-
|
|
3429
|
-
shared_strings << val
|
|
3430
|
-
sst_index[val] = shared_strings.size - 1
|
|
3431
|
-
end
|
|
3432
|
-
end
|
|
3433
|
-
|
|
3434
|
-
ws.write_row(
|
|
3435
|
-
row.index,
|
|
3436
|
-
row.cells,
|
|
3437
|
-
attrs: attrs,
|
|
3438
|
-
unmapped: row.unmapped_data || [],
|
|
3439
|
-
sst_index: use_sst ? sst_index : nil
|
|
3440
|
-
)
|
|
3441
|
-
end
|
|
3442
|
-
|
|
3443
|
-
buf = ws.instance_variable_get(:@row_buffer)
|
|
3444
|
-
if buf && !buf.empty?
|
|
3445
|
-
io.write(buf)
|
|
3446
|
-
buf.clear
|
|
3447
|
-
end
|
|
3448
|
-
io.write("</sheetData>")
|
|
3449
|
-
io.string
|
|
3450
|
-
end
|
|
3451
2634
|
end
|
|
3452
2635
|
end
|