xlsxrb 0.1.18 → 0.2.1
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 +200 -0
- data/README.md +35 -12
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
- data/lib/xlsxrb/chart_builder.rb +251 -386
- data/lib/xlsxrb/dsl_helpers.rb +169 -68
- 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 +45 -316
- 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 +15 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +184 -94
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +353 -261
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +62 -16
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
- data/lib/xlsxrb/ooxml/writer.rb +106 -42
- data/lib/xlsxrb/ooxml/xml_builder.rb +18 -0
- 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 +14 -118
- data/lib/xlsxrb/stream_sheet.rb +14 -261
- data/lib/xlsxrb/stream_writer.rb +807 -1537
- data/lib/xlsxrb/style_builder.rb +715 -593
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +403 -286
- data/lib/xlsxrb/worksheet_builder.rb +1297 -1458
- data/lib/xlsxrb.rb +281 -1079
- data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
- data/sig/generated/xlsxrb/dsl_helpers.rbs +92 -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 +13 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +17 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +58 -3
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +12 -0
- 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 +9 -44
- data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
- data/sig/generated/xlsxrb/stream_writer.rbs +491 -974
- data/sig/generated/xlsxrb/style_builder.rbs +464 -239
- data/sig/generated/xlsxrb/workbook_builder.rbs +124 -139
- data/sig/generated/xlsxrb/worksheet_builder.rbs +922 -686
- data/sig/generated/xlsxrb.rbs +56 -188
- 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,35 +49,49 @@ 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
|
|
|
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
|
|
76
|
+
|
|
77
|
+
# Base class for all exceptions raised by Xlsxrb.
|
|
78
|
+
class Error < StandardError; end
|
|
79
|
+
|
|
60
80
|
# @api private
|
|
61
81
|
module Deprecation
|
|
82
|
+
LIB_DIR = File.expand_path(__dir__).freeze
|
|
83
|
+
private_constant :LIB_DIR
|
|
84
|
+
|
|
85
|
+
# Warns that a feature is deprecated, at the location +uplevel+ frames above the caller,
|
|
86
|
+
# or, with +uplevel: nil+, at the first caller outside this library, for methods that
|
|
87
|
+
# are reached both directly and through another xlsxrb method.
|
|
88
|
+
#: (String message, ?uplevel: Integer?) -> void
|
|
62
89
|
def self.warn(message, uplevel: 1)
|
|
90
|
+
uplevel ||= caller_locations(1)&.index { |location| !location.absolute_path.to_s.start_with?(LIB_DIR) } || 0
|
|
63
91
|
Kernel.warn("[Xlsxrb DEPRECATION] #{message}", uplevel: uplevel + 1)
|
|
64
92
|
end
|
|
65
93
|
end
|
|
66
94
|
|
|
67
|
-
# Base class for all exceptions raised by Xlsxrb.
|
|
68
|
-
# Predefined cell error constants are exposed on this class for convenience.
|
|
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
|
|
81
|
-
|
|
82
95
|
# Raised when parsing malformed or invalid XML structures.
|
|
83
96
|
class ParseError < Error; end
|
|
84
97
|
|
|
@@ -214,19 +227,16 @@ module Xlsxrb
|
|
|
214
227
|
# @param type [Symbol, String, nil] Formula type (:shared, :array, etc.).
|
|
215
228
|
# @param ref [String, nil] Target cell or range reference (e.g. "A1:C1").
|
|
216
229
|
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
217
|
-
# @param si [Integer, nil] Alias for shared_index.
|
|
218
230
|
# @return [Elements::Formula]
|
|
219
231
|
# @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
|
|
232
|
+
#: (String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?) -> Elements::Formula
|
|
233
|
+
def self.formula(expression, cached_value: nil, type: nil, ref: nil, shared_index: nil)
|
|
224
234
|
Elements::Formula.new(
|
|
225
235
|
expression: expression,
|
|
226
236
|
cached_value: cached_value,
|
|
227
|
-
type: type || (
|
|
237
|
+
type: type || (shared_index ? :shared : nil),
|
|
228
238
|
ref: ref,
|
|
229
|
-
shared_index:
|
|
239
|
+
shared_index: shared_index,
|
|
230
240
|
calculate_always: cached_value.nil? || nil
|
|
231
241
|
)
|
|
232
242
|
end
|
|
@@ -241,52 +251,44 @@ module Xlsxrb
|
|
|
241
251
|
#
|
|
242
252
|
# @param expression [String, nil] The formula expression (nil for follower cells).
|
|
243
253
|
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
244
|
-
# @param si [Integer, nil] Legacy alias for shared_index.
|
|
245
254
|
# @param ref [String, nil] Target range for master cell (e.g. "B2:B10").
|
|
246
255
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
247
256
|
# @return [Elements::Formula]
|
|
248
257
|
# @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?
|
|
258
|
+
#: (?String? expression, ?shared_index: Integer?, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil)) -> Elements::Formula
|
|
259
|
+
def self.shared_formula(expression = nil, shared_index: nil, ref: nil, cached_value: nil)
|
|
260
|
+
raise ArgumentError, "shared_index is required for shared formula" if shared_index.nil?
|
|
254
261
|
|
|
255
262
|
Elements::Formula.new(
|
|
256
263
|
expression: expression || "",
|
|
257
264
|
cached_value: cached_value,
|
|
258
265
|
type: :shared,
|
|
259
266
|
ref: ref,
|
|
260
|
-
shared_index:
|
|
267
|
+
shared_index: shared_index,
|
|
261
268
|
calculate_always: cached_value.nil? || nil
|
|
262
269
|
)
|
|
263
270
|
end
|
|
264
|
-
# rubocop:enable Naming/MethodParameterName
|
|
265
271
|
|
|
266
272
|
# Creates an array or dynamic array {Elements::Formula} object.
|
|
267
273
|
#
|
|
268
274
|
# @example Create a multi-cell array formula
|
|
269
|
-
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", "A1:C1", cached_value: 0)
|
|
270
275
|
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", ref: "A1:C1", cached_value: 0)
|
|
271
276
|
#
|
|
272
277
|
# @example Create a dynamic array formula
|
|
273
|
-
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", "D1")
|
|
278
|
+
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", ref: "D1")
|
|
274
279
|
#
|
|
275
280
|
# @param expression [String] The formula expression without leading '='.
|
|
276
|
-
# @param pos_ref [String, nil] Target cell or range reference (positional).
|
|
277
281
|
# @param ref [String, nil] Target cell or range reference (keyword).
|
|
278
282
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
279
283
|
# @return [Elements::Formula]
|
|
280
284
|
# @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
|
|
285
|
+
#: (String expression, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) -> Elements::Formula
|
|
286
|
+
def self.array_formula(expression, ref: nil, cached_value: nil, **attrs)
|
|
285
287
|
Elements::Formula.new(
|
|
286
288
|
expression: expression,
|
|
287
289
|
cached_value: cached_value,
|
|
288
290
|
type: :array,
|
|
289
|
-
ref:
|
|
291
|
+
ref: ref,
|
|
290
292
|
calculate_always: cached_value.nil? || nil,
|
|
291
293
|
**attrs
|
|
292
294
|
)
|
|
@@ -367,13 +369,6 @@ module Xlsxrb
|
|
|
367
369
|
NumberFormatter.format(value, format_code, date1904: date1904)
|
|
368
370
|
end
|
|
369
371
|
|
|
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
372
|
# Converts a Date or Time to an Excel numeric serial number (1900 or 1904 system).
|
|
378
373
|
#
|
|
379
374
|
# @example
|
|
@@ -385,7 +380,7 @@ module Xlsxrb
|
|
|
385
380
|
# @api public
|
|
386
381
|
#: (Date | Time date_or_time, ?date1904: bool) -> (Integer | Float)
|
|
387
382
|
def self.date_to_serial(date_or_time, date1904: false)
|
|
388
|
-
if date_or_time.is_a?(Time)
|
|
383
|
+
if date_or_time.is_a?(Time) || date_or_time.is_a?(DateTime)
|
|
389
384
|
Ooxml::Utils.datetime_to_serial(date_or_time, date1904: date1904)
|
|
390
385
|
else
|
|
391
386
|
Ooxml::Utils.date_to_serial(date_or_time, date1904: date1904)
|
|
@@ -432,20 +427,13 @@ module Xlsxrb
|
|
|
432
427
|
# @api public
|
|
433
428
|
#: (Time | Date time, ?date1904: bool) -> Float
|
|
434
429
|
def self.time_to_serial(time, date1904: false)
|
|
435
|
-
if time.is_a?(Time)
|
|
430
|
+
if time.is_a?(Time) || time.is_a?(DateTime)
|
|
436
431
|
Ooxml::Utils.datetime_to_serial(time, date1904: date1904)
|
|
437
432
|
else
|
|
438
433
|
Ooxml::Utils.date_to_serial(time, date1904: date1904).to_f
|
|
439
434
|
end
|
|
440
435
|
end
|
|
441
436
|
|
|
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
437
|
# Converts an Excel serial number to a Time (UTC).
|
|
450
438
|
#
|
|
451
439
|
# @example
|
|
@@ -460,13 +448,6 @@ module Xlsxrb
|
|
|
460
448
|
Ooxml::Utils.serial_to_datetime(serial, date1904: date1904)
|
|
461
449
|
end
|
|
462
450
|
|
|
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
451
|
# Converts a cell reference string (e.g. "A1", "BC100") to 0-based [row, col] coordinates.
|
|
471
452
|
#
|
|
472
453
|
# @example
|
|
@@ -613,27 +594,21 @@ module Xlsxrb
|
|
|
613
594
|
# Expands a formula by shifting relative cell references from a master cell to a target cell or by offset amounts.
|
|
614
595
|
#
|
|
615
596
|
# @example Shift formula down one row
|
|
616
|
-
# Xlsxrb.expand_formula("A2*2", 1, 0) #=> "A3*2"
|
|
597
|
+
# Xlsxrb.expand_formula("A2*2", row_delta: 1, column_delta: 0) #=> "A3*2"
|
|
617
598
|
# Xlsxrb.expand_formula("A2*2", from_ref: "B2", to_ref: "B3") #=> "A3*2"
|
|
618
599
|
#
|
|
619
600
|
# @example Shift formula with absolute references
|
|
620
|
-
# Xlsxrb.expand_formula("$A$1+A2", 2, 0) #=> "$A$1+A4"
|
|
601
|
+
# Xlsxrb.expand_formula("$A$1+A2", row_delta: 2, column_delta: 0) #=> "$A$1+A4"
|
|
621
602
|
#
|
|
622
603
|
# @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
604
|
# @param from_ref [String, nil] Master cell reference (e.g. "B2").
|
|
626
605
|
# @param to_ref [String, nil] Target cell reference (e.g. "B3").
|
|
627
606
|
# @param row_delta [Integer, nil] Row shift amount (positive = down, negative = up).
|
|
628
607
|
# @param column_delta [Integer, nil] Column shift amount (positive = right, negative = left).
|
|
629
|
-
# @param col_delta [Integer, nil] Deprecated alias for column_delta.
|
|
630
608
|
# @return [String, nil] Expanded formula expression.
|
|
631
609
|
# @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
|
-
|
|
610
|
+
#: (String? formula, ?from_ref: String?, ?to_ref: String?, ?row_delta: Integer?, ?column_delta: Integer?) -> String?
|
|
611
|
+
def self.expand_formula(formula, from_ref: nil, to_ref: nil, row_delta: nil, column_delta: nil)
|
|
637
612
|
return formula if formula.nil? || formula.empty?
|
|
638
613
|
|
|
639
614
|
if from_ref && to_ref
|
|
@@ -644,8 +619,8 @@ module Xlsxrb
|
|
|
644
619
|
rd = t_coords[0] - f_coords[0]
|
|
645
620
|
cd = t_coords[1] - f_coords[1]
|
|
646
621
|
else
|
|
647
|
-
rd = row_delta ||
|
|
648
|
-
cd = column_delta ||
|
|
622
|
+
rd = row_delta || 0
|
|
623
|
+
cd = column_delta || 0
|
|
649
624
|
end
|
|
650
625
|
|
|
651
626
|
return formula if rd.zero? && cd.zero?
|
|
@@ -712,13 +687,6 @@ module Xlsxrb
|
|
|
712
687
|
Colors.to_hex(color, alpha: alpha)
|
|
713
688
|
end
|
|
714
689
|
|
|
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
690
|
# Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters.
|
|
723
691
|
# Replaces forbidden characters (\ / ? * [ ] : and control characters),
|
|
724
692
|
# replaces leading/trailing apostrophes, and replaces reserved 'History' with 'History_'.
|
|
@@ -736,38 +704,11 @@ module Xlsxrb
|
|
|
736
704
|
DslHelpers.sanitize_sheet_name(name, replace_char: replace_char)
|
|
737
705
|
end
|
|
738
706
|
|
|
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
707
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
747
708
|
#
|
|
748
709
|
# 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]
|
|
710
|
+
# yields each {StreamSheet} sequentially. Call {StreamSheet#load} to convert a sheet to
|
|
711
|
+
# an in-memory representation for coordinate random access (`sheet["A1"]`).
|
|
771
712
|
#
|
|
772
713
|
# @example Streaming read across sheets and rows (O(1) constant memory)
|
|
773
714
|
# Xlsxrb.read("large.xlsx") do |sheet|
|
|
@@ -777,51 +718,84 @@ module Xlsxrb
|
|
|
777
718
|
# end
|
|
778
719
|
# end
|
|
779
720
|
#
|
|
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.
|
|
721
|
+
# @param source [String, IO, StringIO] File path, binary content string, or readable IO stream.
|
|
722
|
+
# @param password [String, nil] Optional password to decrypt password-protected XLSX files.
|
|
723
|
+
# @param trim_empty_rows [Boolean] Whether to omit trailing empty rows during sheet iteration.
|
|
724
|
+
# @param pad_empty_rows [Boolean] Whether to yield empty rows for skipped row numbers.
|
|
725
|
+
# @param pad_empty_cells [Boolean] Whether to pad missing cells within rows.
|
|
791
726
|
# @param max_uncompressed_size [Integer, nil] Maximum allowed uncompressed size in bytes per entry (ZIP bomb defense).
|
|
727
|
+
# @yield [sheet]
|
|
728
|
+
# @yieldparam sheet [StreamSheet] The streaming worksheet object.
|
|
729
|
+
# @return [Enumerator<StreamSheet>, void]
|
|
792
730
|
# @raise [ZipBombError] If an uncompressed entry exceeds max_uncompressed_size.
|
|
793
731
|
# @raise [EncryptedFileError] If the file is encrypted and no password was supplied.
|
|
794
732
|
# @raise [InvalidPasswordError] If the supplied password is incorrect.
|
|
795
733
|
# @raise [ParseError] If the spreadsheet XML structure is invalid.
|
|
796
734
|
# @api public
|
|
797
735
|
#: (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?) ->
|
|
736
|
+
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Enumerator[StreamSheet, void]
|
|
799
737
|
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
|
-
|
|
738
|
+
prepared_source = prepare_source_io(source, password)
|
|
739
|
+
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?
|
|
740
|
+
|
|
741
|
+
zip_options = {}
|
|
742
|
+
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
743
|
+
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
744
|
+
|
|
745
|
+
begin
|
|
746
|
+
workbook_path = resolve_workbook_entry(zip_reader)
|
|
747
|
+
wb_dir = File.dirname(workbook_path)
|
|
748
|
+
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
749
|
+
rels_xml = zip_reader.read_entry(wb_rels_name)
|
|
750
|
+
rels, rel_types = parse_workbook_relationships(rels_xml)
|
|
751
|
+
|
|
752
|
+
sst_entry = resolve_part_entry(zip_reader, rels, "sharedStrings", "sharedStrings.xml", wb_dir)
|
|
753
|
+
shared_strings_xml = sst_entry ? zip_reader.read_entry(sst_entry) : nil
|
|
754
|
+
shared_strings = shared_strings_xml ? Ooxml::SharedStringsParser.parse(shared_strings_xml) : []
|
|
755
|
+
|
|
756
|
+
styles_entry = resolve_part_entry(zip_reader, rels, "styles", "styles.xml", wb_dir)
|
|
757
|
+
styles_xml = styles_entry ? zip_reader.read_entry(styles_entry) : nil
|
|
758
|
+
styles = styles_xml ? Ooxml::StylesParser.parse(styles_xml) : {}
|
|
759
|
+
|
|
760
|
+
workbook_xml = zip_reader.read_entry(workbook_path)
|
|
761
|
+
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
762
|
+
workbook_sheets = wb_parsed[:sheets]
|
|
763
|
+
is_date1904 = wb_parsed[:date1904]
|
|
764
|
+
|
|
765
|
+
ws_idx = 0
|
|
766
|
+
workbook_sheets.each do |sheet_info|
|
|
767
|
+
r_id = sheet_info[:r_id]
|
|
768
|
+
target = rels[r_id]
|
|
769
|
+
rel_type = rel_types[r_id]
|
|
770
|
+
next if non_worksheet_relationship?(rel_type, target)
|
|
771
|
+
|
|
772
|
+
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
773
|
+
current_sheet_idx = ws_idx
|
|
774
|
+
ws_idx += 1
|
|
775
|
+
next unless sheet_path
|
|
776
|
+
|
|
777
|
+
chunk_supplier = ->(&blk) { zip_reader.each_entry_chunk(sheet_path, &blk) }
|
|
778
|
+
|
|
779
|
+
stream_sheet = StreamSheet.new(
|
|
780
|
+
sheet_info[:name],
|
|
781
|
+
chunk_supplier,
|
|
782
|
+
shared_strings,
|
|
783
|
+
styles,
|
|
784
|
+
zip_reader: zip_reader,
|
|
785
|
+
entry_name: sheet_path,
|
|
786
|
+
state: sheet_info[:state] || :visible,
|
|
787
|
+
date1904: is_date1904,
|
|
788
|
+
trim_empty_rows: trim_empty_rows,
|
|
789
|
+
pad_empty_rows: pad_empty_rows,
|
|
790
|
+
pad_empty_cells: pad_empty_cells,
|
|
791
|
+
defined_names: wb_parsed[:defined_names] || [],
|
|
792
|
+
sheet_index: current_sheet_idx
|
|
793
|
+
)
|
|
794
|
+
yield stream_sheet
|
|
814
795
|
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
|
-
)
|
|
796
|
+
nil
|
|
797
|
+
ensure
|
|
798
|
+
zip_reader&.close
|
|
825
799
|
end
|
|
826
800
|
end
|
|
827
801
|
|
|
@@ -911,7 +885,7 @@ module Xlsxrb
|
|
|
911
885
|
wb_props = {}
|
|
912
886
|
wb_props[:date1904] = true if is_date1904
|
|
913
887
|
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
914
|
-
wb_props[:
|
|
888
|
+
wb_props[:active_sheet_index] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
915
889
|
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
916
890
|
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
917
891
|
wb_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
@@ -924,8 +898,16 @@ module Xlsxrb
|
|
|
924
898
|
prot_match = workbook_xml.match(%r{<workbookProtection\b([^>]*)/>})
|
|
925
899
|
if prot_match
|
|
926
900
|
prot = {}
|
|
927
|
-
|
|
928
|
-
|
|
901
|
+
prot_match[1].scan(/([A-Za-z]+)="([^"]*)"/) do |attr, value|
|
|
902
|
+
key, kind = WORKBOOK_PROTECTION_ATTRIBUTES[attr]
|
|
903
|
+
next unless key
|
|
904
|
+
|
|
905
|
+
prot[key] = case kind
|
|
906
|
+
when :bool then %w[1 true].include?(value)
|
|
907
|
+
when :int then value.to_i
|
|
908
|
+
else value
|
|
909
|
+
end
|
|
910
|
+
end
|
|
929
911
|
wb_unmapped[:workbook_protection] = prot unless prot.empty?
|
|
930
912
|
end
|
|
931
913
|
end
|
|
@@ -963,130 +945,6 @@ module Xlsxrb
|
|
|
963
945
|
end
|
|
964
946
|
end
|
|
965
947
|
|
|
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
948
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
1091
949
|
#
|
|
1092
950
|
# @overload write(?target, password: nil, encryption_mode: :standard, strict_excel_mode: true, &block)
|
|
@@ -1138,16 +996,16 @@ module Xlsxrb
|
|
|
1138
996
|
# When set to `false`, strings are emitted directly as inline strings (`<c t="inlineStr">`), providing flat
|
|
1139
997
|
# $O(1)$ constant memory during streaming write. Disabling shared strings is recommended when streaming
|
|
1140
998
|
# 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,
|
|
999
|
+
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION
|
|
1000
|
+
# for zlib's level 6). Defaults to {DEFAULT_COMPRESSION_LEVEL}. Higher levels write smaller files more slowly;
|
|
1001
|
+
# level 0 stores entries without compression. See docs/adr/0009-default-zip-compression-level.md.
|
|
1002
|
+
#: (?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
|
|
1003
|
+
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
1004
|
if block_given?
|
|
1147
1005
|
if target_or_workbook.equal?(OMITTED_TARGET)
|
|
1148
1006
|
io = StringIO.new
|
|
1149
1007
|
io.binmode
|
|
1150
|
-
write(io, password: password, encryption_mode: encryption_mode, strict_excel_mode: strict_excel_mode,
|
|
1008
|
+
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
1009
|
return io.string.b
|
|
1152
1010
|
end
|
|
1153
1011
|
|
|
@@ -1157,7 +1015,7 @@ module Xlsxrb
|
|
|
1157
1015
|
if password && !password.empty?
|
|
1158
1016
|
buf = StringIO.new
|
|
1159
1017
|
buf.binmode
|
|
1160
|
-
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode,
|
|
1018
|
+
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
1161
1019
|
begin
|
|
1162
1020
|
yield stream_writer
|
|
1163
1021
|
stream_writer.close
|
|
@@ -1172,7 +1030,7 @@ module Xlsxrb
|
|
|
1172
1030
|
stream_writer.cleanup!
|
|
1173
1031
|
end
|
|
1174
1032
|
else
|
|
1175
|
-
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode,
|
|
1033
|
+
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
1176
1034
|
begin
|
|
1177
1035
|
yield stream_writer
|
|
1178
1036
|
stream_writer.close
|
|
@@ -1237,7 +1095,7 @@ module Xlsxrb
|
|
|
1237
1095
|
style_idx = col.unmapped_data&.[](:style_index) || col.unmapped_data&.[](:style) || (col.respond_to?(:style_index) && col.style_index)
|
|
1238
1096
|
min = col.unmapped_data&.[](:min)
|
|
1239
1097
|
max = col.unmapped_data&.[](:max)
|
|
1240
|
-
{ index: col.index, width: col.width, hidden: col.hidden
|
|
1098
|
+
{ 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
1099
|
# simplecov:enable
|
|
1242
1100
|
end
|
|
1243
1101
|
sd = { name: ws.name, rows: ws.rows, columns: columns }
|
|
@@ -1273,7 +1131,13 @@ module Xlsxrb
|
|
|
1273
1131
|
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
1132
|
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
1133
|
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
|
-
|
|
1134
|
+
if ws.respond_to?(:selection) && ws.selection
|
|
1135
|
+
sd[:selection] = if ws.selection.is_a?(Array)
|
|
1136
|
+
ws.selection.map { |sel| sel.respond_to?(:to_h) ? sel.to_h : sel }
|
|
1137
|
+
else
|
|
1138
|
+
ws.selection.respond_to?(:to_h) ? ws.selection.to_h : ws.selection
|
|
1139
|
+
end
|
|
1140
|
+
end
|
|
1277
1141
|
sd[:selection] ||= ws.sheet_view[:selection] if ws.respond_to?(:sheet_view) && ws.sheet_view.is_a?(Hash) && ws.sheet_view[:selection]
|
|
1278
1142
|
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
1143
|
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 +1147,7 @@ module Xlsxrb
|
|
|
1283
1147
|
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
1148
|
sd[:merge_cells] = ws.merged_cells if ws.respond_to?(:merged_cells) && ws.merged_cells && !ws.merged_cells.empty?
|
|
1285
1149
|
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?(:
|
|
1150
|
+
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
1151
|
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
1152
|
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
1153
|
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 +1158,6 @@ module Xlsxrb
|
|
|
1294
1158
|
end
|
|
1295
1159
|
if ws.respond_to?(:column_breaks) && ws.column_breaks && !ws.column_breaks.empty?
|
|
1296
1160
|
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
1161
|
end
|
|
1300
1162
|
sd[:background_image] = ws.background_image if ws.respond_to?(:background_image) && ws.background_image
|
|
1301
1163
|
if ws.respond_to?(:images) && ws.images && !ws.images.empty?
|
|
@@ -1307,10 +1169,8 @@ module Xlsxrb
|
|
|
1307
1169
|
opts[:file_data] = img.data
|
|
1308
1170
|
opts[:ext] = ext
|
|
1309
1171
|
opts[:from_column] = img.from_column
|
|
1310
|
-
opts[:from_col] = img.from_column
|
|
1311
1172
|
opts[:from_row] = img.from_row
|
|
1312
1173
|
opts[:to_column] = img.to_column
|
|
1313
|
-
opts[:to_col] = img.to_column
|
|
1314
1174
|
opts[:to_row] = img.to_row
|
|
1315
1175
|
opts
|
|
1316
1176
|
else
|
|
@@ -1329,13 +1189,19 @@ module Xlsxrb
|
|
|
1329
1189
|
|
|
1330
1190
|
# Extract workbook-level facade metadata
|
|
1331
1191
|
wb_facade = workbook.unmapped_data[:facade] || {}
|
|
1332
|
-
wb_properties = workbook.unmapped_data[:workbook_properties] || wb_facade[:workbook_properties]
|
|
1192
|
+
wb_properties = (workbook.unmapped_data[:workbook_properties] || wb_facade[:workbook_properties] || {}).merge(
|
|
1193
|
+
workbook.respond_to?(:workbook_properties) && workbook.workbook_properties ? workbook.workbook_properties.to_h : {}
|
|
1194
|
+
)
|
|
1333
1195
|
wb_core = (workbook.respond_to?(:core_properties) && workbook.core_properties) || wb_facade[:core_properties]
|
|
1334
1196
|
wb_app = (workbook.respond_to?(:app_properties) && workbook.app_properties) || wb_facade[:app_properties]
|
|
1335
1197
|
wb_custom = (workbook.respond_to?(:custom_properties) && workbook.custom_properties) || wb_facade[:custom_properties]
|
|
1336
1198
|
wb_prot = (workbook.respond_to?(:workbook_protection) && workbook.workbook_protection) || wb_facade[:workbook_protection]
|
|
1337
1199
|
raw_dns = wb_facade[:defined_names] || (workbook.respond_to?(:defined_names) && !workbook.defined_names.empty? ? workbook.defined_names : nil)
|
|
1338
|
-
defined_names =
|
|
1200
|
+
defined_names = if raw_dns
|
|
1201
|
+
raw_dns.map { |dn| dn.is_a?(Elements::MetadataRecord) ? dn.to_h : dn.dup }
|
|
1202
|
+
else
|
|
1203
|
+
[]
|
|
1204
|
+
end
|
|
1339
1205
|
|
|
1340
1206
|
# Auto-generate _xlnm.Print_Area and _xlnm.Print_Titles from worksheet definitions if not already present
|
|
1341
1207
|
workbook.sheets.each_with_index do |ws, idx|
|
|
@@ -1349,7 +1215,7 @@ module Xlsxrb
|
|
|
1349
1215
|
pt = ws.print_titles
|
|
1350
1216
|
pt = pt.to_h if pt.is_a?(Elements::MetadataRecord)
|
|
1351
1217
|
parts = []
|
|
1352
|
-
col_val = pt[:columns]
|
|
1218
|
+
col_val = pt[:columns]
|
|
1353
1219
|
parts << "'#{sheet_name}'!$#{col_val.sub(":", ":$")}" if col_val
|
|
1354
1220
|
parts << "'#{sheet_name}'!$#{pt[:rows].sub(":", ":$")}" if pt[:rows]
|
|
1355
1221
|
defined_names << { name: "_xlnm.Print_Titles", value: parts.join(","), local_sheet_id: idx } unless parts.empty?
|
|
@@ -1397,288 +1263,6 @@ module Xlsxrb
|
|
|
1397
1263
|
end
|
|
1398
1264
|
end
|
|
1399
1265
|
|
|
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
1266
|
# Builds an in-memory {Elements::Workbook} using a declarative DSL.
|
|
1683
1267
|
#
|
|
1684
1268
|
# @example Build an in-memory workbook
|
|
@@ -1689,18 +1273,21 @@ module Xlsxrb
|
|
|
1689
1273
|
# end
|
|
1690
1274
|
# end
|
|
1691
1275
|
#
|
|
1276
|
+
# @note Planned for version 0.3.0: Cell#format_code of the built cells will be the
|
|
1277
|
+
# number format of each cell's resolved style, as in a loaded workbook; cells styled
|
|
1278
|
+
# :default or :none will have style index 0; and a style index given in cell_styles
|
|
1279
|
+
# will be kept only in Cell#style_index.
|
|
1692
1280
|
# @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
1281
|
# @param default_font [Hash, nil] Optional default font options.
|
|
1695
1282
|
# @yield [builder]
|
|
1696
1283
|
# @yieldparam builder [Xlsxrb::WorkbookBuilder]
|
|
1697
1284
|
# @return [Elements::Workbook] The compiled in-memory workbook.
|
|
1698
1285
|
# @api public
|
|
1699
|
-
#: (?strict_excel_mode: bool, ?
|
|
1700
|
-
def self.build(strict_excel_mode: true,
|
|
1286
|
+
#: (?strict_excel_mode: bool, ?default_font: untyped?) ?{ (WorkbookBuilder) -> void } -> Elements::Workbook
|
|
1287
|
+
def self.build(strict_excel_mode: true, default_font: nil)
|
|
1701
1288
|
raise Error, "block is required" unless block_given?
|
|
1702
1289
|
|
|
1703
|
-
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode,
|
|
1290
|
+
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode, default_font: default_font)
|
|
1704
1291
|
yield builder
|
|
1705
1292
|
builder.build
|
|
1706
1293
|
end
|
|
@@ -1721,7 +1308,8 @@ module Xlsxrb
|
|
|
1721
1308
|
return "xl/workbook.xml" if zip_reader.entry?("xl/workbook.xml")
|
|
1722
1309
|
return "workbook.xml" if zip_reader.entry?("workbook.xml")
|
|
1723
1310
|
|
|
1724
|
-
|
|
1311
|
+
# A ZIP archive without a workbook part, such as an OpenDocument spreadsheet (.ods) renamed to .xlsx.
|
|
1312
|
+
raise ParseError, "not an XLSX workbook: the ZIP archive has no workbook part (xl/workbook.xml)"
|
|
1725
1313
|
end
|
|
1726
1314
|
|
|
1727
1315
|
def parse_workbook_relationships(rels_xml)
|
|
@@ -1786,10 +1374,9 @@ module Xlsxrb
|
|
|
1786
1374
|
fallbacks.find { |f| zip_reader.entry?(f) }
|
|
1787
1375
|
end
|
|
1788
1376
|
|
|
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,
|
|
1377
|
+
#: (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
|
|
1378
|
+
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
1379
|
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
1380
|
if sheet_xml.nil? || sheet_xml.empty?
|
|
1794
1381
|
return Elements::Worksheet.new(
|
|
1795
1382
|
name: name, state: state, styles: styles, images: images || [], date1904: date1904,
|
|
@@ -1806,9 +1393,9 @@ module Xlsxrb
|
|
|
1806
1393
|
header_footer_images: header_footer_images || [],
|
|
1807
1394
|
page_setup: page_setup || {}, page_margins: page_margins, header_footer: header_footer || {},
|
|
1808
1395
|
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 ||
|
|
1396
|
+
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [], column_breaks: column_breaks || [],
|
|
1810
1397
|
protected_ranges: protected_ranges || [], cell_watches: cell_watches || [], ignored_errors: ignored_errors || [],
|
|
1811
|
-
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties:
|
|
1398
|
+
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties: sheet_calc_properties,
|
|
1812
1399
|
background_image: background_image,
|
|
1813
1400
|
threaded_comments: threaded_comments || []
|
|
1814
1401
|
)
|
|
@@ -1921,7 +1508,7 @@ module Xlsxrb
|
|
|
1921
1508
|
{}
|
|
1922
1509
|
end
|
|
1923
1510
|
|
|
1924
|
-
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? ||
|
|
1511
|
+
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? || column_breaks.nil?
|
|
1925
1512
|
if sheet_xml.match?(/<(?:pageSetup|pageMargins|headerFooter|printOptions|rowBreaks|colBreaks)\b/)
|
|
1926
1513
|
pg_listener = Ooxml::Reader::PrintPageListener.new
|
|
1927
1514
|
Ooxml::XmlParser.parse(sheet_xml, pg_listener)
|
|
@@ -1931,15 +1518,13 @@ module Xlsxrb
|
|
|
1931
1518
|
header_footer: pg_listener.header_footer,
|
|
1932
1519
|
print_options: pg_listener.print_options,
|
|
1933
1520
|
row_breaks: pg_listener.row_breaks,
|
|
1934
|
-
column_breaks: pg_listener.column_breaks
|
|
1935
|
-
col_breaks: pg_listener.col_breaks
|
|
1521
|
+
column_breaks: pg_listener.column_breaks
|
|
1936
1522
|
}
|
|
1937
1523
|
else
|
|
1938
|
-
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], column_breaks: []
|
|
1524
|
+
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], column_breaks: [] }
|
|
1939
1525
|
end
|
|
1940
1526
|
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 }
|
|
1527
|
+
{ 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
1528
|
end
|
|
1944
1529
|
|
|
1945
1530
|
sheet_prot_ranges = protected_ranges || if sheet_xml.include?("<protectedRanges")
|
|
@@ -2015,13 +1600,13 @@ module Xlsxrb
|
|
|
2015
1600
|
sheet_properties: sheet_props,
|
|
2016
1601
|
sheet_format: sheet_fmt,
|
|
2017
1602
|
row_breaks: row_breaks || print_data[:row_breaks] || [],
|
|
2018
|
-
column_breaks: column_breaks ||
|
|
1603
|
+
column_breaks: column_breaks || print_data[:column_breaks] || [],
|
|
2019
1604
|
protected_ranges: sheet_prot_ranges,
|
|
2020
1605
|
cell_watches: sheet_watches,
|
|
2021
1606
|
ignored_errors: sheet_ignored_errors,
|
|
2022
1607
|
phonetic_properties: sheet_phonetic,
|
|
2023
1608
|
selection: sheet_selection,
|
|
2024
|
-
sheet_calc_properties:
|
|
1609
|
+
sheet_calc_properties: sheet_calc_properties,
|
|
2025
1610
|
background_image: background_image || (sheet_xml.include?("<picture") ? resolve_background_image(sheet_xml, zip_reader: zip_reader, entry_name: entry_name) : nil),
|
|
2026
1611
|
threaded_comments: sheet_threaded_comments
|
|
2027
1612
|
)
|
|
@@ -2087,7 +1672,7 @@ module Xlsxrb
|
|
|
2087
1672
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2088
1673
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2089
1674
|
|
|
2090
|
-
parser =
|
|
1675
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2091
1676
|
listener = Ooxml::Reader::RelsListener.new
|
|
2092
1677
|
parser.listen(listener)
|
|
2093
1678
|
parser.parse
|
|
@@ -2101,11 +1686,47 @@ module Xlsxrb
|
|
|
2101
1686
|
xml = zip_reader.read_entry(comments_path)
|
|
2102
1687
|
return [] if xml.nil? || xml.empty?
|
|
2103
1688
|
|
|
2104
|
-
c_parser =
|
|
1689
|
+
c_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2105
1690
|
c_listener = Ooxml::Reader::CommentsListener.new
|
|
2106
1691
|
c_parser.listen(c_listener)
|
|
2107
1692
|
c_parser.parse
|
|
2108
|
-
c_listener.comments
|
|
1693
|
+
comments = c_listener.comments
|
|
1694
|
+
notes = vml_notes(zip_reader, listener.relationships, base_dir)
|
|
1695
|
+
unless notes.empty?
|
|
1696
|
+
comments.each do |c|
|
|
1697
|
+
note = notes[c[:ref].to_s.upcase]
|
|
1698
|
+
next unless note
|
|
1699
|
+
|
|
1700
|
+
c[:visible] = true if note[:visible]
|
|
1701
|
+
c[:anchor] = note[:anchor] if note[:anchor]
|
|
1702
|
+
end
|
|
1703
|
+
end
|
|
1704
|
+
comments
|
|
1705
|
+
end
|
|
1706
|
+
|
|
1707
|
+
# Reads the comment notes of the sheet's legacy VML drawing, keyed by cell reference:
|
|
1708
|
+
# whether each is shown permanently (<x:Visible/>) and its <x:Anchor> box position.
|
|
1709
|
+
#: (Ooxml::ZipReader zip_reader, Array[Hash[Symbol, untyped]] relationships, String base_dir) -> Hash[String, Hash[Symbol, untyped]]
|
|
1710
|
+
def vml_notes(zip_reader, relationships, base_dir)
|
|
1711
|
+
rel = relationships.find { |r| r[:type]&.end_with?("/vmlDrawing") }
|
|
1712
|
+
vml = rel && zip_reader.read_entry(resolve_relative_path(base_dir, rel[:target]))
|
|
1713
|
+
return {} if vml.nil?
|
|
1714
|
+
|
|
1715
|
+
vml.split("</v:shape>").each_with_object({}) do |shape, notes|
|
|
1716
|
+
next unless shape.include?('ObjectType="Note"')
|
|
1717
|
+
|
|
1718
|
+
row = shape[%r{<x:Row>\s*(\d+)\s*</x:Row>}, 1]
|
|
1719
|
+
col = shape[%r{<x:Column>\s*(\d+)\s*</x:Column>}, 1]
|
|
1720
|
+
next unless row && col
|
|
1721
|
+
|
|
1722
|
+
anchor = shape[%r{<x:Anchor>([\d,\s]+)</x:Anchor>}, 1]&.split(",")&.map(&:to_i)
|
|
1723
|
+
# The writer's default box reads back as no anchor, as it was built.
|
|
1724
|
+
anchor = nil if anchor&.size != 8 || anchor == Elements::Comment.default_anchor(col.to_i, row.to_i)
|
|
1725
|
+
notes["#{Elements::Cell._column_letter(col.to_i)}#{row.to_i + 1}"] = {
|
|
1726
|
+
visible: shape.include?("<x:Visible"),
|
|
1727
|
+
anchor: anchor
|
|
1728
|
+
}
|
|
1729
|
+
end
|
|
2109
1730
|
end
|
|
2110
1731
|
|
|
2111
1732
|
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Hash[Symbol, untyped]]
|
|
@@ -2116,7 +1737,7 @@ module Xlsxrb
|
|
|
2116
1737
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2117
1738
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2118
1739
|
|
|
2119
|
-
parser =
|
|
1740
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2120
1741
|
listener = Ooxml::Reader::RelsListener.new
|
|
2121
1742
|
parser.listen(listener)
|
|
2122
1743
|
parser.parse
|
|
@@ -2130,7 +1751,7 @@ module Xlsxrb
|
|
|
2130
1751
|
xml = zip_reader.read_entry(tc_path)
|
|
2131
1752
|
return [] if xml.nil? || xml.empty?
|
|
2132
1753
|
|
|
2133
|
-
tc_parser =
|
|
1754
|
+
tc_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2134
1755
|
tc_listener = Ooxml::Reader::ThreadedCommentsListener.new
|
|
2135
1756
|
tc_parser.listen(tc_listener)
|
|
2136
1757
|
tc_parser.parse
|
|
@@ -2151,7 +1772,7 @@ module Xlsxrb
|
|
|
2151
1772
|
if xml.nil? || xml.empty?
|
|
2152
1773
|
wb_rels_xml = zip_reader.read_entry("xl/_rels/workbook.xml.rels")
|
|
2153
1774
|
if wb_rels_xml && !wb_rels_xml.empty?
|
|
2154
|
-
parser =
|
|
1775
|
+
parser = Ooxml::XmlParser.sax2_parser(wb_rels_xml)
|
|
2155
1776
|
listener = Ooxml::Reader::RelsListener.new
|
|
2156
1777
|
parser.listen(listener)
|
|
2157
1778
|
parser.parse
|
|
@@ -2164,7 +1785,7 @@ module Xlsxrb
|
|
|
2164
1785
|
end
|
|
2165
1786
|
return {} if xml.nil? || xml.empty?
|
|
2166
1787
|
|
|
2167
|
-
p_parser =
|
|
1788
|
+
p_parser = Ooxml::XmlParser.sax2_parser(xml)
|
|
2168
1789
|
p_listener = Ooxml::Reader::PersonListListener.new
|
|
2169
1790
|
p_parser.listen(p_listener)
|
|
2170
1791
|
p_parser.parse
|
|
@@ -2179,7 +1800,7 @@ module Xlsxrb
|
|
|
2179
1800
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2180
1801
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2181
1802
|
|
|
2182
|
-
parser =
|
|
1803
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2183
1804
|
listener = Ooxml::Reader::RelsListener.new
|
|
2184
1805
|
parser.listen(listener)
|
|
2185
1806
|
parser.parse
|
|
@@ -2197,7 +1818,7 @@ module Xlsxrb
|
|
|
2197
1818
|
drawing_rels_xml = zip_reader.read_entry(drawing_rels_path)
|
|
2198
1819
|
drawing_rels = {}
|
|
2199
1820
|
if drawing_rels_xml && !drawing_rels_xml.empty?
|
|
2200
|
-
d_parser =
|
|
1821
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_rels_xml)
|
|
2201
1822
|
d_listener = Ooxml::Reader::RelsListener.new
|
|
2202
1823
|
d_parser.listen(d_listener)
|
|
2203
1824
|
d_parser.parse
|
|
@@ -2206,7 +1827,7 @@ module Xlsxrb
|
|
|
2206
1827
|
end
|
|
2207
1828
|
end
|
|
2208
1829
|
|
|
2209
|
-
p_parser =
|
|
1830
|
+
p_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2210
1831
|
img_listener = Ooxml::Reader::DrawingImagesListener.new
|
|
2211
1832
|
p_parser.listen(img_listener)
|
|
2212
1833
|
p_parser.parse
|
|
@@ -2259,7 +1880,7 @@ module Xlsxrb
|
|
|
2259
1880
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2260
1881
|
return nil if rels_xml.nil? || rels_xml.empty?
|
|
2261
1882
|
|
|
2262
|
-
parser =
|
|
1883
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2263
1884
|
listener = Ooxml::Reader::RelsListener.new
|
|
2264
1885
|
parser.listen(listener)
|
|
2265
1886
|
parser.parse
|
|
@@ -2284,10 +1905,10 @@ module Xlsxrb
|
|
|
2284
1905
|
end
|
|
2285
1906
|
|
|
2286
1907
|
{
|
|
1908
|
+
r_id: picture_rid,
|
|
1909
|
+
path: target_path,
|
|
1910
|
+
extension: ext,
|
|
2287
1911
|
data: data.b,
|
|
2288
|
-
file_data: data.b,
|
|
2289
|
-
ext: ext,
|
|
2290
|
-
filename: File.basename(target_path),
|
|
2291
1912
|
content_type: mime
|
|
2292
1913
|
}.freeze
|
|
2293
1914
|
end
|
|
@@ -2300,7 +1921,7 @@ module Xlsxrb
|
|
|
2300
1921
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2301
1922
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2302
1923
|
|
|
2303
|
-
parser =
|
|
1924
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2304
1925
|
listener = Ooxml::Reader::RelsListener.new
|
|
2305
1926
|
parser.listen(listener)
|
|
2306
1927
|
parser.parse
|
|
@@ -2314,7 +1935,7 @@ module Xlsxrb
|
|
|
2314
1935
|
tbl_xml = zip_reader.read_entry(tbl_path)
|
|
2315
1936
|
next if tbl_xml.nil? || tbl_xml.empty?
|
|
2316
1937
|
|
|
2317
|
-
t_parser =
|
|
1938
|
+
t_parser = Ooxml::XmlParser.sax2_parser(tbl_xml)
|
|
2318
1939
|
t_listener = Ooxml::Reader::TableListener.new
|
|
2319
1940
|
t_parser.listen(t_listener)
|
|
2320
1941
|
t_parser.parse
|
|
@@ -2330,7 +1951,7 @@ module Xlsxrb
|
|
|
2330
1951
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2331
1952
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2332
1953
|
|
|
2333
|
-
parser =
|
|
1954
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2334
1955
|
listener = Ooxml::Reader::RelsListener.new
|
|
2335
1956
|
parser.listen(listener)
|
|
2336
1957
|
parser.parse
|
|
@@ -2343,7 +1964,7 @@ module Xlsxrb
|
|
|
2343
1964
|
drawing_xml = zip_reader.read_entry(drawing_path)
|
|
2344
1965
|
return [] if drawing_xml.nil? || drawing_xml.empty?
|
|
2345
1966
|
|
|
2346
|
-
d_parser =
|
|
1967
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2347
1968
|
d_listener = Ooxml::Reader::DrawingShapesListener.new
|
|
2348
1969
|
d_parser.listen(d_listener)
|
|
2349
1970
|
d_parser.parse
|
|
@@ -2358,7 +1979,7 @@ module Xlsxrb
|
|
|
2358
1979
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2359
1980
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2360
1981
|
|
|
2361
|
-
parser =
|
|
1982
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2362
1983
|
listener = Ooxml::Reader::RelsListener.new
|
|
2363
1984
|
parser.listen(listener)
|
|
2364
1985
|
parser.parse
|
|
@@ -2372,7 +1993,7 @@ module Xlsxrb
|
|
|
2372
1993
|
pt_xml = zip_reader.read_entry(pt_path)
|
|
2373
1994
|
next if pt_xml.nil? || pt_xml.empty?
|
|
2374
1995
|
|
|
2375
|
-
p_parser =
|
|
1996
|
+
p_parser = Ooxml::XmlParser.sax2_parser(pt_xml)
|
|
2376
1997
|
p_listener = Ooxml::Reader::PivotTableListener.new
|
|
2377
1998
|
p_parser.listen(p_listener)
|
|
2378
1999
|
p_parser.parse
|
|
@@ -2382,7 +2003,7 @@ module Xlsxrb
|
|
|
2382
2003
|
pt_rels_path = pt_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2383
2004
|
pt_rels_xml = zip_reader.read_entry(pt_rels_path)
|
|
2384
2005
|
if pt_rels_xml && !pt_rels_xml.empty?
|
|
2385
|
-
c_parser =
|
|
2006
|
+
c_parser = Ooxml::XmlParser.sax2_parser(pt_rels_xml)
|
|
2386
2007
|
c_listener = Ooxml::Reader::RelsListener.new
|
|
2387
2008
|
c_parser.listen(c_listener)
|
|
2388
2009
|
c_parser.parse
|
|
@@ -2391,7 +2012,7 @@ module Xlsxrb
|
|
|
2391
2012
|
c_path = resolve_relative_path(File.dirname(pt_path), cache_rel[:target])
|
|
2392
2013
|
c_xml = zip_reader.read_entry(c_path)
|
|
2393
2014
|
if c_xml && !c_xml.empty?
|
|
2394
|
-
cd_parser =
|
|
2015
|
+
cd_parser = Ooxml::XmlParser.sax2_parser(c_xml)
|
|
2395
2016
|
cd_listener = Ooxml::Reader::PivotCacheDefinitionListener.new
|
|
2396
2017
|
cd_parser.listen(cd_listener)
|
|
2397
2018
|
cd_parser.parse
|
|
@@ -2400,7 +2021,7 @@ module Xlsxrb
|
|
|
2400
2021
|
cd_rels_path = c_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2401
2022
|
cd_rels_xml = zip_reader.read_entry(cd_rels_path)
|
|
2402
2023
|
if cd_rels_xml && !cd_rels_xml.empty?
|
|
2403
|
-
rec_parser =
|
|
2024
|
+
rec_parser = Ooxml::XmlParser.sax2_parser(cd_rels_xml)
|
|
2404
2025
|
rec_rels_listener = Ooxml::Reader::RelsListener.new
|
|
2405
2026
|
rec_parser.listen(rec_rels_listener)
|
|
2406
2027
|
rec_parser.parse
|
|
@@ -2409,7 +2030,7 @@ module Xlsxrb
|
|
|
2409
2030
|
rec_path = resolve_relative_path(File.dirname(c_path), rec_rel[:target])
|
|
2410
2031
|
rec_xml = zip_reader.read_entry(rec_path)
|
|
2411
2032
|
if rec_xml && !rec_xml.empty?
|
|
2412
|
-
r_parser =
|
|
2033
|
+
r_parser = Ooxml::XmlParser.sax2_parser(rec_xml)
|
|
2413
2034
|
r_listener = Ooxml::Reader::PivotCacheRecordsListener.new
|
|
2414
2035
|
r_parser.listen(r_listener)
|
|
2415
2036
|
r_parser.parse
|
|
@@ -2434,7 +2055,7 @@ module Xlsxrb
|
|
|
2434
2055
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2435
2056
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2436
2057
|
|
|
2437
|
-
parser =
|
|
2058
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2438
2059
|
listener = Ooxml::Reader::RelsListener.new
|
|
2439
2060
|
parser.listen(listener)
|
|
2440
2061
|
parser.parse
|
|
@@ -2451,7 +2072,7 @@ module Xlsxrb
|
|
|
2451
2072
|
drawing_rels_xml = zip_reader.read_entry(drawing_rels_path)
|
|
2452
2073
|
drawing_rels = {}
|
|
2453
2074
|
if drawing_rels_xml && !drawing_rels_xml.empty?
|
|
2454
|
-
d_parser =
|
|
2075
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_rels_xml)
|
|
2455
2076
|
d_listener = Ooxml::Reader::RelsListener.new
|
|
2456
2077
|
d_parser.listen(d_listener)
|
|
2457
2078
|
d_parser.parse
|
|
@@ -2460,7 +2081,7 @@ module Xlsxrb
|
|
|
2460
2081
|
end
|
|
2461
2082
|
end
|
|
2462
2083
|
|
|
2463
|
-
d_parser =
|
|
2084
|
+
d_parser = Ooxml::XmlParser.sax2_parser(drawing_xml)
|
|
2464
2085
|
dc_listener = Ooxml::Reader::DrawingChartsListener.new
|
|
2465
2086
|
d_parser.listen(dc_listener)
|
|
2466
2087
|
d_parser.parse
|
|
@@ -2474,7 +2095,7 @@ module Xlsxrb
|
|
|
2474
2095
|
chart_path = resolve_relative_path(drawing_dir, target)
|
|
2475
2096
|
chart_xml = zip_reader.read_entry(chart_path)
|
|
2476
2097
|
if chart_xml && !chart_xml.empty?
|
|
2477
|
-
cp =
|
|
2098
|
+
cp = Ooxml::XmlParser.sax2_parser(chart_xml)
|
|
2478
2099
|
cl = Ooxml::Reader::ChartTypeListener.new
|
|
2479
2100
|
cp.listen(cl)
|
|
2480
2101
|
cp.parse
|
|
@@ -2680,10 +2301,14 @@ module Xlsxrb
|
|
|
2680
2301
|
end
|
|
2681
2302
|
end
|
|
2682
2303
|
end
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
chart[:
|
|
2686
|
-
chart[:
|
|
2304
|
+
from_col = chart.delete(:from_col)
|
|
2305
|
+
to_col = chart.delete(:to_col)
|
|
2306
|
+
chart[:from_column] ||= from_col if from_col
|
|
2307
|
+
chart[:to_column] ||= to_col if to_col
|
|
2308
|
+
rid = chart.delete(:rid)
|
|
2309
|
+
chart[:r_id] ||= rid if rid
|
|
2310
|
+
ctype = chart.delete(:chart_type)
|
|
2311
|
+
chart[:type] ||= ctype if ctype
|
|
2687
2312
|
Elements::Chart.new(**chart)
|
|
2688
2313
|
end
|
|
2689
2314
|
end
|
|
@@ -2693,7 +2318,7 @@ module Xlsxrb
|
|
|
2693
2318
|
return [] if sheet_xml.nil? || !sheet_xml.include?("sparklineGroup")
|
|
2694
2319
|
|
|
2695
2320
|
listener = Ooxml::Reader::SparklineGroupsListener.new
|
|
2696
|
-
parser =
|
|
2321
|
+
parser = Ooxml::XmlParser.sax2_parser(sheet_xml)
|
|
2697
2322
|
parser.listen(listener)
|
|
2698
2323
|
parser.parse
|
|
2699
2324
|
listener.groups.map do |grp|
|
|
@@ -2714,7 +2339,7 @@ module Xlsxrb
|
|
|
2714
2339
|
rels_xml = zip_reader.read_entry(rels_path)
|
|
2715
2340
|
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2716
2341
|
|
|
2717
|
-
parser =
|
|
2342
|
+
parser = Ooxml::XmlParser.sax2_parser(rels_xml)
|
|
2718
2343
|
listener = Ooxml::Reader::RelsListener.new
|
|
2719
2344
|
parser.listen(listener)
|
|
2720
2345
|
parser.parse
|
|
@@ -2731,7 +2356,7 @@ module Xlsxrb
|
|
|
2731
2356
|
vml_rels_xml = zip_reader.read_entry(vml_rels_path)
|
|
2732
2357
|
vml_rels = {}
|
|
2733
2358
|
if vml_rels_xml && !vml_rels_xml.empty?
|
|
2734
|
-
vr_parser =
|
|
2359
|
+
vr_parser = Ooxml::XmlParser.sax2_parser(vml_rels_xml)
|
|
2735
2360
|
vr_listener = Ooxml::Reader::RelsListener.new
|
|
2736
2361
|
vr_parser.listen(vr_listener)
|
|
2737
2362
|
vr_parser.parse
|
|
@@ -2740,7 +2365,7 @@ module Xlsxrb
|
|
|
2740
2365
|
end
|
|
2741
2366
|
end
|
|
2742
2367
|
|
|
2743
|
-
vml_parser =
|
|
2368
|
+
vml_parser = Ooxml::XmlParser.sax2_parser(vml_xml)
|
|
2744
2369
|
hf_listener = Ooxml::Reader::VmlHeaderFooterListener.new
|
|
2745
2370
|
vml_parser.listen(hf_listener)
|
|
2746
2371
|
vml_parser.parse
|
|
@@ -2756,7 +2381,6 @@ module Xlsxrb
|
|
|
2756
2381
|
next unless img_data
|
|
2757
2382
|
|
|
2758
2383
|
img_data = img_data.b
|
|
2759
|
-
filename = File.basename(media_path)
|
|
2760
2384
|
ext = File.extname(media_path).delete_prefix(".").downcase
|
|
2761
2385
|
content_type = case ext
|
|
2762
2386
|
when "png" then "image/png"
|
|
@@ -2771,9 +2395,7 @@ module Xlsxrb
|
|
|
2771
2395
|
position: shape[:id],
|
|
2772
2396
|
r_id: rel_id,
|
|
2773
2397
|
path: media_path,
|
|
2774
|
-
filename: filename,
|
|
2775
2398
|
extension: ext,
|
|
2776
|
-
ext: ext,
|
|
2777
2399
|
data: img_data,
|
|
2778
2400
|
content_type: content_type
|
|
2779
2401
|
)
|
|
@@ -2822,7 +2444,7 @@ module Xlsxrb
|
|
|
2822
2444
|
|
|
2823
2445
|
return nil if rows.nil? && cols.nil?
|
|
2824
2446
|
|
|
2825
|
-
Elements::PrintTitles.new(rows: rows, columns: cols
|
|
2447
|
+
Elements::PrintTitles.new(rows: rows, columns: cols)
|
|
2826
2448
|
end
|
|
2827
2449
|
|
|
2828
2450
|
#: (untyped defined_name, String? sheet_name, Integer? sheet_index) -> bool
|
|
@@ -2889,7 +2511,7 @@ module Xlsxrb
|
|
|
2889
2511
|
{}
|
|
2890
2512
|
end
|
|
2891
2513
|
|
|
2892
|
-
raw_cells = raw_row[:cells]
|
|
2514
|
+
raw_cells = raw_row.respond_to?(:cells) ? raw_row.cells : raw_row[:cells]
|
|
2893
2515
|
cells = raw_cells.map do |rc|
|
|
2894
2516
|
if rc.is_a?(Elements::Cell)
|
|
2895
2517
|
hl = hyperlinks_map[rc.ref]
|
|
@@ -2909,7 +2531,11 @@ module Xlsxrb
|
|
|
2909
2531
|
end
|
|
2910
2532
|
else
|
|
2911
2533
|
parsed = Elements::Cell._parse_ref(rc[:ref]) if rc[:ref]
|
|
2912
|
-
row_idx =
|
|
2534
|
+
row_idx = if parsed
|
|
2535
|
+
parsed[0]
|
|
2536
|
+
else
|
|
2537
|
+
(raw_row.respond_to?(:index) ? raw_row.index : raw_row[:index])
|
|
2538
|
+
end
|
|
2913
2539
|
col_idx = parsed ? parsed[1] : 0
|
|
2914
2540
|
ref = rc[:ref] || "#{Elements::Cell._column_letter(col_idx)}#{row_idx + 1}"
|
|
2915
2541
|
ref_key = ref.upcase
|
|
@@ -2943,20 +2569,24 @@ module Xlsxrb
|
|
|
2943
2569
|
)
|
|
2944
2570
|
end
|
|
2945
2571
|
end
|
|
2946
|
-
attrs = raw_row[:attrs] || {}
|
|
2947
|
-
|
|
2948
|
-
if
|
|
2572
|
+
attrs = raw_row.respond_to?(:attributes) ? raw_row.attributes : (raw_row[:attrs] || {})
|
|
2573
|
+
row_idx = raw_row.respond_to?(:index) ? raw_row.index : raw_row[:index]
|
|
2574
|
+
row_source = if raw_row.respond_to?(:source)
|
|
2575
|
+
raw_row.source
|
|
2576
|
+
else
|
|
2577
|
+
(raw_row.is_a?(Hash) ? raw_row[:source] : nil)
|
|
2578
|
+
end
|
|
2579
|
+
row_errors = Elements::Row.validate(row_idx, cells)
|
|
2580
|
+
if !row_errors.empty? && row_source
|
|
2949
2581
|
# simplecov:disable
|
|
2950
2582
|
# Edge case / untested delegation block
|
|
2951
2583
|
row_errors = row_errors.map do |err|
|
|
2952
|
-
"#{err} (at #{
|
|
2584
|
+
"#{err} (at #{row_source[:part]} row #{row_source[:row] + 1})"
|
|
2953
2585
|
# simplecov:enable
|
|
2954
2586
|
end
|
|
2955
2587
|
end
|
|
2956
|
-
row_unmapped = {}
|
|
2957
|
-
row_unmapped[:style_index] = attrs[:style_index] if attrs[:style_index]
|
|
2958
2588
|
Elements::Row.new(
|
|
2959
|
-
index:
|
|
2589
|
+
index: row_idx,
|
|
2960
2590
|
cells: cells,
|
|
2961
2591
|
height: attrs[:height],
|
|
2962
2592
|
hidden: attrs[:hidden] || false,
|
|
@@ -2964,7 +2594,7 @@ module Xlsxrb
|
|
|
2964
2594
|
collapsed: attrs[:collapsed] || false,
|
|
2965
2595
|
outline_level: attrs[:outline_level],
|
|
2966
2596
|
style_index: attrs[:style_index],
|
|
2967
|
-
unmapped_data:
|
|
2597
|
+
unmapped_data: Elements::EMPTY_HASH,
|
|
2968
2598
|
errors: row_errors
|
|
2969
2599
|
)
|
|
2970
2600
|
end
|
|
@@ -2994,7 +2624,12 @@ module Xlsxrb
|
|
|
2994
2624
|
source
|
|
2995
2625
|
end
|
|
2996
2626
|
else
|
|
2997
|
-
|
|
2627
|
+
# Not XLSX or encrypted bytes and not an existing file: report the path rather than
|
|
2628
|
+
# parsing the path string itself as ZIP content.
|
|
2629
|
+
raise Errno::EISDIR, source if File.directory?(source)
|
|
2630
|
+
|
|
2631
|
+
shown = source.length > 200 || source.include?("\n") ? "#{source[0, 60].inspect}... (#{source.bytesize} bytes)" : source
|
|
2632
|
+
raise Errno::ENOENT, shown
|
|
2998
2633
|
end
|
|
2999
2634
|
elsif source.respond_to?(:read) && source.respond_to?(:pos) && source.respond_to?(:seek)
|
|
3000
2635
|
begin
|
|
@@ -3015,438 +2650,5 @@ module Xlsxrb
|
|
|
3015
2650
|
source
|
|
3016
2651
|
end
|
|
3017
2652
|
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
2653
|
end
|
|
3452
2654
|
end
|