xlsxrb 0.1.17 → 0.1.18
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 +93 -1
- data/README.md +79 -19
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +31 -61
- data/lib/xlsxrb/chart_builder.rb +595 -113
- data/lib/xlsxrb/dsl_helpers.rb +72 -20
- data/lib/xlsxrb/elements/cell.rb +243 -56
- data/lib/xlsxrb/elements/column.rb +94 -5
- data/lib/xlsxrb/elements/coordinate_access.rb +108 -10
- data/lib/xlsxrb/elements/image.rb +81 -15
- data/lib/xlsxrb/elements/metadata.rb +2733 -0
- data/lib/xlsxrb/elements/row.rb +130 -46
- data/lib/xlsxrb/elements/styles.rb +184 -76
- data/lib/xlsxrb/elements/types.rb +242 -7
- data/lib/xlsxrb/elements/workbook.rb +171 -82
- data/lib/xlsxrb/elements/worksheet.rb +598 -102
- data/lib/xlsxrb/elements.rb +1 -0
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +12 -4
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -2
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +264 -17
- data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
- data/lib/xlsxrb/ooxml/reader.rb +4 -0
- data/lib/xlsxrb/ooxml/styles_parser.rb +10 -3
- data/lib/xlsxrb/ooxml/workbook_writer.rb +90 -36
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +2 -2
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +64 -19
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +5 -2
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +26 -5
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +19 -9
- data/lib/xlsxrb/ooxml/writer.rb +22 -6
- data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
- data/lib/xlsxrb/ooxml/zip_reader.rb +47 -10
- data/lib/xlsxrb/stream_row.rb +97 -18
- data/lib/xlsxrb/stream_sheet.rb +706 -190
- data/lib/xlsxrb/stream_writer.rb +1893 -374
- data/lib/xlsxrb/style_builder.rb +774 -111
- data/lib/xlsxrb/utils.rb +66 -106
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +380 -83
- data/lib/xlsxrb/worksheet_builder.rb +1210 -233
- data/lib/xlsxrb.rb +1266 -326
- data/sig/generated/xlsxrb/chart_builder.rbs +319 -90
- data/sig/generated/xlsxrb/dsl_helpers.rbs +20 -2
- data/sig/generated/xlsxrb/elements/cell.rbs +92 -20
- data/sig/generated/xlsxrb/elements/column.rbs +61 -2
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +49 -7
- data/sig/generated/xlsxrb/elements/image.rbs +30 -8
- data/sig/generated/xlsxrb/elements/metadata.rbs +1587 -0
- data/sig/generated/xlsxrb/elements/row.rbs +61 -21
- data/sig/generated/xlsxrb/elements/styles.rbs +96 -58
- data/sig/generated/xlsxrb/elements/types.rbs +27 -0
- data/sig/generated/xlsxrb/elements/workbook.rbs +63 -52
- data/sig/generated/xlsxrb/elements/worksheet.rbs +192 -51
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -0
- data/sig/generated/xlsxrb/ooxml/reader.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/writer.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +2 -2
- data/sig/generated/xlsxrb/stream_row.rbs +48 -14
- data/sig/generated/xlsxrb/stream_sheet.rbs +324 -115
- data/sig/generated/xlsxrb/stream_writer.rbs +1068 -255
- data/sig/generated/xlsxrb/style_builder.rbs +389 -79
- data/sig/generated/xlsxrb/utils.rbs +34 -18
- data/sig/generated/xlsxrb/workbook_builder.rbs +234 -34
- data/sig/generated/xlsxrb/worksheet_builder.rbs +628 -126
- data/sig/generated/xlsxrb.rbs +257 -49
- metadata +3 -1
data/lib/xlsxrb.rb
CHANGED
|
@@ -39,7 +39,7 @@ require_relative "xlsxrb/utils"
|
|
|
39
39
|
|
|
40
40
|
# Modern, streaming-capable, low-memory XLSX reading and writing library for Ruby.
|
|
41
41
|
#
|
|
42
|
-
# Provides high-level facade methods ({Xlsxrb.read}, {Xlsxrb.write}, {Xlsxrb.build}, {Xlsxrb.
|
|
42
|
+
# Provides high-level facade methods ({Xlsxrb.read}, {Xlsxrb.write}, {Xlsxrb.build}, {Xlsxrb.load})
|
|
43
43
|
# with full ECMA-376 OpenXML compliance, MS-OFFCRYPTO password protection, and zero core dependencies.
|
|
44
44
|
#
|
|
45
45
|
# @api public
|
|
@@ -48,21 +48,35 @@ module Xlsxrb
|
|
|
48
48
|
MAX_ROW = 1_048_576
|
|
49
49
|
|
|
50
50
|
# Maximum number of columns supported by Excel / OpenXML.
|
|
51
|
-
|
|
51
|
+
MAX_COLUMN = 16_384
|
|
52
|
+
|
|
53
|
+
# @deprecated Use {MAX_COLUMN} instead. Will be removed in 0.2.0.
|
|
54
|
+
MAX_COL = MAX_COLUMN
|
|
55
|
+
deprecate_constant :MAX_COL
|
|
52
56
|
|
|
53
57
|
# Maximum number of individual cell/range references allowed in a single data validation element by Excel.
|
|
54
58
|
MAX_REFS_PER_VALIDATION = 32_767
|
|
55
59
|
|
|
60
|
+
# @api private
|
|
61
|
+
module Deprecation
|
|
62
|
+
def self.warn(message, uplevel: 1)
|
|
63
|
+
Kernel.warn("[Xlsxrb DEPRECATION] #{message}", uplevel: uplevel + 1)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
56
67
|
# Base class for all exceptions raised by Xlsxrb.
|
|
57
68
|
# Predefined cell error constants are exposed on this class for convenience.
|
|
58
69
|
class Error < StandardError
|
|
59
|
-
DIV0
|
|
60
|
-
NA
|
|
61
|
-
NAME
|
|
62
|
-
NULL
|
|
63
|
-
NUM
|
|
64
|
-
REF
|
|
65
|
-
VALUE
|
|
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
|
|
66
80
|
end
|
|
67
81
|
|
|
68
82
|
# Raised when parsing malformed or invalid XML structures.
|
|
@@ -89,7 +103,7 @@ module Xlsxrb
|
|
|
89
103
|
xl/dialogsheets/
|
|
90
104
|
xl/macrosheets/
|
|
91
105
|
].freeze
|
|
92
|
-
private_constant :OMITTED_TARGET, :OFFICE_DOC_REL_TYPES, :NON_WORKSHEET_REL_SUFFIXES, :NON_WORKSHEET_TARGET_PREFIXES
|
|
106
|
+
private_constant :OMITTED_TARGET, :VALID_SHEET_STATES, :OFFICE_DOC_REL_TYPES, :NON_WORKSHEET_REL_SUFFIXES, :NON_WORKSHEET_TARGET_PREFIXES
|
|
93
107
|
|
|
94
108
|
# Raised when cell, row, or workbook parameters fail specification validation.
|
|
95
109
|
class ValidationError < Error; end
|
|
@@ -220,25 +234,30 @@ module Xlsxrb
|
|
|
220
234
|
# Creates a shared formula {Elements::Formula} object for master or follower cell.
|
|
221
235
|
#
|
|
222
236
|
# @example Create a shared formula master
|
|
223
|
-
# formula = Xlsxrb.shared_formula("A2*2",
|
|
237
|
+
# formula = Xlsxrb.shared_formula("A2*2", shared_index: 0, ref: "B2:B10")
|
|
224
238
|
#
|
|
225
239
|
# @example Create a shared formula follower
|
|
226
|
-
# formula = Xlsxrb.shared_formula(
|
|
240
|
+
# formula = Xlsxrb.shared_formula(shared_index: 0)
|
|
227
241
|
#
|
|
228
242
|
# @param expression [String, nil] The formula expression (nil for follower cells).
|
|
229
|
-
# @param
|
|
243
|
+
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
244
|
+
# @param si [Integer, nil] Legacy alias for shared_index.
|
|
230
245
|
# @param ref [String, nil] Target range for master cell (e.g. "B2:B10").
|
|
231
246
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
232
247
|
# @return [Elements::Formula]
|
|
233
248
|
# @api public
|
|
234
|
-
#: (?String? expression, si: Integer
|
|
235
|
-
def self.shared_formula(expression = nil, si
|
|
249
|
+
#: (?String? expression, ?shared_index: Integer?, ?si: Integer?, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil)) -> Elements::Formula
|
|
250
|
+
def self.shared_formula(expression = nil, shared_index: nil, si: nil, ref: nil, cached_value: nil)
|
|
251
|
+
warn "[DEPRECATION] Xlsxrb.shared_formula keyword :si is deprecated; use :shared_index instead.", uplevel: 1 if si && shared_index.nil?
|
|
252
|
+
s_idx = shared_index || si
|
|
253
|
+
raise ArgumentError, "shared_index is required for shared formula" if s_idx.nil?
|
|
254
|
+
|
|
236
255
|
Elements::Formula.new(
|
|
237
256
|
expression: expression || "",
|
|
238
257
|
cached_value: cached_value,
|
|
239
258
|
type: :shared,
|
|
240
259
|
ref: ref,
|
|
241
|
-
shared_index:
|
|
260
|
+
shared_index: s_idx,
|
|
242
261
|
calculate_always: cached_value.nil? || nil
|
|
243
262
|
)
|
|
244
263
|
end
|
|
@@ -248,23 +267,28 @@ module Xlsxrb
|
|
|
248
267
|
#
|
|
249
268
|
# @example Create a multi-cell array formula
|
|
250
269
|
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", "A1:C1", cached_value: 0)
|
|
270
|
+
# formula = Xlsxrb.array_formula("SUM(B1:B10*C1:C10)", ref: "A1:C1", cached_value: 0)
|
|
251
271
|
#
|
|
252
272
|
# @example Create a dynamic array formula
|
|
253
273
|
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", "D1")
|
|
254
274
|
#
|
|
255
275
|
# @param expression [String] The formula expression without leading '='.
|
|
256
|
-
# @param
|
|
276
|
+
# @param pos_ref [String, nil] Target cell or range reference (positional).
|
|
277
|
+
# @param ref [String, nil] Target cell or range reference (keyword).
|
|
257
278
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
258
279
|
# @return [Elements::Formula]
|
|
259
280
|
# @api public
|
|
260
|
-
#: (String expression, ?String?
|
|
261
|
-
def self.array_formula(expression,
|
|
281
|
+
#: (String expression, ?String? pos_ref, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) -> Elements::Formula
|
|
282
|
+
def self.array_formula(expression, pos_ref = nil, ref: nil, cached_value: nil, **attrs)
|
|
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
|
|
262
285
|
Elements::Formula.new(
|
|
263
286
|
expression: expression,
|
|
264
287
|
cached_value: cached_value,
|
|
265
288
|
type: :array,
|
|
266
|
-
ref:
|
|
267
|
-
calculate_always: cached_value.nil? || nil
|
|
289
|
+
ref: target_ref,
|
|
290
|
+
calculate_always: cached_value.nil? || nil,
|
|
291
|
+
**attrs
|
|
268
292
|
)
|
|
269
293
|
end
|
|
270
294
|
|
|
@@ -287,6 +311,50 @@ module Xlsxrb
|
|
|
287
311
|
Elements::Hyperlink.new(url: url, display: display, tooltip: tooltip, location: location)
|
|
288
312
|
end
|
|
289
313
|
|
|
314
|
+
# Creates an {Elements::Comment} object for use in cell comments.
|
|
315
|
+
#
|
|
316
|
+
# @example Create a cell comment
|
|
317
|
+
# comment = Xlsxrb.comment("Review this value", author: "Auditor")
|
|
318
|
+
#
|
|
319
|
+
# @param text [String, nil] Comment body text.
|
|
320
|
+
# @param author [String] Comment author.
|
|
321
|
+
# @param visible [Boolean] Whether comment is visible by default.
|
|
322
|
+
# @param ref [String, nil] Cell coordinate (e.g. "A1").
|
|
323
|
+
# @return [Elements::Comment]
|
|
324
|
+
# @api public
|
|
325
|
+
#: (?String? text, ?author: String, ?visible: bool, ?ref: String?, **untyped extra) -> Elements::Comment
|
|
326
|
+
def self.comment(text = nil, author: "Author", visible: false, ref: nil, **extra)
|
|
327
|
+
Elements::Comment.new(text: text, author: author, visible: visible, ref: ref, **extra)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Creates or returns an {Elements::CellError} object.
|
|
331
|
+
#
|
|
332
|
+
# @example
|
|
333
|
+
# err = Xlsxrb.cell_error("#VALUE!")
|
|
334
|
+
# err = Xlsxrb.cell_error(:value)
|
|
335
|
+
#
|
|
336
|
+
# @param code [String, Symbol, Elements::CellError] Error code or symbol.
|
|
337
|
+
# @return [Elements::CellError]
|
|
338
|
+
# @api public
|
|
339
|
+
#: (String | Symbol | Elements::CellError code) -> Elements::CellError
|
|
340
|
+
def self.cell_error(code)
|
|
341
|
+
return code if code.is_a?(Elements::CellError)
|
|
342
|
+
|
|
343
|
+
code_str = code.to_s
|
|
344
|
+
error_map = {
|
|
345
|
+
null: "#NULL!",
|
|
346
|
+
div0: "#DIV/0!",
|
|
347
|
+
value: "#VALUE!",
|
|
348
|
+
ref: "#REF!",
|
|
349
|
+
name: "#NAME?",
|
|
350
|
+
num: "#NUM!",
|
|
351
|
+
na: "#N/A",
|
|
352
|
+
getting_data: "#GETTING_DATA"
|
|
353
|
+
}
|
|
354
|
+
resolved_code = error_map[code.to_sym] || code_str
|
|
355
|
+
Elements::CellError.new(code: resolved_code)
|
|
356
|
+
end
|
|
357
|
+
|
|
290
358
|
# Formats a cell value according to an Excel format code string.
|
|
291
359
|
#
|
|
292
360
|
# @param value [Object, nil] Value to format.
|
|
@@ -295,10 +363,17 @@ module Xlsxrb
|
|
|
295
363
|
# @return [String] Formatted string representation.
|
|
296
364
|
# @api public
|
|
297
365
|
#: (untyped value, ?String? format_code, ?date1904: bool) -> String
|
|
298
|
-
def self.
|
|
366
|
+
def self.format_value(value, format_code = nil, date1904: false)
|
|
299
367
|
NumberFormatter.format(value, format_code, date1904: date1904)
|
|
300
368
|
end
|
|
301
369
|
|
|
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
|
+
|
|
302
377
|
# Converts a Date or Time to an Excel numeric serial number (1900 or 1904 system).
|
|
303
378
|
#
|
|
304
379
|
# @example
|
|
@@ -310,7 +385,11 @@ module Xlsxrb
|
|
|
310
385
|
# @api public
|
|
311
386
|
#: (Date | Time date_or_time, ?date1904: bool) -> (Integer | Float)
|
|
312
387
|
def self.date_to_serial(date_or_time, date1904: false)
|
|
313
|
-
|
|
388
|
+
if date_or_time.is_a?(Time)
|
|
389
|
+
Ooxml::Utils.datetime_to_serial(date_or_time, date1904: date1904)
|
|
390
|
+
else
|
|
391
|
+
Ooxml::Utils.date_to_serial(date_or_time, date1904: date1904)
|
|
392
|
+
end
|
|
314
393
|
end
|
|
315
394
|
|
|
316
395
|
# Checks whether a serial number is a valid Excel date within the supported range.
|
|
@@ -325,7 +404,7 @@ module Xlsxrb
|
|
|
325
404
|
# @api public
|
|
326
405
|
#: (untyped serial, ?date1904: bool) -> bool
|
|
327
406
|
def self.valid_excel_date?(serial, date1904: false)
|
|
328
|
-
Utils.valid_excel_date?(serial, date1904: date1904)
|
|
407
|
+
Ooxml::Utils.valid_excel_date?(serial, date1904: date1904)
|
|
329
408
|
end
|
|
330
409
|
|
|
331
410
|
# Converts an Excel serial number to a Date.
|
|
@@ -339,21 +418,32 @@ module Xlsxrb
|
|
|
339
418
|
# @api public
|
|
340
419
|
#: (Numeric serial, ?date1904: bool) -> Date
|
|
341
420
|
def self.serial_to_date(serial, date1904: false)
|
|
342
|
-
Utils.serial_to_date(serial, date1904: date1904)
|
|
421
|
+
Ooxml::Utils.serial_to_date(serial, date1904: date1904)
|
|
343
422
|
end
|
|
344
423
|
|
|
345
424
|
# Converts a Time or Date to a fractional Excel serial number.
|
|
346
425
|
#
|
|
347
426
|
# @example
|
|
348
|
-
# Xlsxrb.
|
|
427
|
+
# Xlsxrb.time_to_serial(Time.utc(2026, 1, 1, 12, 0, 0)) #=> 46023.5
|
|
349
428
|
#
|
|
350
429
|
# @param time [Time, Date] Time or Date instance.
|
|
351
430
|
# @param date1904 [Boolean] Whether to use the 1904 date system.
|
|
352
431
|
# @return [Float] Fractional serial number.
|
|
353
432
|
# @api public
|
|
354
433
|
#: (Time | Date time, ?date1904: bool) -> Float
|
|
434
|
+
def self.time_to_serial(time, date1904: false)
|
|
435
|
+
if time.is_a?(Time)
|
|
436
|
+
Ooxml::Utils.datetime_to_serial(time, date1904: date1904)
|
|
437
|
+
else
|
|
438
|
+
Ooxml::Utils.date_to_serial(time, date1904: date1904).to_f
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# @deprecated Use {.time_to_serial} instead. Will be removed in 0.2.0.
|
|
443
|
+
#: (Time | Date time, ?date1904: bool) -> Float
|
|
355
444
|
def self.datetime_to_serial(time, date1904: false)
|
|
356
|
-
|
|
445
|
+
warn "[DEPRECATION] Xlsxrb.datetime_to_serial is deprecated; use Xlsxrb.time_to_serial instead.", uplevel: 1
|
|
446
|
+
time_to_serial(time, date1904: date1904)
|
|
357
447
|
end
|
|
358
448
|
|
|
359
449
|
# Converts an Excel serial number to a Time (UTC).
|
|
@@ -367,11 +457,71 @@ module Xlsxrb
|
|
|
367
457
|
# @api public
|
|
368
458
|
#: (Numeric serial, ?date1904: bool) -> Time
|
|
369
459
|
def self.serial_to_time(serial, date1904: false)
|
|
370
|
-
Utils.
|
|
460
|
+
Ooxml::Utils.serial_to_datetime(serial, date1904: date1904)
|
|
371
461
|
end
|
|
372
462
|
|
|
373
|
-
|
|
374
|
-
|
|
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
|
+
# Converts a cell reference string (e.g. "A1", "BC100") to 0-based [row, col] coordinates.
|
|
471
|
+
#
|
|
472
|
+
# @example
|
|
473
|
+
# Xlsxrb.parse_ref("A1") #=> [0, 0]
|
|
474
|
+
# Xlsxrb.parse_ref("BC100") #=> [99, 54]
|
|
475
|
+
#
|
|
476
|
+
# @param ref [String, nil] Excel cell reference string.
|
|
477
|
+
# @return [Array(Integer, Integer), nil] 0-based [row, col] tuple, or nil if invalid.
|
|
478
|
+
# @api public
|
|
479
|
+
#: (String? ref) -> [Integer, Integer]?
|
|
480
|
+
def self.parse_ref(ref)
|
|
481
|
+
Elements::Cell._parse_ref(ref)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Converts 0-based [row, col] coordinates to an A1-style reference string.
|
|
485
|
+
#
|
|
486
|
+
# @example
|
|
487
|
+
# Xlsxrb.cell_to_ref(0, 0) #=> "A1"
|
|
488
|
+
# Xlsxrb.cell_to_ref(99, 54) #=> "BC100"
|
|
489
|
+
#
|
|
490
|
+
# @param row [Integer] 0-based row index.
|
|
491
|
+
# @param column [Integer] 0-based column index.
|
|
492
|
+
# @return [String] A1-style reference string.
|
|
493
|
+
# @api public
|
|
494
|
+
#: (Integer row, Integer column) -> String
|
|
495
|
+
def self.cell_to_ref(row, column)
|
|
496
|
+
"#{Elements::Cell._column_letter(column)}#{row + 1}"
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
# Converts a column letter (e.g. "A", :AA) to a 0-based column index.
|
|
500
|
+
#
|
|
501
|
+
# @example
|
|
502
|
+
# Xlsxrb.column_index("A") #=> 0
|
|
503
|
+
# Xlsxrb.column_index(:AA) #=> 26
|
|
504
|
+
#
|
|
505
|
+
# @param col [String, Symbol, Integer] Column letter or integer index.
|
|
506
|
+
# @return [Integer] 0-based column index.
|
|
507
|
+
# @api public
|
|
508
|
+
#: (String | Symbol | Integer col) -> Integer
|
|
509
|
+
def self.column_index(col)
|
|
510
|
+
Elements::Cell._column_index(col)
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
# Converts a 0-based column index to an Excel letter (0 -> "A", 25 -> "Z", 26 -> "AA").
|
|
514
|
+
#
|
|
515
|
+
# @example
|
|
516
|
+
# Xlsxrb.column_letter(0) #=> "A"
|
|
517
|
+
# Xlsxrb.column_letter(26) #=> "AA"
|
|
518
|
+
#
|
|
519
|
+
# @param index [Integer] 0-based column index.
|
|
520
|
+
# @return [String] Excel column letter.
|
|
521
|
+
# @api public
|
|
522
|
+
#: (Integer index) -> String
|
|
523
|
+
def self.column_letter(index)
|
|
524
|
+
Elements::Cell._column_letter(index)
|
|
375
525
|
end
|
|
376
526
|
|
|
377
527
|
# Converts a cell range reference string (e.g. "A1:C10", "A1") to 0-based [[top_row, left_col], [bottom_row, right_col]].
|
|
@@ -384,23 +534,150 @@ module Xlsxrb
|
|
|
384
534
|
# @api public
|
|
385
535
|
#: (String? range_ref) -> [[Integer, Integer], [Integer, Integer]]?
|
|
386
536
|
def self.parse_range(range_ref)
|
|
387
|
-
|
|
537
|
+
return nil if range_ref.nil? || range_ref.empty?
|
|
538
|
+
|
|
539
|
+
parts = range_ref.split(":", 2)
|
|
540
|
+
first = Elements::Cell._parse_ref(parts[0])
|
|
541
|
+
return nil unless first
|
|
542
|
+
|
|
543
|
+
second = parts[1] ? Elements::Cell._parse_ref(parts[1]) : first
|
|
544
|
+
return nil unless second
|
|
545
|
+
|
|
546
|
+
top_row = [first[0], second[0]].min
|
|
547
|
+
bottom_row = [first[0], second[0]].max
|
|
548
|
+
left_col = [first[1], second[1]].min
|
|
549
|
+
right_col = [first[1], second[1]].max
|
|
550
|
+
[[top_row, left_col], [bottom_row, right_col]]
|
|
388
551
|
end
|
|
389
552
|
|
|
390
553
|
# Converts 0-based bounding coordinates to an A1-style range reference string.
|
|
554
|
+
# Accepts either 4 flat integers (top_row, left_column, bottom_row, right_column)
|
|
555
|
+
# or a nested pair ([[top_row, left_column], [bottom_row, right_column]]) as returned by parse_range.
|
|
391
556
|
#
|
|
392
|
-
# @example
|
|
557
|
+
# @example 4 flat integers
|
|
393
558
|
# Xlsxrb.range_to_ref(0, 0, 9, 2) #=> "A1:C10"
|
|
394
559
|
#
|
|
395
|
-
# @
|
|
396
|
-
#
|
|
397
|
-
#
|
|
398
|
-
# @param
|
|
399
|
-
# @
|
|
560
|
+
# @example Nested pair from parse_range
|
|
561
|
+
# Xlsxrb.range_to_ref([[0, 0], [9, 2]]) #=> "A1:C10"
|
|
562
|
+
#
|
|
563
|
+
# @param top_row_or_bounds [Integer, Array] 0-based start row index or nested bounding coordinates.
|
|
564
|
+
# @param left_column [Integer, nil] 0-based start column index.
|
|
565
|
+
# @param bottom_row [Integer, nil] 0-based end row index.
|
|
566
|
+
# @param right_column [Integer, nil] 0-based end column index.
|
|
567
|
+
# @return [String] A1-style range reference string.
|
|
568
|
+
# @api public
|
|
569
|
+
#: (Integer | [[Integer, Integer], [Integer, Integer]] top_row_or_bounds, ?Integer? left_column, ?Integer? bottom_row, ?Integer? right_column) -> String
|
|
570
|
+
def self.range_to_ref(top_row_or_bounds, left_column = nil, bottom_row = nil, right_column = nil)
|
|
571
|
+
if top_row_or_bounds.is_a?(Array)
|
|
572
|
+
first, second = top_row_or_bounds
|
|
573
|
+
top_row = first[0]
|
|
574
|
+
left_col = first[1]
|
|
575
|
+
bot_row = second[0]
|
|
576
|
+
rgt_col = second[1]
|
|
577
|
+
else
|
|
578
|
+
top_row = top_row_or_bounds
|
|
579
|
+
left_col = left_column || 0
|
|
580
|
+
bot_row = bottom_row || top_row
|
|
581
|
+
rgt_col = right_column || left_col
|
|
582
|
+
end
|
|
583
|
+
if top_row == bot_row && left_col == rgt_col
|
|
584
|
+
cell_to_ref(top_row, left_col)
|
|
585
|
+
else
|
|
586
|
+
"#{cell_to_ref(top_row, left_col)}:#{cell_to_ref(bot_row, rgt_col)}"
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
# Splits an A1-style cell reference into its column name and 1-based row number.
|
|
591
|
+
#
|
|
592
|
+
# @example
|
|
593
|
+
# Xlsxrb.split_coordinate("A1") #=> ["A", 1]
|
|
594
|
+
# Xlsxrb.split_coordinate("BC100") #=> ["BC", 100]
|
|
595
|
+
#
|
|
596
|
+
# @param cell_ref [String, nil] Excel cell reference string.
|
|
597
|
+
# @return [Array(String, Integer), nil] [col_name, row_number] tuple (1-based row), or nil if invalid.
|
|
400
598
|
# @api public
|
|
401
|
-
#: (
|
|
402
|
-
def self.
|
|
403
|
-
|
|
599
|
+
#: (String? cell_ref) -> [String, Integer]?
|
|
600
|
+
def self.split_coordinate(cell_ref)
|
|
601
|
+
return nil unless cell_ref
|
|
602
|
+
|
|
603
|
+
coords = Elements::Cell._parse_ref(cell_ref)
|
|
604
|
+
return nil unless coords
|
|
605
|
+
|
|
606
|
+
row_idx, col_idx = coords
|
|
607
|
+
[Elements::Cell._column_letter(col_idx), row_idx + 1]
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
FORMULA_TOKEN_RE = %r{(?<str>"(?:[^"]|"")*")|(?<ref>(?<=\b|\A|[$!:,()+\-*/^<>=& \r\n])(?<col_abs>\$?)(?<col>[A-Za-z]{1,3})(?<row_abs>\$?)(?<row>\d+)(?!\w|\())}
|
|
611
|
+
private_constant :FORMULA_TOKEN_RE
|
|
612
|
+
|
|
613
|
+
# Expands a formula by shifting relative cell references from a master cell to a target cell or by offset amounts.
|
|
614
|
+
#
|
|
615
|
+
# @example Shift formula down one row
|
|
616
|
+
# Xlsxrb.expand_formula("A2*2", 1, 0) #=> "A3*2"
|
|
617
|
+
# Xlsxrb.expand_formula("A2*2", from_ref: "B2", to_ref: "B3") #=> "A3*2"
|
|
618
|
+
#
|
|
619
|
+
# @example Shift formula with absolute references
|
|
620
|
+
# Xlsxrb.expand_formula("$A$1+A2", 2, 0) #=> "$A$1+A4"
|
|
621
|
+
#
|
|
622
|
+
# @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
|
+
# @param from_ref [String, nil] Master cell reference (e.g. "B2").
|
|
626
|
+
# @param to_ref [String, nil] Target cell reference (e.g. "B3").
|
|
627
|
+
# @param row_delta [Integer, nil] Row shift amount (positive = down, negative = up).
|
|
628
|
+
# @param column_delta [Integer, nil] Column shift amount (positive = right, negative = left).
|
|
629
|
+
# @param col_delta [Integer, nil] Deprecated alias for column_delta.
|
|
630
|
+
# @return [String, nil] Expanded formula expression.
|
|
631
|
+
# @api public
|
|
632
|
+
#: (String? formula, ?Integer? offset_row, ?Integer? offset_col, ?from_ref: String?, ?to_ref: String?, ?row_delta: Integer?, ?column_delta: Integer?, ?col_delta: Integer?) -> String?
|
|
633
|
+
def self.expand_formula(formula, offset_row = nil, offset_col = nil, from_ref: nil, to_ref: nil, row_delta: nil, column_delta: nil, col_delta: nil)
|
|
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
|
+
|
|
637
|
+
return formula if formula.nil? || formula.empty?
|
|
638
|
+
|
|
639
|
+
if from_ref && to_ref
|
|
640
|
+
f_coords = Elements::Cell._parse_ref(from_ref)
|
|
641
|
+
t_coords = Elements::Cell._parse_ref(to_ref)
|
|
642
|
+
return formula unless f_coords && t_coords
|
|
643
|
+
|
|
644
|
+
rd = t_coords[0] - f_coords[0]
|
|
645
|
+
cd = t_coords[1] - f_coords[1]
|
|
646
|
+
else
|
|
647
|
+
rd = row_delta || offset_row || 0
|
|
648
|
+
cd = column_delta || col_delta || offset_col || 0
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
return formula if rd.zero? && cd.zero?
|
|
652
|
+
|
|
653
|
+
formula.gsub(FORMULA_TOKEN_RE) do |match|
|
|
654
|
+
m = Regexp.last_match
|
|
655
|
+
if m[:str]
|
|
656
|
+
match
|
|
657
|
+
else
|
|
658
|
+
c_abs = !m[:col_abs].empty?
|
|
659
|
+
r_abs = !m[:row_abs].empty?
|
|
660
|
+
col = m[:col]
|
|
661
|
+
row = m[:row].to_i
|
|
662
|
+
|
|
663
|
+
new_col = if c_abs || cd.zero?
|
|
664
|
+
col
|
|
665
|
+
else
|
|
666
|
+
ci = Elements::Cell._column_index(col) + cd
|
|
667
|
+
ci = ci.clamp(0, 16_383)
|
|
668
|
+
Elements::Cell._column_letter(ci)
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
new_row = if r_abs || rd.zero?
|
|
672
|
+
row
|
|
673
|
+
else
|
|
674
|
+
ri = row + rd
|
|
675
|
+
ri.clamp(1, 1_048_576)
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
"#{m[:col_abs]}#{new_col}#{m[:row_abs]}#{new_row}"
|
|
679
|
+
end
|
|
680
|
+
end
|
|
404
681
|
end
|
|
405
682
|
|
|
406
683
|
# Resolves an ECMA-376 indexed color palette index (0..63) to an RRGGBB hex string (or AARRGGBB if alpha: true).
|
|
@@ -435,8 +712,11 @@ module Xlsxrb
|
|
|
435
712
|
Colors.to_hex(color, alpha: alpha)
|
|
436
713
|
end
|
|
437
714
|
|
|
438
|
-
|
|
439
|
-
|
|
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)
|
|
440
720
|
end
|
|
441
721
|
|
|
442
722
|
# Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters.
|
|
@@ -456,8 +736,11 @@ module Xlsxrb
|
|
|
456
736
|
DslHelpers.sanitize_sheet_name(name, replace_char: replace_char)
|
|
457
737
|
end
|
|
458
738
|
|
|
459
|
-
|
|
460
|
-
|
|
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)
|
|
461
744
|
end
|
|
462
745
|
|
|
463
746
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
@@ -500,6 +783,11 @@ module Xlsxrb
|
|
|
500
783
|
# doc_sheet = sheet.load # explicitly load into memory
|
|
501
784
|
# puts doc_sheet["A1"].value # coordinate random access
|
|
502
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.
|
|
503
791
|
# @param max_uncompressed_size [Integer, nil] Maximum allowed uncompressed size in bytes per entry (ZIP bomb defense).
|
|
504
792
|
# @raise [ZipBombError] If an uncompressed entry exceeds max_uncompressed_size.
|
|
505
793
|
# @raise [EncryptedFileError] If the file is encrypted and no password was supplied.
|
|
@@ -509,10 +797,57 @@ module Xlsxrb
|
|
|
509
797
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) { (StreamSheet) -> void } -> void
|
|
510
798
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Elements::Workbook
|
|
511
799
|
def self.read(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil, &)
|
|
800
|
+
if block_given?
|
|
801
|
+
wb = _read_workbook(
|
|
802
|
+
source,
|
|
803
|
+
password: password,
|
|
804
|
+
trim_empty_rows: trim_empty_rows,
|
|
805
|
+
pad_empty_rows: pad_empty_rows,
|
|
806
|
+
pad_empty_cells: pad_empty_cells,
|
|
807
|
+
max_uncompressed_size: max_uncompressed_size
|
|
808
|
+
)
|
|
809
|
+
begin
|
|
810
|
+
wb.sheets.each(&)
|
|
811
|
+
nil
|
|
812
|
+
ensure
|
|
813
|
+
wb.close
|
|
814
|
+
end
|
|
815
|
+
else
|
|
816
|
+
warn "[DEPRECATION] Calling Xlsxrb.read without a block is deprecated; use Xlsxrb.load(source) for in-memory random access or pass a block to Xlsxrb.read(source) { |sheet| ... } for streaming.", uplevel: 1
|
|
817
|
+
_read_workbook(
|
|
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
|
+
)
|
|
825
|
+
end
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
# Loads all sheets into memory, returning an {Elements::Workbook} where every
|
|
829
|
+
# worksheet is a fully-parsed {Elements::Worksheet} supporting coordinate random access.
|
|
830
|
+
# The underlying zip resources are closed immediately.
|
|
831
|
+
#
|
|
832
|
+
# @example
|
|
833
|
+
# wb = Xlsxrb.load("report.xlsx")
|
|
834
|
+
# puts wb["Sheet1"]["A1"].value
|
|
835
|
+
#
|
|
836
|
+
# @param source [String, IO, StringIO] Path to the .xlsx file or an IO-like stream.
|
|
837
|
+
# @param password [String, nil] Password for decrypting password-protected (ECMA-376 Standard/Agile) workbooks.
|
|
838
|
+
# @param trim_empty_rows [Boolean] Whether to omit trailing empty rows from worksheet data.
|
|
839
|
+
# @param pad_empty_rows [Boolean] Whether to pad missing rows with empty row objects.
|
|
840
|
+
# @param pad_empty_cells [Boolean] Whether to pad missing cells within rows with nil cells.
|
|
841
|
+
# @param max_uncompressed_size [Integer, nil] Maximum uncompressed file size threshold in bytes for zip bomb protection.
|
|
842
|
+
# @return [Elements::Workbook] The fully loaded in-memory workbook.
|
|
843
|
+
# @api public
|
|
844
|
+
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Elements::Workbook
|
|
845
|
+
def self.load(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil)
|
|
512
846
|
prepared_source = prepare_source_io(source, password)
|
|
513
847
|
zip_options = {}
|
|
514
848
|
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
515
849
|
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
850
|
+
|
|
516
851
|
begin
|
|
517
852
|
workbook_path = resolve_workbook_entry(zip_reader)
|
|
518
853
|
wb_dir = File.dirname(workbook_path)
|
|
@@ -532,6 +867,7 @@ module Xlsxrb
|
|
|
532
867
|
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
533
868
|
workbook_sheets = wb_parsed[:sheets]
|
|
534
869
|
is_date1904 = wb_parsed[:date1904]
|
|
870
|
+
defined_names = wb_parsed[:defined_names] || []
|
|
535
871
|
|
|
536
872
|
non_worksheet_sheets = []
|
|
537
873
|
ws_idx = 0
|
|
@@ -545,29 +881,36 @@ module Xlsxrb
|
|
|
545
881
|
end
|
|
546
882
|
|
|
547
883
|
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
884
|
+
current_sheet_idx = ws_idx
|
|
548
885
|
ws_idx += 1
|
|
549
886
|
next unless sheet_path
|
|
550
887
|
|
|
551
|
-
|
|
888
|
+
sheet_xml = zip_reader.read_entry(sheet_path)
|
|
889
|
+
sheet_name = sheet_info[:name]
|
|
890
|
+
pa = extract_sheet_print_area(defined_names, sheet_name, current_sheet_idx)
|
|
891
|
+
pt = extract_sheet_print_titles(defined_names, sheet_name, current_sheet_idx)
|
|
552
892
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
893
|
+
build_worksheet(
|
|
894
|
+
sheet_name,
|
|
895
|
+
sheet_xml,
|
|
556
896
|
shared_strings,
|
|
557
897
|
styles,
|
|
898
|
+
state: sheet_info[:state] || :visible,
|
|
558
899
|
zip_reader: zip_reader,
|
|
559
900
|
entry_name: sheet_path,
|
|
560
|
-
state: sheet_info[:state] || :visible,
|
|
561
901
|
date1904: is_date1904,
|
|
562
902
|
trim_empty_rows: trim_empty_rows,
|
|
563
903
|
pad_empty_rows: pad_empty_rows,
|
|
564
|
-
pad_empty_cells: pad_empty_cells
|
|
904
|
+
pad_empty_cells: pad_empty_cells,
|
|
905
|
+
print_area: pa,
|
|
906
|
+
print_titles: pt
|
|
565
907
|
)
|
|
566
908
|
end
|
|
567
909
|
|
|
568
910
|
wb_unmapped = {}
|
|
569
911
|
wb_props = {}
|
|
570
912
|
wb_props[:date1904] = true if is_date1904
|
|
913
|
+
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
571
914
|
wb_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
572
915
|
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
573
916
|
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
@@ -607,28 +950,141 @@ module Xlsxrb
|
|
|
607
950
|
Ooxml::XmlParser.parse(custom_xml, cu_listener)
|
|
608
951
|
wb_unmapped[:custom_properties] = cu_listener.properties unless cu_listener.properties.empty?
|
|
609
952
|
end
|
|
610
|
-
|
|
611
|
-
|
|
953
|
+
|
|
954
|
+
Elements::Workbook.new(
|
|
612
955
|
sheets: sheets,
|
|
613
956
|
shared_strings: shared_strings,
|
|
614
957
|
styles: styles,
|
|
615
958
|
unmapped_data: wb_unmapped,
|
|
616
959
|
defined_names: defined_names
|
|
617
960
|
)
|
|
961
|
+
ensure
|
|
962
|
+
zip_reader&.close
|
|
963
|
+
end
|
|
964
|
+
end
|
|
618
965
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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
|
|
624
1003
|
end
|
|
625
|
-
|
|
626
|
-
zip_reader
|
|
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?
|
|
627
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
|
+
)
|
|
628
1080
|
end
|
|
1081
|
+
private_class_method :_read_workbook
|
|
629
1082
|
|
|
630
|
-
|
|
631
|
-
|
|
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(...)
|
|
632
1088
|
end
|
|
633
1089
|
|
|
634
1090
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
@@ -786,61 +1242,75 @@ module Xlsxrb
|
|
|
786
1242
|
end
|
|
787
1243
|
sd = { name: ws.name, rows: ws.rows, columns: columns }
|
|
788
1244
|
sd[:charts] = ws.charts unless ws.charts.empty?
|
|
789
|
-
sd[:conditional_formats] = ws.
|
|
1245
|
+
sd[:conditional_formats] = ws.conditional_formats unless ws.conditional_formats.empty?
|
|
790
1246
|
sd[:data_validations] = ws.data_validations unless ws.data_validations.empty?
|
|
791
1247
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
facade&.each { |key, val| sd[key] ||= val }
|
|
795
|
-
|
|
796
|
-
sd[:state] ||= ws.state if ws.respond_to?(:state) && ws.state && ws.state != :visible
|
|
797
|
-
sd[:comments] ||= ws.comments if ws.respond_to?(:comments) && !ws.comments.empty?
|
|
1248
|
+
sd[:state] = ws.state if ws.respond_to?(:state) && ws.state && ws.state != :visible
|
|
1249
|
+
sd[:comments] = ws.comments if ws.respond_to?(:comments) && !ws.comments.empty?
|
|
798
1250
|
if ws.respond_to?(:hyperlinks) && !ws.hyperlinks.empty?
|
|
799
|
-
sd[:hyperlinks]
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1251
|
+
sd[:hyperlinks] = if ws.hyperlinks.is_a?(Hash)
|
|
1252
|
+
ws.hyperlinks.map do |cell_ref, link|
|
|
1253
|
+
entry = if link.is_a?(Hash)
|
|
1254
|
+
link.dup
|
|
1255
|
+
else
|
|
1256
|
+
(link.respond_to?(:to_h) ? link.to_h : { url: link })
|
|
1257
|
+
end
|
|
1258
|
+
entry[:ref] ||= cell_ref
|
|
1259
|
+
entry
|
|
807
1260
|
end
|
|
1261
|
+
else
|
|
1262
|
+
ws.hyperlinks
|
|
1263
|
+
end
|
|
808
1264
|
end
|
|
809
|
-
sd[:sparkline_groups]
|
|
810
|
-
sd[:sheet_properties]
|
|
811
|
-
sd[:sheet_format]
|
|
812
|
-
sd[:protected_ranges]
|
|
813
|
-
sd[:cell_watches]
|
|
814
|
-
sd[:ignored_errors]
|
|
815
|
-
sd[:phonetic_properties]
|
|
816
|
-
sd[:sheet_calc_pr]
|
|
817
|
-
sd[:freeze_pane]
|
|
818
|
-
sd[:split_pane]
|
|
819
|
-
sd[:sheet_view]
|
|
820
|
-
sd[:selection]
|
|
1265
|
+
sd[:sparkline_groups] = ws.sparkline_groups if ws.respond_to?(:sparkline_groups) && !ws.sparkline_groups.empty?
|
|
1266
|
+
sd[:sheet_properties] = ws.sheet_properties if ws.respond_to?(:sheet_properties) && ws.sheet_properties && !ws.sheet_properties.empty?
|
|
1267
|
+
sd[:sheet_format] = ws.sheet_format if ws.respond_to?(:sheet_format) && ws.sheet_format && !ws.sheet_format.empty?
|
|
1268
|
+
sd[:protected_ranges] = ws.protected_ranges.map { |r| r.respond_to?(:to_h) ? r.to_h : r } if ws.respond_to?(:protected_ranges) && !ws.protected_ranges.empty?
|
|
1269
|
+
sd[:cell_watches] = ws.cell_watches.map { |w| w.respond_to?(:to_h) ? w.to_h : w } if ws.respond_to?(:cell_watches) && !ws.cell_watches.empty?
|
|
1270
|
+
sd[:ignored_errors] = ws.ignored_errors if ws.respond_to?(:ignored_errors) && !ws.ignored_errors.empty?
|
|
1271
|
+
sd[:phonetic_properties] = ws.phonetic_properties.respond_to?(:to_h) ? ws.phonetic_properties.to_h : ws.phonetic_properties if ws.respond_to?(:phonetic_properties) && ws.phonetic_properties
|
|
1272
|
+
sd[:sheet_calc_pr] = ws.sheet_calc_properties.respond_to?(:to_h) ? ws.sheet_calc_properties.to_h : ws.sheet_calc_properties if ws.respond_to?(:sheet_calc_properties) && ws.sheet_calc_properties
|
|
1273
|
+
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
|
+
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
|
+
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
|
+
sd[:selection] = ws.selection.respond_to?(:to_h) ? ws.selection.to_h : ws.selection if ws.respond_to?(:selection) && ws.selection
|
|
821
1277
|
sd[:selection] ||= ws.sheet_view[:selection] if ws.respond_to?(:sheet_view) && ws.sheet_view.is_a?(Hash) && ws.sheet_view[:selection]
|
|
822
|
-
sd[:page_setup]
|
|
823
|
-
sd[:page_margins]
|
|
824
|
-
sd[:header_footer]
|
|
825
|
-
sd[:print_options]
|
|
826
|
-
sd[:sheet_protection]
|
|
827
|
-
sd[:tables]
|
|
828
|
-
sd[:merge_cells]
|
|
829
|
-
sd[:auto_filter]
|
|
830
|
-
sd[:
|
|
831
|
-
sd[:
|
|
832
|
-
sd[:
|
|
1278
|
+
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
|
+
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?
|
|
1280
|
+
sd[:header_footer] = ws.header_footer.respond_to?(:to_h) ? ws.header_footer.to_h : ws.header_footer if ws.respond_to?(:header_footer) && ws.header_footer && !ws.header_footer.empty?
|
|
1281
|
+
sd[:print_options] = ws.print_options.respond_to?(:to_h) ? ws.print_options.to_h : ws.print_options if ws.respond_to?(:print_options) && ws.print_options && !ws.print_options.empty?
|
|
1282
|
+
sd[:sheet_protection] = ws.sheet_protection.respond_to?(:to_h) ? ws.sheet_protection.to_h : ws.sheet_protection if ws.respond_to?(:sheet_protection) && ws.sheet_protection && !ws.sheet_protection.empty?
|
|
1283
|
+
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
|
+
sd[:merge_cells] = ws.merged_cells if ws.respond_to?(:merged_cells) && ws.merged_cells && !ws.merged_cells.empty?
|
|
1285
|
+
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?(:to_h) ? fc.to_h : fc } if ws.respond_to?(:filter_columns) && ws.filter_columns && !ws.filter_columns.empty?)
|
|
1287
|
+
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
|
+
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
|
+
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?
|
|
1290
|
+
sd[:threaded_comments] = ws.threaded_comments if ws.respond_to?(:threaded_comments) && ws.threaded_comments && !ws.threaded_comments.empty?
|
|
1291
|
+
sd[:header_footer_images] = ws.header_footer_images if ws.respond_to?(:header_footer_images) && ws.header_footer_images && !ws.header_footer_images.empty?
|
|
1292
|
+
if ws.respond_to?(:row_breaks) && ws.row_breaks && !ws.row_breaks.empty?
|
|
1293
|
+
sd[:row_breaks] = ws.row_breaks.map { |r| r.respond_to?(:to_h) ? r.to_h : r }
|
|
1294
|
+
end
|
|
1295
|
+
if ws.respond_to?(:column_breaks) && ws.column_breaks && !ws.column_breaks.empty?
|
|
1296
|
+
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
|
+
end
|
|
1300
|
+
sd[:background_image] = ws.background_image if ws.respond_to?(:background_image) && ws.background_image
|
|
833
1301
|
if ws.respond_to?(:images) && ws.images && !ws.images.empty?
|
|
834
|
-
sd[:images]
|
|
1302
|
+
sd[:images] = ws.images.map do |img|
|
|
835
1303
|
if img.is_a?(Elements::Image)
|
|
836
1304
|
ext = img.filename ? File.extname(img.filename).delete_prefix(".").downcase : "png"
|
|
837
1305
|
opts = img.unmapped_data ? img.unmapped_data.dup : {}
|
|
838
1306
|
opts[:name] ||= img.name
|
|
839
1307
|
opts[:file_data] = img.data
|
|
840
1308
|
opts[:ext] = ext
|
|
841
|
-
opts[:
|
|
1309
|
+
opts[:from_column] = img.from_column
|
|
1310
|
+
opts[:from_col] = img.from_column
|
|
842
1311
|
opts[:from_row] = img.from_row
|
|
843
|
-
opts[:
|
|
1312
|
+
opts[:to_column] = img.to_column
|
|
1313
|
+
opts[:to_col] = img.to_column
|
|
844
1314
|
opts[:to_row] = img.to_row
|
|
845
1315
|
opts
|
|
846
1316
|
else
|
|
@@ -848,7 +1318,11 @@ module Xlsxrb
|
|
|
848
1318
|
end
|
|
849
1319
|
end
|
|
850
1320
|
end
|
|
851
|
-
sd[:shapes]
|
|
1321
|
+
sd[:shapes] = ws.shapes if ws.respond_to?(:shapes) && ws.shapes && !ws.shapes.empty?
|
|
1322
|
+
|
|
1323
|
+
# Extract facade metadata from unmapped_data as fallback
|
|
1324
|
+
facade = ws.unmapped_data[:facade] if ws.respond_to?(:unmapped_data) && ws.unmapped_data.is_a?(Hash)
|
|
1325
|
+
facade&.each { |key, val| sd[key] = val unless sd.key?(key) }
|
|
852
1326
|
|
|
853
1327
|
sd
|
|
854
1328
|
end
|
|
@@ -873,8 +1347,10 @@ module Xlsxrb
|
|
|
873
1347
|
next unless ws.respond_to?(:print_titles) && ws.print_titles && defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Titles" && (dn[:local_sheet_id] == idx || dn[:local_sheet_name] == sheet_name) }
|
|
874
1348
|
|
|
875
1349
|
pt = ws.print_titles
|
|
1350
|
+
pt = pt.to_h if pt.is_a?(Elements::MetadataRecord)
|
|
876
1351
|
parts = []
|
|
877
|
-
|
|
1352
|
+
col_val = pt[:columns] || pt[:cols]
|
|
1353
|
+
parts << "'#{sheet_name}'!$#{col_val.sub(":", ":$")}" if col_val
|
|
878
1354
|
parts << "'#{sheet_name}'!$#{pt[:rows].sub(":", ":$")}" if pt[:rows]
|
|
879
1355
|
defined_names << { name: "_xlnm.Print_Titles", value: parts.join(","), local_sheet_id: idx } unless parts.empty?
|
|
880
1356
|
end
|
|
@@ -921,6 +1397,39 @@ module Xlsxrb
|
|
|
921
1397
|
end
|
|
922
1398
|
end
|
|
923
1399
|
|
|
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
|
+
|
|
924
1433
|
# Modifies an existing XLSX file in-memory using an immutable transformation block.
|
|
925
1434
|
#
|
|
926
1435
|
# Reads the workbook, passes it to the block, and writes the resulting workbook.
|
|
@@ -936,14 +1445,17 @@ module Xlsxrb
|
|
|
936
1445
|
#
|
|
937
1446
|
# @param source [String, IO, StringIO] The source file path or IO object.
|
|
938
1447
|
# @param target [String, IO, StringIO, nil] Target file path or IO object (overwrites source if nil).
|
|
939
|
-
# @param password [String, nil] Optional password for encrypted files.
|
|
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).
|
|
940
1451
|
# @yield [workbook]
|
|
941
1452
|
# @yieldparam workbook [Elements::Workbook] The loaded workbook.
|
|
942
1453
|
# @yieldreturn [Elements::Workbook] The modified workbook.
|
|
943
|
-
# @return [
|
|
1454
|
+
# @return [Elements::Workbook] The modified workbook returned by the block.
|
|
944
1455
|
# @api public
|
|
945
|
-
#: (untyped source, ?untyped target, ?password: String?) ?{ (Elements::Workbook) -> untyped } ->
|
|
946
|
-
def self.modify(source, target = nil, password: nil)
|
|
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
|
|
947
1459
|
raise Error, "source is required" if source.nil?
|
|
948
1460
|
raise Error, "block is required" unless block_given?
|
|
949
1461
|
|
|
@@ -965,8 +1477,12 @@ module Xlsxrb
|
|
|
965
1477
|
|
|
966
1478
|
replaced_entries = {}
|
|
967
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]
|
|
968
1484
|
|
|
969
|
-
Ooxml::ZipReader.open(prepared_source) do |zip_reader|
|
|
1485
|
+
Ooxml::ZipReader.open(prepared_source, **zip_options) do |zip_reader|
|
|
970
1486
|
workbook_path = resolve_workbook_entry(zip_reader)
|
|
971
1487
|
wb_dir = File.dirname(workbook_path)
|
|
972
1488
|
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
@@ -996,12 +1512,28 @@ module Xlsxrb
|
|
|
996
1512
|
next if non_worksheet_relationship?(rel_types[r_id], target_path)
|
|
997
1513
|
|
|
998
1514
|
sheet_path = resolve_sheet_entry(zip_reader, target_path, wb_dir, ws_idx)
|
|
1515
|
+
current_ws_idx = ws_idx
|
|
999
1516
|
ws_idx += 1
|
|
1000
1517
|
next unless sheet_path
|
|
1001
1518
|
|
|
1002
1519
|
sheet_entry_map[sheet_info[:name]] = sheet_path
|
|
1003
1520
|
sheet_xml = zip_reader.read_entry(sheet_path) || ""
|
|
1004
|
-
|
|
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
|
+
)
|
|
1005
1537
|
end
|
|
1006
1538
|
|
|
1007
1539
|
mod_unmapped = {}
|
|
@@ -1023,15 +1555,23 @@ module Xlsxrb
|
|
|
1023
1555
|
)
|
|
1024
1556
|
mutable_shared_strings = shared_strings.dup
|
|
1025
1557
|
|
|
1026
|
-
|
|
1027
|
-
|
|
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
|
|
1028
1567
|
|
|
1029
1568
|
structural_change = result_workbook.sheets.size != original_workbook.sheets.size ||
|
|
1030
1569
|
result_workbook.sheets.map(&:name) != original_workbook.sheets.map(&:name)
|
|
1031
1570
|
|
|
1032
1571
|
if structural_change
|
|
1033
1572
|
write_target = target || source
|
|
1034
|
-
|
|
1573
|
+
write_opts = opts.slice(:encryption_mode, :compression_level, :shared_strings)
|
|
1574
|
+
write(write_target, result_workbook, password: write_password, **write_opts)
|
|
1035
1575
|
else
|
|
1036
1576
|
result_workbook.sheets.each_with_index do |new_sheet, idx|
|
|
1037
1577
|
orig_sheet = original_workbook.sheets[idx]
|
|
@@ -1109,7 +1649,7 @@ module Xlsxrb
|
|
|
1109
1649
|
write_dest,
|
|
1110
1650
|
zip_reader,
|
|
1111
1651
|
replaced_entries,
|
|
1112
|
-
password:
|
|
1652
|
+
password: write_password
|
|
1113
1653
|
)
|
|
1114
1654
|
end
|
|
1115
1655
|
end
|
|
@@ -1127,6 +1667,7 @@ module Xlsxrb
|
|
|
1127
1667
|
source.rewind
|
|
1128
1668
|
end
|
|
1129
1669
|
end
|
|
1670
|
+
block_result
|
|
1130
1671
|
ensure
|
|
1131
1672
|
if temp_target_path && File.exist?(temp_target_path)
|
|
1132
1673
|
begin
|
|
@@ -1149,15 +1690,17 @@ module Xlsxrb
|
|
|
1149
1690
|
# end
|
|
1150
1691
|
#
|
|
1151
1692
|
# @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
|
+
# @param default_font [Hash, nil] Optional default font options.
|
|
1152
1695
|
# @yield [builder]
|
|
1153
1696
|
# @yieldparam builder [Xlsxrb::WorkbookBuilder]
|
|
1154
1697
|
# @return [Elements::Workbook] The compiled in-memory workbook.
|
|
1155
1698
|
# @api public
|
|
1156
|
-
#: (?strict_excel_mode: bool, ?default_font: untyped?) ?{ (WorkbookBuilder) -> void } -> Elements::Workbook
|
|
1157
|
-
def self.build(strict_excel_mode: true, default_font: nil)
|
|
1699
|
+
#: (?strict_excel_mode: bool, ?escape_formulas: bool, ?default_font: untyped?) ?{ (WorkbookBuilder) -> void } -> Elements::Workbook
|
|
1700
|
+
def self.build(strict_excel_mode: true, escape_formulas: false, default_font: nil)
|
|
1158
1701
|
raise Error, "block is required" unless block_given?
|
|
1159
1702
|
|
|
1160
|
-
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode, default_font: default_font)
|
|
1703
|
+
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, default_font: default_font)
|
|
1161
1704
|
yield builder
|
|
1162
1705
|
builder.build
|
|
1163
1706
|
end
|
|
@@ -1243,20 +1786,29 @@ module Xlsxrb
|
|
|
1243
1786
|
fallbacks.find { |f| zip_reader.entry?(f) }
|
|
1244
1787
|
end
|
|
1245
1788
|
|
|
1246
|
-
#: (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]?, ?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_pr: Hash[Symbol, untyped]?, ?background_image: Hash[Symbol, untyped]?, ?threaded_comments: Array[Hash[Symbol, untyped]]?) -> Elements::Worksheet
|
|
1247
|
-
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, col_breaks: nil, protected_ranges: nil, cell_watches: nil, ignored_errors: nil, phonetic_properties: nil, selection: nil, sheet_calc_pr: nil, background_image: nil, threaded_comments: nil)
|
|
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]?, ?sheet_calc_pr: 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
|
|
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, col_breaks: nil, protected_ranges: nil, cell_watches: nil, ignored_errors: nil, phonetic_properties: nil, selection: nil, sheet_calc_properties: nil, sheet_calc_pr: 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)
|
|
1248
1791
|
sheet_dimension = dimension || sheet_xml&.slice(/<(?:[a-zA-Z0-9_]+:)?dimension\b[^>]*\bref=["']([^"']+)["']/, 1)
|
|
1792
|
+
resolved_calc_pr = sheet_calc_properties || sheet_calc_pr
|
|
1249
1793
|
if sheet_xml.nil? || sheet_xml.empty?
|
|
1250
1794
|
return Elements::Worksheet.new(
|
|
1251
1795
|
name: name, state: state, styles: styles, images: images || [], date1904: date1904,
|
|
1252
1796
|
dimension: sheet_dimension, trim_empty_rows: trim_empty_rows, pad_empty_rows: pad_empty_rows, pad_empty_cells: pad_empty_cells,
|
|
1797
|
+
columns: columns || [],
|
|
1798
|
+
charts: charts || [],
|
|
1799
|
+
sparkline_groups: sparkline_groups || [],
|
|
1800
|
+
print_area: print_area,
|
|
1801
|
+
print_titles: print_titles,
|
|
1253
1802
|
merged_cells: merged_cells || [], auto_filter: auto_filter,
|
|
1254
1803
|
tables: tables || [], shapes: shapes || [], freeze_pane: freeze_pane,
|
|
1804
|
+
split_pane: split_pane, pivot_tables: pivot_tables || [],
|
|
1805
|
+
filter_columns: filter_columns || {}, sort_state: sort_state,
|
|
1806
|
+
header_footer_images: header_footer_images || [],
|
|
1255
1807
|
page_setup: page_setup || {}, page_margins: page_margins, header_footer: header_footer || {},
|
|
1256
1808
|
print_options: print_options || {}, sheet_protection: sheet_protection, sheet_view: sheet_view || {},
|
|
1257
|
-
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [],
|
|
1809
|
+
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [], column_breaks: column_breaks || col_breaks || [],
|
|
1258
1810
|
protected_ranges: protected_ranges || [], cell_watches: cell_watches || [], ignored_errors: ignored_errors || [],
|
|
1259
|
-
phonetic_properties: phonetic_properties, selection: selection,
|
|
1811
|
+
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties: resolved_calc_pr,
|
|
1260
1812
|
background_image: background_image,
|
|
1261
1813
|
threaded_comments: threaded_comments || []
|
|
1262
1814
|
)
|
|
@@ -1270,7 +1822,7 @@ module Xlsxrb
|
|
|
1270
1822
|
raw_columns = Ooxml::WorksheetParser.parse_columns(sheet_xml)
|
|
1271
1823
|
|
|
1272
1824
|
rows = raw_rows.map { |rr| build_row_from_raw(rr, sheet_hyperlinks, sheet_comments, styles, date1904: date1904, threaded_comments: sheet_threaded_comments) }
|
|
1273
|
-
|
|
1825
|
+
sheet_columns = columns || raw_columns.map do |rc|
|
|
1274
1826
|
# Columns from OOXML are 1-based min/max ranges; convert to 0-based
|
|
1275
1827
|
col_unmapped = {}
|
|
1276
1828
|
col_unmapped[:style_index] = rc[:style_index] if rc[:style_index]
|
|
@@ -1311,16 +1863,27 @@ module Xlsxrb
|
|
|
1311
1863
|
[]
|
|
1312
1864
|
end
|
|
1313
1865
|
|
|
1314
|
-
sheet_auto_filter = auto_filter
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1866
|
+
sheet_auto_filter = auto_filter
|
|
1867
|
+
sheet_filter_columns = filter_columns
|
|
1868
|
+
if sheet_auto_filter.nil? && sheet_xml.include?("<autoFilter")
|
|
1869
|
+
af_listener = Ooxml::Reader::AutoFilterListener.new
|
|
1870
|
+
Ooxml::XmlParser.parse(sheet_xml, af_listener)
|
|
1871
|
+
sheet_auto_filter = af_listener.ref
|
|
1872
|
+
sheet_filter_columns ||= af_listener.filter_columns
|
|
1873
|
+
end
|
|
1874
|
+
sheet_filter_columns ||= {}
|
|
1875
|
+
|
|
1876
|
+
sheet_sort_state = sort_state || if sheet_xml.include?("<sortState")
|
|
1877
|
+
ss_listener = Ooxml::Reader::SortStateListener.new
|
|
1878
|
+
Ooxml::XmlParser.parse(sheet_xml, ss_listener)
|
|
1879
|
+
ss_listener.sort_state
|
|
1880
|
+
end
|
|
1319
1881
|
|
|
1320
1882
|
sheet_tables = tables || resolve_tables(zip_reader: zip_reader, entry_name: entry_name)
|
|
1883
|
+
sheet_pivot_tables = pivot_tables || resolve_pivot_tables(zip_reader: zip_reader, entry_name: entry_name)
|
|
1321
1884
|
sheet_shapes = shapes || resolve_shapes(zip_reader: zip_reader, entry_name: entry_name)
|
|
1322
1885
|
|
|
1323
|
-
sheet_view_data = if freeze_pane.nil? || sheet_view.nil? || selection.nil?
|
|
1886
|
+
sheet_view_data = if freeze_pane.nil? || split_pane.nil? || sheet_view.nil? || selection.nil?
|
|
1324
1887
|
if sheet_xml.include?("<sheetView")
|
|
1325
1888
|
v_listener = Ooxml::Reader::SheetViewListener.new
|
|
1326
1889
|
Ooxml::XmlParser.parse(sheet_xml, v_listener)
|
|
@@ -1329,9 +1892,10 @@ module Xlsxrb
|
|
|
1329
1892
|
{ view: {}, pane: nil, selection: nil }
|
|
1330
1893
|
end
|
|
1331
1894
|
else
|
|
1332
|
-
{ view: sheet_view, pane: freeze_pane, selection: selection }
|
|
1895
|
+
{ view: sheet_view, pane: freeze_pane || split_pane, selection: selection }
|
|
1333
1896
|
end
|
|
1334
|
-
sheet_freeze_pane = freeze_pane || sheet_view_data[:pane]
|
|
1897
|
+
sheet_freeze_pane = freeze_pane || (sheet_view_data[:pane] if sheet_view_data[:pane] && sheet_view_data[:pane][:state] == :frozen)
|
|
1898
|
+
sheet_split_pane = split_pane || (sheet_view_data[:pane] if sheet_view_data[:pane] && sheet_view_data[:pane][:state] != :frozen)
|
|
1335
1899
|
sheet_view_val = sheet_view || sheet_view_data[:view] || {}
|
|
1336
1900
|
sheet_selection = selection || sheet_view_data[:selection]
|
|
1337
1901
|
|
|
@@ -1357,7 +1921,7 @@ module Xlsxrb
|
|
|
1357
1921
|
{}
|
|
1358
1922
|
end
|
|
1359
1923
|
|
|
1360
|
-
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? || col_breaks.nil?
|
|
1924
|
+
print_data = if page_setup.nil? || page_margins.nil? || header_footer.nil? || print_options.nil? || row_breaks.nil? || (column_breaks.nil? && col_breaks.nil?)
|
|
1361
1925
|
if sheet_xml.match?(/<(?:pageSetup|pageMargins|headerFooter|printOptions|rowBreaks|colBreaks)\b/)
|
|
1362
1926
|
pg_listener = Ooxml::Reader::PrintPageListener.new
|
|
1363
1927
|
Ooxml::XmlParser.parse(sheet_xml, pg_listener)
|
|
@@ -1367,13 +1931,15 @@ module Xlsxrb
|
|
|
1367
1931
|
header_footer: pg_listener.header_footer,
|
|
1368
1932
|
print_options: pg_listener.print_options,
|
|
1369
1933
|
row_breaks: pg_listener.row_breaks,
|
|
1934
|
+
column_breaks: pg_listener.column_breaks,
|
|
1370
1935
|
col_breaks: pg_listener.col_breaks
|
|
1371
1936
|
}
|
|
1372
1937
|
else
|
|
1373
|
-
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], col_breaks: [] }
|
|
1938
|
+
{ page_margins: nil, page_setup: {}, header_footer: {}, print_options: {}, row_breaks: [], column_breaks: [], col_breaks: [] }
|
|
1374
1939
|
end
|
|
1375
1940
|
else
|
|
1376
|
-
|
|
1941
|
+
cb = column_breaks || col_breaks
|
|
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 }
|
|
1377
1943
|
end
|
|
1378
1944
|
|
|
1379
1945
|
sheet_prot_ranges = protected_ranges || if sheet_xml.include?("<protectedRanges")
|
|
@@ -1406,11 +1972,19 @@ module Xlsxrb
|
|
|
1406
1972
|
pp_listener.phonetic_pr
|
|
1407
1973
|
end
|
|
1408
1974
|
|
|
1975
|
+
sheet_charts = charts || resolve_charts(zip_reader: zip_reader, entry_name: entry_name)
|
|
1976
|
+
sheet_sparkline_groups = sparkline_groups || (sheet_xml.include?("sparklineGroup") ? resolve_sparkline_groups(sheet_xml) : [])
|
|
1977
|
+
sheet_hf_images = header_footer_images || (sheet_xml.include?("legacyDrawingHF") ? resolve_header_footer_images(sheet_xml, zip_reader: zip_reader, entry_name: entry_name) : [])
|
|
1978
|
+
|
|
1409
1979
|
Elements::Worksheet.new(
|
|
1410
1980
|
name: name,
|
|
1411
1981
|
rows: rows,
|
|
1412
|
-
columns:
|
|
1413
|
-
|
|
1982
|
+
columns: sheet_columns,
|
|
1983
|
+
charts: sheet_charts,
|
|
1984
|
+
sparkline_groups: sheet_sparkline_groups,
|
|
1985
|
+
print_area: print_area,
|
|
1986
|
+
print_titles: print_titles,
|
|
1987
|
+
conditional_formats: cfs,
|
|
1414
1988
|
data_validations: dvs,
|
|
1415
1989
|
state: state,
|
|
1416
1990
|
hyperlinks: sheet_hyperlinks,
|
|
@@ -1425,8 +1999,13 @@ module Xlsxrb
|
|
|
1425
1999
|
merged_cells: sheet_merged_cells,
|
|
1426
2000
|
auto_filter: sheet_auto_filter,
|
|
1427
2001
|
tables: sheet_tables,
|
|
2002
|
+
pivot_tables: sheet_pivot_tables,
|
|
1428
2003
|
shapes: sheet_shapes,
|
|
1429
2004
|
freeze_pane: sheet_freeze_pane,
|
|
2005
|
+
split_pane: sheet_split_pane,
|
|
2006
|
+
filter_columns: sheet_filter_columns,
|
|
2007
|
+
sort_state: sheet_sort_state,
|
|
2008
|
+
header_footer_images: sheet_hf_images,
|
|
1430
2009
|
page_setup: page_setup || print_data[:page_setup] || {},
|
|
1431
2010
|
page_margins: page_margins || print_data[:page_margins],
|
|
1432
2011
|
header_footer: header_footer || print_data[:header_footer] || {},
|
|
@@ -1436,13 +2015,13 @@ module Xlsxrb
|
|
|
1436
2015
|
sheet_properties: sheet_props,
|
|
1437
2016
|
sheet_format: sheet_fmt,
|
|
1438
2017
|
row_breaks: row_breaks || print_data[:row_breaks] || [],
|
|
1439
|
-
|
|
2018
|
+
column_breaks: column_breaks || col_breaks || print_data[:column_breaks] || print_data[:col_breaks] || [],
|
|
1440
2019
|
protected_ranges: sheet_prot_ranges,
|
|
1441
2020
|
cell_watches: sheet_watches,
|
|
1442
2021
|
ignored_errors: sheet_ignored_errors,
|
|
1443
2022
|
phonetic_properties: sheet_phonetic,
|
|
1444
2023
|
selection: sheet_selection,
|
|
1445
|
-
|
|
2024
|
+
sheet_calc_properties: resolved_calc_pr,
|
|
1446
2025
|
background_image: background_image || (sheet_xml.include?("<picture") ? resolve_background_image(sheet_xml, zip_reader: zip_reader, entry_name: entry_name) : nil),
|
|
1447
2026
|
threaded_comments: sheet_threaded_comments
|
|
1448
2027
|
)
|
|
@@ -1474,7 +2053,14 @@ module Xlsxrb
|
|
|
1474
2053
|
entry[:display] = link[:display] if link[:display]
|
|
1475
2054
|
entry[:tooltip] = link[:tooltip] if link[:tooltip]
|
|
1476
2055
|
entry[:location] = link[:location] if link[:location]
|
|
1477
|
-
|
|
2056
|
+
next if entry.empty?
|
|
2057
|
+
|
|
2058
|
+
result[link[:ref]] = Elements::Hyperlink.new(
|
|
2059
|
+
url: entry[:url],
|
|
2060
|
+
location: entry[:location],
|
|
2061
|
+
tooltip: entry[:tooltip],
|
|
2062
|
+
display: entry[:display]
|
|
2063
|
+
)
|
|
1478
2064
|
end
|
|
1479
2065
|
result
|
|
1480
2066
|
end
|
|
@@ -1637,9 +2223,9 @@ module Xlsxrb
|
|
|
1637
2223
|
img_data = zip_reader.read_entry(media_path)
|
|
1638
2224
|
end
|
|
1639
2225
|
|
|
1640
|
-
from_col = raw_img[:from_col] || 0
|
|
2226
|
+
from_col = raw_img[:from_column] || raw_img[:from_col] || 0
|
|
1641
2227
|
from_row = raw_img[:from_row] || 0
|
|
1642
|
-
to_col = raw_img[:to_col]
|
|
2228
|
+
to_col = raw_img[:to_column] || raw_img[:to_col]
|
|
1643
2229
|
to_row = raw_img[:to_row]
|
|
1644
2230
|
cx = raw_img[:cx]
|
|
1645
2231
|
cy = raw_img[:cy]
|
|
@@ -1649,9 +2235,9 @@ module Xlsxrb
|
|
|
1649
2235
|
filename: filename || "image.png",
|
|
1650
2236
|
name: name,
|
|
1651
2237
|
data: img_data,
|
|
1652
|
-
|
|
2238
|
+
from_column: from_col,
|
|
1653
2239
|
from_row: from_row,
|
|
1654
|
-
|
|
2240
|
+
to_column: to_col,
|
|
1655
2241
|
to_row: to_row,
|
|
1656
2242
|
cx: cx,
|
|
1657
2243
|
cy: cy,
|
|
@@ -1764,31 +2350,551 @@ module Xlsxrb
|
|
|
1764
2350
|
d_listener.shapes
|
|
1765
2351
|
end
|
|
1766
2352
|
|
|
1767
|
-
#: (
|
|
2353
|
+
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Hash[Symbol, untyped]]
|
|
2354
|
+
def resolve_pivot_tables(zip_reader: nil, entry_name: nil)
|
|
2355
|
+
return [] unless zip_reader && entry_name
|
|
2356
|
+
|
|
2357
|
+
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2358
|
+
rels_xml = zip_reader.read_entry(rels_path)
|
|
2359
|
+
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2360
|
+
|
|
2361
|
+
parser = REXML::Parsers::SAX2Parser.new(rels_xml)
|
|
2362
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2363
|
+
parser.listen(listener)
|
|
2364
|
+
parser.parse
|
|
2365
|
+
|
|
2366
|
+
pivot_rels = listener.relationships.select { |r| r[:type]&.end_with?("/pivotTable") }
|
|
2367
|
+
return [] if pivot_rels.empty?
|
|
2368
|
+
|
|
2369
|
+
base_dir = File.dirname(entry_name)
|
|
2370
|
+
pivot_rels.filter_map do |rel|
|
|
2371
|
+
pt_path = resolve_relative_path(base_dir, rel[:target])
|
|
2372
|
+
pt_xml = zip_reader.read_entry(pt_path)
|
|
2373
|
+
next if pt_xml.nil? || pt_xml.empty?
|
|
2374
|
+
|
|
2375
|
+
p_parser = REXML::Parsers::SAX2Parser.new(pt_xml)
|
|
2376
|
+
p_listener = Ooxml::Reader::PivotTableListener.new
|
|
2377
|
+
p_parser.listen(p_listener)
|
|
2378
|
+
p_parser.parse
|
|
2379
|
+
pt = p_listener.pivot_table
|
|
2380
|
+
next unless pt
|
|
2381
|
+
|
|
2382
|
+
pt_rels_path = pt_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2383
|
+
pt_rels_xml = zip_reader.read_entry(pt_rels_path)
|
|
2384
|
+
if pt_rels_xml && !pt_rels_xml.empty?
|
|
2385
|
+
c_parser = REXML::Parsers::SAX2Parser.new(pt_rels_xml)
|
|
2386
|
+
c_listener = Ooxml::Reader::RelsListener.new
|
|
2387
|
+
c_parser.listen(c_listener)
|
|
2388
|
+
c_parser.parse
|
|
2389
|
+
cache_rel = c_listener.relationships.find { |r| r[:type]&.end_with?("/pivotCacheDefinition") }
|
|
2390
|
+
if cache_rel
|
|
2391
|
+
c_path = resolve_relative_path(File.dirname(pt_path), cache_rel[:target])
|
|
2392
|
+
c_xml = zip_reader.read_entry(c_path)
|
|
2393
|
+
if c_xml && !c_xml.empty?
|
|
2394
|
+
cd_parser = REXML::Parsers::SAX2Parser.new(c_xml)
|
|
2395
|
+
cd_listener = Ooxml::Reader::PivotCacheDefinitionListener.new
|
|
2396
|
+
cd_parser.listen(cd_listener)
|
|
2397
|
+
cd_parser.parse
|
|
2398
|
+
cache_info = cd_listener.cache_definition
|
|
2399
|
+
if cache_info
|
|
2400
|
+
cd_rels_path = c_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2401
|
+
cd_rels_xml = zip_reader.read_entry(cd_rels_path)
|
|
2402
|
+
if cd_rels_xml && !cd_rels_xml.empty?
|
|
2403
|
+
rec_parser = REXML::Parsers::SAX2Parser.new(cd_rels_xml)
|
|
2404
|
+
rec_rels_listener = Ooxml::Reader::RelsListener.new
|
|
2405
|
+
rec_parser.listen(rec_rels_listener)
|
|
2406
|
+
rec_parser.parse
|
|
2407
|
+
rec_rel = rec_rels_listener.relationships.find { |r| r[:type]&.end_with?("/pivotCacheRecords") }
|
|
2408
|
+
if rec_rel
|
|
2409
|
+
rec_path = resolve_relative_path(File.dirname(c_path), rec_rel[:target])
|
|
2410
|
+
rec_xml = zip_reader.read_entry(rec_path)
|
|
2411
|
+
if rec_xml && !rec_xml.empty?
|
|
2412
|
+
r_parser = REXML::Parsers::SAX2Parser.new(rec_xml)
|
|
2413
|
+
r_listener = Ooxml::Reader::PivotCacheRecordsListener.new
|
|
2414
|
+
r_parser.listen(r_listener)
|
|
2415
|
+
r_parser.parse
|
|
2416
|
+
cache_info[:records] = r_listener.records unless r_listener.records.empty?
|
|
2417
|
+
end
|
|
2418
|
+
end
|
|
2419
|
+
end
|
|
2420
|
+
pt[:cache] = cache_info
|
|
2421
|
+
end
|
|
2422
|
+
end
|
|
2423
|
+
end
|
|
2424
|
+
end
|
|
2425
|
+
pt
|
|
2426
|
+
end
|
|
2427
|
+
end
|
|
2428
|
+
|
|
2429
|
+
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Elements::Chart]
|
|
2430
|
+
def resolve_charts(zip_reader: nil, entry_name: nil)
|
|
2431
|
+
return [] unless zip_reader && entry_name
|
|
2432
|
+
|
|
2433
|
+
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2434
|
+
rels_xml = zip_reader.read_entry(rels_path)
|
|
2435
|
+
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2436
|
+
|
|
2437
|
+
parser = REXML::Parsers::SAX2Parser.new(rels_xml)
|
|
2438
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2439
|
+
parser.listen(listener)
|
|
2440
|
+
parser.parse
|
|
2441
|
+
|
|
2442
|
+
drawing_rel = listener.relationships.find { |r| r[:type]&.end_with?("/drawing") }
|
|
2443
|
+
return [] unless drawing_rel
|
|
2444
|
+
|
|
2445
|
+
base_dir = File.dirname(entry_name)
|
|
2446
|
+
drawing_path = resolve_relative_path(base_dir, drawing_rel[:target])
|
|
2447
|
+
drawing_xml = zip_reader.read_entry(drawing_path)
|
|
2448
|
+
return [] if drawing_xml.nil? || drawing_xml.empty?
|
|
2449
|
+
|
|
2450
|
+
drawing_rels_path = drawing_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2451
|
+
drawing_rels_xml = zip_reader.read_entry(drawing_rels_path)
|
|
2452
|
+
drawing_rels = {}
|
|
2453
|
+
if drawing_rels_xml && !drawing_rels_xml.empty?
|
|
2454
|
+
d_parser = REXML::Parsers::SAX2Parser.new(drawing_rels_xml)
|
|
2455
|
+
d_listener = Ooxml::Reader::RelsListener.new
|
|
2456
|
+
d_parser.listen(d_listener)
|
|
2457
|
+
d_parser.parse
|
|
2458
|
+
d_listener.relationships.each do |r|
|
|
2459
|
+
drawing_rels[r[:id]] = r[:target]
|
|
2460
|
+
end
|
|
2461
|
+
end
|
|
2462
|
+
|
|
2463
|
+
d_parser = REXML::Parsers::SAX2Parser.new(drawing_xml)
|
|
2464
|
+
dc_listener = Ooxml::Reader::DrawingChartsListener.new
|
|
2465
|
+
d_parser.listen(dc_listener)
|
|
2466
|
+
d_parser.parse
|
|
2467
|
+
|
|
2468
|
+
drawing_dir = File.dirname(drawing_path)
|
|
2469
|
+
dc_listener.charts.map do |raw_chart|
|
|
2470
|
+
chart = raw_chart.dup
|
|
2471
|
+
target = drawing_rels[chart[:rid]]
|
|
2472
|
+
if target
|
|
2473
|
+
chart[:target] = target
|
|
2474
|
+
chart_path = resolve_relative_path(drawing_dir, target)
|
|
2475
|
+
chart_xml = zip_reader.read_entry(chart_path)
|
|
2476
|
+
if chart_xml && !chart_xml.empty?
|
|
2477
|
+
cp = REXML::Parsers::SAX2Parser.new(chart_xml)
|
|
2478
|
+
cl = Ooxml::Reader::ChartTypeListener.new
|
|
2479
|
+
cp.listen(cl)
|
|
2480
|
+
cp.parse
|
|
2481
|
+
chart[:chart_type] = cl.chart_type
|
|
2482
|
+
chart[:type] = cl.chart_type
|
|
2483
|
+
chart[:title] = cl.title
|
|
2484
|
+
chart[:title_overlay] = cl.title_overlay unless cl.title_overlay.nil?
|
|
2485
|
+
chart[:title_font] = cl.title_font if cl.title_font
|
|
2486
|
+
chart[:title_fill_color] = cl.title_fill_color if cl.title_fill_color
|
|
2487
|
+
chart[:title_no_fill] = cl.title_no_fill if cl.title_no_fill
|
|
2488
|
+
chart[:title_line_color] = cl.title_line_color if cl.title_line_color
|
|
2489
|
+
chart[:title_line_width] = cl.title_line_width if cl.title_line_width
|
|
2490
|
+
chart[:title_line_dash] = cl.title_line_dash if cl.title_line_dash
|
|
2491
|
+
chart[:series] = cl.series unless cl.series.empty?
|
|
2492
|
+
chart[:legend] = cl.legend unless cl.legend.empty?
|
|
2493
|
+
if cl.legend_font
|
|
2494
|
+
chart[:legend] ||= {}
|
|
2495
|
+
chart[:legend][:font] = cl.legend_font
|
|
2496
|
+
end
|
|
2497
|
+
chart[:data_labels] = cl.data_labels unless cl.data_labels.empty?
|
|
2498
|
+
chart[:cat_axis_title] = cl.cat_axis_title if cl.cat_axis_title
|
|
2499
|
+
chart[:val_axis_title] = cl.val_axis_title if cl.val_axis_title
|
|
2500
|
+
chart[:cat_axis_title_font] = cl.cat_axis_title_font if cl.cat_axis_title_font
|
|
2501
|
+
chart[:val_axis_title_font] = cl.val_axis_title_font if cl.val_axis_title_font
|
|
2502
|
+
chart[:cat_axis_title_fill] = cl.cat_axis_title_fill if cl.cat_axis_title_fill
|
|
2503
|
+
chart[:cat_axis_title_no_fill] = cl.cat_axis_title_no_fill if cl.cat_axis_title_no_fill
|
|
2504
|
+
chart[:cat_axis_title_line_color] = cl.cat_axis_title_line_color if cl.cat_axis_title_line_color
|
|
2505
|
+
chart[:cat_axis_title_line_width] = cl.cat_axis_title_line_width if cl.cat_axis_title_line_width
|
|
2506
|
+
chart[:cat_axis_title_line_dash] = cl.cat_axis_title_line_dash if cl.cat_axis_title_line_dash
|
|
2507
|
+
chart[:val_axis_title_fill] = cl.val_axis_title_fill if cl.val_axis_title_fill
|
|
2508
|
+
chart[:val_axis_title_no_fill] = cl.val_axis_title_no_fill if cl.val_axis_title_no_fill
|
|
2509
|
+
chart[:val_axis_title_line_color] = cl.val_axis_title_line_color if cl.val_axis_title_line_color
|
|
2510
|
+
chart[:val_axis_title_line_width] = cl.val_axis_title_line_width if cl.val_axis_title_line_width
|
|
2511
|
+
chart[:val_axis_title_line_dash] = cl.val_axis_title_line_dash if cl.val_axis_title_line_dash
|
|
2512
|
+
chart[:title_layout] = cl.title_layout if cl.title_layout
|
|
2513
|
+
chart[:cat_axis_title_layout] = cl.cat_axis_title_layout if cl.cat_axis_title_layout
|
|
2514
|
+
chart[:val_axis_title_layout] = cl.val_axis_title_layout if cl.val_axis_title_layout
|
|
2515
|
+
chart[:title_rotation] = cl.title_rotation if cl.title_rotation
|
|
2516
|
+
chart[:cat_axis_title_rotation] = cl.cat_axis_title_rotation if cl.cat_axis_title_rotation
|
|
2517
|
+
chart[:val_axis_title_rotation] = cl.val_axis_title_rotation if cl.val_axis_title_rotation
|
|
2518
|
+
chart[:grouping] = cl.grouping if cl.grouping
|
|
2519
|
+
chart[:bar_dir] = cl.bar_dir if cl.bar_dir
|
|
2520
|
+
chart[:vary_colors] = cl.vary_colors unless cl.vary_colors.nil?
|
|
2521
|
+
chart[:plot_vis_only] = cl.plot_vis_only unless cl.plot_vis_only.nil?
|
|
2522
|
+
chart[:disp_blanks_as] = cl.disp_blanks_as if cl.disp_blanks_as
|
|
2523
|
+
chart[:style] = cl.style if cl.style
|
|
2524
|
+
chart[:auto_title_deleted] = cl.auto_title_deleted unless cl.auto_title_deleted.nil?
|
|
2525
|
+
chart[:rounded_corners] = cl.rounded_corners unless cl.rounded_corners.nil?
|
|
2526
|
+
chart[:cat_axis_tick_lbl_pos] = cl.cat_axis_tick_lbl_pos if cl.cat_axis_tick_lbl_pos
|
|
2527
|
+
chart[:val_axis_tick_lbl_pos] = cl.val_axis_tick_lbl_pos if cl.val_axis_tick_lbl_pos
|
|
2528
|
+
chart[:cat_axis_major_gridlines] = cl.cat_axis_major_gridlines if cl.cat_axis_major_gridlines
|
|
2529
|
+
chart[:val_axis_major_gridlines] = cl.val_axis_major_gridlines if cl.val_axis_major_gridlines
|
|
2530
|
+
chart[:cat_axis_minor_gridlines] = cl.cat_axis_minor_gridlines if cl.cat_axis_minor_gridlines
|
|
2531
|
+
chart[:val_axis_minor_gridlines] = cl.val_axis_minor_gridlines if cl.val_axis_minor_gridlines
|
|
2532
|
+
chart[:show_d_lbls_over_max] = cl.show_d_lbls_over_max unless cl.show_d_lbls_over_max.nil?
|
|
2533
|
+
chart[:cat_axis_delete] = cl.cat_axis_delete unless cl.cat_axis_delete.nil?
|
|
2534
|
+
chart[:val_axis_delete] = cl.val_axis_delete unless cl.val_axis_delete.nil?
|
|
2535
|
+
chart[:cat_axis_orientation] = cl.cat_axis_orientation if cl.cat_axis_orientation
|
|
2536
|
+
chart[:val_axis_orientation] = cl.val_axis_orientation if cl.val_axis_orientation
|
|
2537
|
+
chart[:gap_width] = cl.gap_width if cl.gap_width
|
|
2538
|
+
chart[:overlap] = cl.overlap if cl.overlap
|
|
2539
|
+
chart[:gap_depth] = cl.gap_depth if cl.gap_depth
|
|
2540
|
+
chart[:bar_shape] = cl.bar_shape if cl.bar_shape
|
|
2541
|
+
chart[:bubble_3d] = cl.bubble_3d unless cl.bubble_3d.nil?
|
|
2542
|
+
chart[:bubble_scale] = cl.bubble_scale if cl.bubble_scale
|
|
2543
|
+
chart[:show_neg_bubbles] = cl.show_neg_bubbles unless cl.show_neg_bubbles.nil?
|
|
2544
|
+
chart[:size_represents] = cl.size_represents if cl.size_represents
|
|
2545
|
+
chart[:view_3d] = cl.view_3d if cl.view_3d
|
|
2546
|
+
chart[:cat_axis_num_fmt] = cl.cat_axis_num_fmt if cl.cat_axis_num_fmt
|
|
2547
|
+
chart[:val_axis_num_fmt] = cl.val_axis_num_fmt if cl.val_axis_num_fmt
|
|
2548
|
+
chart[:cat_axis_major_tick_mark] = cl.cat_axis_major_tick_mark if cl.cat_axis_major_tick_mark
|
|
2549
|
+
chart[:cat_axis_minor_tick_mark] = cl.cat_axis_minor_tick_mark if cl.cat_axis_minor_tick_mark
|
|
2550
|
+
chart[:val_axis_major_tick_mark] = cl.val_axis_major_tick_mark if cl.val_axis_major_tick_mark
|
|
2551
|
+
chart[:val_axis_minor_tick_mark] = cl.val_axis_minor_tick_mark if cl.val_axis_minor_tick_mark
|
|
2552
|
+
chart[:cat_axis_crosses] = cl.cat_axis_crosses if cl.cat_axis_crosses
|
|
2553
|
+
chart[:val_axis_crosses] = cl.val_axis_crosses if cl.val_axis_crosses
|
|
2554
|
+
chart[:cat_axis_crosses_at] = cl.cat_axis_crosses_at if cl.cat_axis_crosses_at
|
|
2555
|
+
chart[:val_axis_crosses_at] = cl.val_axis_crosses_at if cl.val_axis_crosses_at
|
|
2556
|
+
chart[:cat_axis_tick_lbl_skip] = cl.cat_axis_tick_lbl_skip if cl.cat_axis_tick_lbl_skip
|
|
2557
|
+
chart[:cat_axis_tick_mark_skip] = cl.cat_axis_tick_mark_skip if cl.cat_axis_tick_mark_skip
|
|
2558
|
+
chart[:cat_axis_lbl_offset] = cl.cat_axis_lbl_offset if cl.cat_axis_lbl_offset
|
|
2559
|
+
chart[:cat_axis_auto] = cl.cat_axis_auto unless cl.cat_axis_auto.nil?
|
|
2560
|
+
chart[:cat_axis_lbl_algn] = cl.cat_axis_lbl_algn if cl.cat_axis_lbl_algn
|
|
2561
|
+
chart[:cat_axis_no_multi_lvl_lbl] = cl.cat_axis_no_multi_lvl_lbl unless cl.cat_axis_no_multi_lvl_lbl.nil?
|
|
2562
|
+
chart[:val_axis_cross_between] = cl.val_axis_cross_between if cl.val_axis_cross_between
|
|
2563
|
+
chart[:val_axis_major_unit] = cl.val_axis_major_unit if cl.val_axis_major_unit
|
|
2564
|
+
chart[:val_axis_minor_unit] = cl.val_axis_minor_unit if cl.val_axis_minor_unit
|
|
2565
|
+
chart[:val_axis_disp_units] = cl.val_axis_disp_units if cl.val_axis_disp_units
|
|
2566
|
+
chart[:cat_axis_scaling_max] = cl.cat_axis_scaling_max if cl.cat_axis_scaling_max
|
|
2567
|
+
chart[:cat_axis_scaling_min] = cl.cat_axis_scaling_min if cl.cat_axis_scaling_min
|
|
2568
|
+
chart[:val_axis_scaling_max] = cl.val_axis_scaling_max if cl.val_axis_scaling_max
|
|
2569
|
+
chart[:val_axis_scaling_min] = cl.val_axis_scaling_min if cl.val_axis_scaling_min
|
|
2570
|
+
chart[:cat_axis_log_base] = cl.cat_axis_log_base if cl.cat_axis_log_base
|
|
2571
|
+
chart[:val_axis_log_base] = cl.val_axis_log_base if cl.val_axis_log_base
|
|
2572
|
+
chart[:first_slice_ang] = cl.first_slice_ang if cl.first_slice_ang
|
|
2573
|
+
chart[:hole_size] = cl.hole_size if cl.hole_size
|
|
2574
|
+
chart[:smooth] = cl.smooth unless cl.smooth.nil?
|
|
2575
|
+
chart[:marker] = cl.marker unless cl.marker.nil?
|
|
2576
|
+
chart[:drop_lines] = cl.drop_lines unless cl.drop_lines.nil?
|
|
2577
|
+
chart[:hi_low_lines] = cl.hi_low_lines unless cl.hi_low_lines.nil?
|
|
2578
|
+
chart[:ser_lines] = cl.ser_lines unless cl.ser_lines.nil?
|
|
2579
|
+
chart[:up_down_bars] = cl.up_down_bars if cl.up_down_bars
|
|
2580
|
+
chart[:scatter_style] = cl.scatter_style if cl.scatter_style
|
|
2581
|
+
chart[:radar_style] = cl.radar_style if cl.radar_style
|
|
2582
|
+
chart[:cat_axis_pos] = cl.cat_axis_pos if cl.cat_axis_pos
|
|
2583
|
+
chart[:val_axis_pos] = cl.val_axis_pos if cl.val_axis_pos
|
|
2584
|
+
chart[:wireframe] = cl.wireframe unless cl.wireframe.nil?
|
|
2585
|
+
chart[:band_fmts] = cl.band_fmts if cl.band_fmts
|
|
2586
|
+
chart[:of_pie_type] = cl.of_pie_type if cl.of_pie_type
|
|
2587
|
+
chart[:split_type] = cl.split_type if cl.split_type
|
|
2588
|
+
chart[:split_pos] = cl.split_pos if cl.split_pos
|
|
2589
|
+
chart[:cust_split] = cl.cust_split if cl.cust_split&.any?
|
|
2590
|
+
chart[:second_pie_size] = cl.second_pie_size if cl.second_pie_size
|
|
2591
|
+
chart[:data_table] = cl.data_table if cl.data_table
|
|
2592
|
+
chart[:plot_area_fill] = cl.plot_area_fill if cl.plot_area_fill
|
|
2593
|
+
chart[:plot_area_line_color] = cl.plot_area_line_color if cl.plot_area_line_color
|
|
2594
|
+
chart[:plot_area_line_width] = cl.plot_area_line_width if cl.plot_area_line_width
|
|
2595
|
+
chart[:plot_area_line_dash] = cl.plot_area_line_dash if cl.plot_area_line_dash
|
|
2596
|
+
chart[:plot_area_no_fill] = cl.plot_area_no_fill if cl.plot_area_no_fill
|
|
2597
|
+
chart[:plot_area_layout] = cl.plot_area_layout if cl.plot_area_layout
|
|
2598
|
+
chart[:cat_axis_label_rotation] = cl.cat_axis_label_rotation if cl.cat_axis_label_rotation
|
|
2599
|
+
chart[:val_axis_label_rotation] = cl.val_axis_label_rotation if cl.val_axis_label_rotation
|
|
2600
|
+
chart[:cat_axis_font] = cl.cat_axis_font if cl.cat_axis_font
|
|
2601
|
+
chart[:val_axis_font] = cl.val_axis_font if cl.val_axis_font
|
|
2602
|
+
chart[:cat_axis_fill] = cl.cat_axis_fill if cl.cat_axis_fill
|
|
2603
|
+
chart[:cat_axis_no_fill] = cl.cat_axis_no_fill if cl.cat_axis_no_fill
|
|
2604
|
+
chart[:val_axis_fill] = cl.val_axis_fill if cl.val_axis_fill
|
|
2605
|
+
chart[:val_axis_no_fill] = cl.val_axis_no_fill if cl.val_axis_no_fill
|
|
2606
|
+
chart[:cat_axis_line_color] = cl.cat_axis_line_color if cl.cat_axis_line_color
|
|
2607
|
+
chart[:cat_axis_line_width] = cl.cat_axis_line_width if cl.cat_axis_line_width
|
|
2608
|
+
chart[:cat_axis_line_dash] = cl.cat_axis_line_dash if cl.cat_axis_line_dash
|
|
2609
|
+
chart[:val_axis_line_color] = cl.val_axis_line_color if cl.val_axis_line_color
|
|
2610
|
+
chart[:val_axis_line_width] = cl.val_axis_line_width if cl.val_axis_line_width
|
|
2611
|
+
chart[:val_axis_line_dash] = cl.val_axis_line_dash if cl.val_axis_line_dash
|
|
2612
|
+
chart[:floor] = cl.floor if cl.floor
|
|
2613
|
+
chart[:side_wall] = cl.side_wall if cl.side_wall
|
|
2614
|
+
chart[:back_wall] = cl.back_wall if cl.back_wall
|
|
2615
|
+
chart[:cat_axis_type] = cl.cat_axis_type if cl.cat_axis_type
|
|
2616
|
+
chart[:cat_axis_base_time_unit] = cl.cat_axis_base_time_unit if cl.cat_axis_base_time_unit
|
|
2617
|
+
chart[:cat_axis_major_time_unit] = cl.cat_axis_major_time_unit if cl.cat_axis_major_time_unit
|
|
2618
|
+
chart[:cat_axis_minor_time_unit] = cl.cat_axis_minor_time_unit if cl.cat_axis_minor_time_unit
|
|
2619
|
+
chart[:cat_axis_major_unit] = cl.cat_axis_major_unit if cl.cat_axis_major_unit
|
|
2620
|
+
chart[:cat_axis_minor_unit] = cl.cat_axis_minor_unit if cl.cat_axis_minor_unit
|
|
2621
|
+
chart[:chart_fill] = cl.chart_fill if cl.chart_fill
|
|
2622
|
+
chart[:chart_no_fill] = cl.chart_no_fill if cl.chart_no_fill
|
|
2623
|
+
chart[:chart_line_color] = cl.chart_line_color if cl.chart_line_color
|
|
2624
|
+
chart[:chart_line_width] = cl.chart_line_width if cl.chart_line_width
|
|
2625
|
+
chart[:chart_line_dash] = cl.chart_line_dash if cl.chart_line_dash
|
|
2626
|
+
chart[:protection] = cl.protection if cl.protection
|
|
2627
|
+
chart[:print_settings] = cl.print_settings if cl.print_settings
|
|
2628
|
+
chart[:chart_font] = cl.chart_font if cl.chart_font
|
|
2629
|
+
ca = {
|
|
2630
|
+
title: cl.cat_axis_title,
|
|
2631
|
+
major_gridlines: cl.cat_axis_major_gridlines,
|
|
2632
|
+
minor_gridlines: cl.cat_axis_minor_gridlines,
|
|
2633
|
+
num_fmt: cl.cat_axis_num_fmt,
|
|
2634
|
+
pos: cl.cat_axis_pos,
|
|
2635
|
+
max: cl.cat_axis_scaling_max,
|
|
2636
|
+
min: cl.cat_axis_scaling_min,
|
|
2637
|
+
major_unit: cl.cat_axis_major_unit,
|
|
2638
|
+
minor_unit: cl.cat_axis_minor_unit,
|
|
2639
|
+
delete: cl.cat_axis_delete
|
|
2640
|
+
}.compact
|
|
2641
|
+
chart[:category_axis] = ca unless ca.empty?
|
|
2642
|
+
|
|
2643
|
+
va = {
|
|
2644
|
+
title: cl.val_axis_title,
|
|
2645
|
+
major_gridlines: cl.val_axis_major_gridlines,
|
|
2646
|
+
minor_gridlines: cl.val_axis_minor_gridlines,
|
|
2647
|
+
num_fmt: cl.val_axis_num_fmt,
|
|
2648
|
+
pos: cl.val_axis_pos,
|
|
2649
|
+
max: cl.val_axis_scaling_max,
|
|
2650
|
+
min: cl.val_axis_scaling_min,
|
|
2651
|
+
major_unit: cl.val_axis_major_unit,
|
|
2652
|
+
minor_unit: cl.val_axis_minor_unit,
|
|
2653
|
+
delete: cl.val_axis_delete
|
|
2654
|
+
}.compact
|
|
2655
|
+
chart[:value_axis] = va unless va.empty?
|
|
2656
|
+
|
|
2657
|
+
chart[:sec_val_axis_title] = cl.sec_val_axis_title if cl.sec_val_axis_title
|
|
2658
|
+
chart[:sec_val_axis_major_gridlines] = cl.sec_val_axis_major_gridlines if cl.sec_val_axis_major_gridlines
|
|
2659
|
+
chart[:sec_val_axis_minor_gridlines] = cl.sec_val_axis_minor_gridlines if cl.sec_val_axis_minor_gridlines
|
|
2660
|
+
chart[:sec_val_axis_num_fmt] = cl.sec_val_axis_num_fmt if cl.sec_val_axis_num_fmt
|
|
2661
|
+
chart[:sec_val_axis_pos] = cl.sec_val_axis_pos if cl.sec_val_axis_pos
|
|
2662
|
+
chart[:sec_val_axis_scaling_max] = cl.sec_val_axis_scaling_max if cl.sec_val_axis_scaling_max
|
|
2663
|
+
chart[:sec_val_axis_scaling_min] = cl.sec_val_axis_scaling_min if cl.sec_val_axis_scaling_min
|
|
2664
|
+
chart[:sec_val_axis_major_unit] = cl.sec_val_axis_major_unit if cl.sec_val_axis_major_unit
|
|
2665
|
+
chart[:sec_val_axis_minor_unit] = cl.sec_val_axis_minor_unit if cl.sec_val_axis_minor_unit
|
|
2666
|
+
if cl.sec_val_axis_title || cl.sec_val_axis_major_gridlines || cl.sec_val_axis_scaling_max ||
|
|
2667
|
+
cl.sec_val_axis_scaling_min || cl.sec_val_axis_num_fmt || cl.sec_val_axis_pos
|
|
2668
|
+
sva = {
|
|
2669
|
+
title: cl.sec_val_axis_title,
|
|
2670
|
+
major_gridlines: cl.sec_val_axis_major_gridlines,
|
|
2671
|
+
minor_gridlines: cl.sec_val_axis_minor_gridlines,
|
|
2672
|
+
num_fmt: cl.sec_val_axis_num_fmt,
|
|
2673
|
+
pos: cl.sec_val_axis_pos,
|
|
2674
|
+
max: cl.sec_val_axis_scaling_max,
|
|
2675
|
+
min: cl.sec_val_axis_scaling_min,
|
|
2676
|
+
major_unit: cl.sec_val_axis_major_unit,
|
|
2677
|
+
minor_unit: cl.sec_val_axis_minor_unit
|
|
2678
|
+
}.compact
|
|
2679
|
+
chart[:secondary_value_axis] = sva unless sva.empty?
|
|
2680
|
+
end
|
|
2681
|
+
end
|
|
2682
|
+
end
|
|
2683
|
+
chart[:from_column] ||= chart[:from_col]
|
|
2684
|
+
chart[:to_column] ||= chart[:to_col]
|
|
2685
|
+
chart[:r_id] ||= chart[:rid]
|
|
2686
|
+
chart[:type] ||= chart[:chart_type]
|
|
2687
|
+
Elements::Chart.new(**chart)
|
|
2688
|
+
end
|
|
2689
|
+
end
|
|
2690
|
+
|
|
2691
|
+
#: (String? sheet_xml) -> Array[Elements::SparklineGroup]
|
|
2692
|
+
def resolve_sparkline_groups(sheet_xml)
|
|
2693
|
+
return [] if sheet_xml.nil? || !sheet_xml.include?("sparklineGroup")
|
|
2694
|
+
|
|
2695
|
+
listener = Ooxml::Reader::SparklineGroupsListener.new
|
|
2696
|
+
parser = REXML::Parsers::SAX2Parser.new(sheet_xml)
|
|
2697
|
+
parser.listen(listener)
|
|
2698
|
+
parser.parse
|
|
2699
|
+
listener.groups.map do |grp|
|
|
2700
|
+
grp.is_a?(Elements::SparklineGroup) ? grp : Elements::SparklineGroup.new(**grp)
|
|
2701
|
+
end
|
|
2702
|
+
end
|
|
2703
|
+
|
|
2704
|
+
#: (String? sheet_xml, ?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Elements::HeaderFooterImage]
|
|
2705
|
+
def resolve_header_footer_images(sheet_xml, zip_reader: nil, entry_name: nil)
|
|
2706
|
+
return [] unless sheet_xml && zip_reader && entry_name
|
|
2707
|
+
return [] unless sheet_xml.include?("legacyDrawingHF")
|
|
2708
|
+
|
|
2709
|
+
m = sheet_xml.match(/<(?:[a-zA-Z0-9_]+:)?legacyDrawingHF\b[^>]*\br:id=["']([^"']+)["']/)
|
|
2710
|
+
return [] unless m
|
|
2711
|
+
|
|
2712
|
+
hf_rid = m[1]
|
|
2713
|
+
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2714
|
+
rels_xml = zip_reader.read_entry(rels_path)
|
|
2715
|
+
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2716
|
+
|
|
2717
|
+
parser = REXML::Parsers::SAX2Parser.new(rels_xml)
|
|
2718
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2719
|
+
parser.listen(listener)
|
|
2720
|
+
parser.parse
|
|
2721
|
+
|
|
2722
|
+
vml_rel = listener.relationships.find { |r| r[:id] == hf_rid }
|
|
2723
|
+
return [] unless vml_rel
|
|
2724
|
+
|
|
2725
|
+
base_dir = File.dirname(entry_name)
|
|
2726
|
+
vml_path = resolve_relative_path(base_dir, vml_rel[:target])
|
|
2727
|
+
vml_xml = zip_reader.read_entry(vml_path)
|
|
2728
|
+
return [] if vml_xml.nil? || vml_xml.empty?
|
|
2729
|
+
|
|
2730
|
+
vml_rels_path = vml_path.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2731
|
+
vml_rels_xml = zip_reader.read_entry(vml_rels_path)
|
|
2732
|
+
vml_rels = {}
|
|
2733
|
+
if vml_rels_xml && !vml_rels_xml.empty?
|
|
2734
|
+
vr_parser = REXML::Parsers::SAX2Parser.new(vml_rels_xml)
|
|
2735
|
+
vr_listener = Ooxml::Reader::RelsListener.new
|
|
2736
|
+
vr_parser.listen(vr_listener)
|
|
2737
|
+
vr_parser.parse
|
|
2738
|
+
vr_listener.relationships.each do |r|
|
|
2739
|
+
vml_rels[r[:id]] = r[:target]
|
|
2740
|
+
end
|
|
2741
|
+
end
|
|
2742
|
+
|
|
2743
|
+
vml_parser = REXML::Parsers::SAX2Parser.new(vml_xml)
|
|
2744
|
+
hf_listener = Ooxml::Reader::VmlHeaderFooterListener.new
|
|
2745
|
+
vml_parser.listen(hf_listener)
|
|
2746
|
+
vml_parser.parse
|
|
2747
|
+
|
|
2748
|
+
vml_dir = File.dirname(vml_path)
|
|
2749
|
+
hf_listener.shapes.filter_map do |shape|
|
|
2750
|
+
rel_id = shape[:rel_id]
|
|
2751
|
+
media_target = vml_rels[rel_id]
|
|
2752
|
+
next unless media_target
|
|
2753
|
+
|
|
2754
|
+
media_path = resolve_relative_path(vml_dir, media_target)
|
|
2755
|
+
img_data = zip_reader.read_entry(media_path)
|
|
2756
|
+
next unless img_data
|
|
2757
|
+
|
|
2758
|
+
img_data = img_data.b
|
|
2759
|
+
filename = File.basename(media_path)
|
|
2760
|
+
ext = File.extname(media_path).delete_prefix(".").downcase
|
|
2761
|
+
content_type = case ext
|
|
2762
|
+
when "png" then "image/png"
|
|
2763
|
+
when "jpg", "jpeg" then "image/jpeg"
|
|
2764
|
+
when "gif" then "image/gif"
|
|
2765
|
+
when "emf" then "image/x-emf"
|
|
2766
|
+
when "wmf" then "image/x-wmf"
|
|
2767
|
+
else "application/octet-stream"
|
|
2768
|
+
end
|
|
2769
|
+
|
|
2770
|
+
Elements::HeaderFooterImage.new(
|
|
2771
|
+
position: shape[:id],
|
|
2772
|
+
r_id: rel_id,
|
|
2773
|
+
path: media_path,
|
|
2774
|
+
filename: filename,
|
|
2775
|
+
extension: ext,
|
|
2776
|
+
ext: ext,
|
|
2777
|
+
data: img_data,
|
|
2778
|
+
content_type: content_type
|
|
2779
|
+
)
|
|
2780
|
+
end
|
|
2781
|
+
end
|
|
2782
|
+
|
|
2783
|
+
#: (Array[untyped]? defined_names, String? sheet_name, Integer? sheet_index) -> String?
|
|
2784
|
+
def extract_sheet_print_area(defined_names, sheet_name, sheet_index)
|
|
2785
|
+
return nil unless defined_names && !defined_names.empty?
|
|
2786
|
+
|
|
2787
|
+
dn = defined_names.find do |d|
|
|
2788
|
+
d_name = d.respond_to?(:name) ? d.name : (d[:name] || d["name"])
|
|
2789
|
+
d_name == "_xlnm.Print_Area" && match_sheet_defined_name?(d, sheet_name, sheet_index)
|
|
2790
|
+
end
|
|
2791
|
+
return nil unless dn
|
|
2792
|
+
|
|
2793
|
+
val = dn.respond_to?(:value) ? dn.value : (dn[:value] || dn["value"])
|
|
2794
|
+
return nil unless val && !val.empty?
|
|
2795
|
+
|
|
2796
|
+
val.split(",").map { |part| part.sub(/\A(?:'(?:''|[^'])*'|[^!]+)!/, "").delete("$").strip }.join(",")
|
|
2797
|
+
end
|
|
2798
|
+
|
|
2799
|
+
#: (Array[untyped]? defined_names, String? sheet_name, Integer? sheet_index) -> Elements::PrintTitles?
|
|
2800
|
+
def extract_sheet_print_titles(defined_names, sheet_name, sheet_index)
|
|
2801
|
+
return nil unless defined_names && !defined_names.empty?
|
|
2802
|
+
|
|
2803
|
+
dn = defined_names.find do |d|
|
|
2804
|
+
d_name = d.respond_to?(:name) ? d.name : (d[:name] || d["name"])
|
|
2805
|
+
d_name == "_xlnm.Print_Titles" && match_sheet_defined_name?(d, sheet_name, sheet_index)
|
|
2806
|
+
end
|
|
2807
|
+
return nil unless dn
|
|
2808
|
+
|
|
2809
|
+
val = dn.respond_to?(:value) ? dn.value : (dn[:value] || dn["value"])
|
|
2810
|
+
return nil unless val && !val.empty?
|
|
2811
|
+
|
|
2812
|
+
rows = nil
|
|
2813
|
+
cols = nil
|
|
2814
|
+
val.split(",").each do |raw_part|
|
|
2815
|
+
part = raw_part.sub(/\A(?:'(?:''|[^'])*'|[^!]+)!/, "").delete("$").strip
|
|
2816
|
+
if part =~ /\A\d+(?::\d+)?\z/
|
|
2817
|
+
rows = part
|
|
2818
|
+
elsif part =~ /\A[A-Za-z]+(?::[A-Za-z]+)?\z/
|
|
2819
|
+
cols = part
|
|
2820
|
+
end
|
|
2821
|
+
end
|
|
2822
|
+
|
|
2823
|
+
return nil if rows.nil? && cols.nil?
|
|
2824
|
+
|
|
2825
|
+
Elements::PrintTitles.new(rows: rows, columns: cols, cols: cols)
|
|
2826
|
+
end
|
|
2827
|
+
|
|
2828
|
+
#: (untyped defined_name, String? sheet_name, Integer? sheet_index) -> bool
|
|
2829
|
+
def match_sheet_defined_name?(defined_name, sheet_name, sheet_index)
|
|
2830
|
+
d_id = if defined_name.respond_to?(:local_sheet_id)
|
|
2831
|
+
defined_name.local_sheet_id
|
|
2832
|
+
else
|
|
2833
|
+
defined_name[:local_sheet_id] || defined_name["local_sheet_id"]
|
|
2834
|
+
end
|
|
2835
|
+
d_name = if defined_name.respond_to?(:local_sheet_name)
|
|
2836
|
+
defined_name.local_sheet_name
|
|
2837
|
+
else
|
|
2838
|
+
defined_name[:local_sheet_name] || defined_name["local_sheet_name"] ||
|
|
2839
|
+
(defined_name.respond_to?(:sheet) ? defined_name.sheet : defined_name[:sheet])
|
|
2840
|
+
end
|
|
2841
|
+
d_val = defined_name.respond_to?(:value) ? defined_name.value : (defined_name[:value] || defined_name["value"])
|
|
2842
|
+
|
|
2843
|
+
return true if !d_id.nil? && !sheet_index.nil? && d_id.to_i == sheet_index
|
|
2844
|
+
return true if d_name && sheet_name && d_name.to_s == sheet_name.to_s
|
|
2845
|
+
|
|
2846
|
+
if d_id.nil? && sheet_name && d_val
|
|
2847
|
+
quoted = Regexp.escape(sheet_name)
|
|
2848
|
+
return true if d_val =~ /\A(?:'#{quoted}'|#{quoted})!/
|
|
2849
|
+
end
|
|
2850
|
+
false
|
|
2851
|
+
end
|
|
2852
|
+
|
|
2853
|
+
#: (untyped raw_row, ?Hash[String, untyped] hyperlinks, ?(Array[untyped] | Hash[String, untyped]) comments, ?Hash[untyped, untyped]? styles, ?date1904: bool, ?threaded_comments: (Array[untyped] | Hash[String, untyped])) -> (Elements::Row | untyped)
|
|
1768
2854
|
def build_row_from_raw(raw_row, hyperlinks = {}, comments = [], styles = nil, date1904: false, threaded_comments: [])
|
|
1769
2855
|
return raw_row if raw_row.is_a?(Elements::Row)
|
|
1770
2856
|
|
|
1771
2857
|
comments_map = if comments.is_a?(Hash)
|
|
1772
|
-
comments
|
|
2858
|
+
comments.each_with_object({}) do |(k, c), acc|
|
|
2859
|
+
acc[k.to_s.upcase] = c.is_a?(Elements::Comment) ? c : Elements::Comment.new(**c)
|
|
2860
|
+
end
|
|
1773
2861
|
else
|
|
1774
|
-
comments.each_with_object({})
|
|
2862
|
+
comments.each_with_object({}) do |c, acc|
|
|
2863
|
+
obj = c.is_a?(Elements::Comment) ? c : Elements::Comment.new(**c)
|
|
2864
|
+
r = obj.ref
|
|
2865
|
+
acc[r.to_s.upcase] = obj if r
|
|
2866
|
+
end
|
|
1775
2867
|
end
|
|
1776
2868
|
|
|
1777
2869
|
threaded_comments_map = if threaded_comments.is_a?(Hash)
|
|
1778
|
-
threaded_comments
|
|
2870
|
+
threaded_comments.each_with_object({}) do |(k, tcs), acc|
|
|
2871
|
+
arr = Array(tcs).map do |tc|
|
|
2872
|
+
tc.is_a?(Elements::ThreadedComment) ? tc : Elements::ThreadedComment.new(**tc)
|
|
2873
|
+
end
|
|
2874
|
+
acc[k.to_s.upcase] = arr
|
|
2875
|
+
end
|
|
1779
2876
|
else
|
|
1780
2877
|
threaded_comments.each_with_object({}) do |c, acc|
|
|
1781
|
-
|
|
1782
|
-
|
|
2878
|
+
obj = c.is_a?(Elements::ThreadedComment) ? c : Elements::ThreadedComment.new(**c)
|
|
2879
|
+
r = obj.ref
|
|
2880
|
+
(acc[r.to_s.upcase] ||= []) << obj if r
|
|
1783
2881
|
end
|
|
1784
2882
|
end
|
|
1785
2883
|
|
|
2884
|
+
hyperlinks_map = if hyperlinks && !hyperlinks.empty?
|
|
2885
|
+
hyperlinks.each_with_object({}) do |(k, hl), acc|
|
|
2886
|
+
acc[k.to_s.upcase] = hl.is_a?(Elements::Hyperlink) ? hl : Elements::Hyperlink.new(**hl)
|
|
2887
|
+
end
|
|
2888
|
+
else
|
|
2889
|
+
{}
|
|
2890
|
+
end
|
|
2891
|
+
|
|
1786
2892
|
raw_cells = raw_row[:cells]
|
|
1787
2893
|
cells = raw_cells.map do |rc|
|
|
1788
2894
|
if rc.is_a?(Elements::Cell)
|
|
1789
|
-
hl =
|
|
1790
|
-
cm = comments_map
|
|
1791
|
-
tcs = threaded_comments_map
|
|
2895
|
+
hl = hyperlinks_map[rc.ref]
|
|
2896
|
+
cm = comments_map[rc.ref]
|
|
2897
|
+
tcs = threaded_comments_map[rc.ref]
|
|
1792
2898
|
fmt = rc.format_code || (NumberFormatter.format_code_for(rc.style_index, styles) if styles && rc.style_index)
|
|
1793
2899
|
if hl || cm || tcs || (fmt && rc.format_code != fmt) || (date1904 && !rc.date1904?)
|
|
1794
2900
|
rc.with(
|
|
@@ -1802,13 +2908,14 @@ module Xlsxrb
|
|
|
1802
2908
|
rc
|
|
1803
2909
|
end
|
|
1804
2910
|
else
|
|
1805
|
-
parsed = Elements::Cell.
|
|
2911
|
+
parsed = Elements::Cell._parse_ref(rc[:ref]) if rc[:ref]
|
|
1806
2912
|
row_idx = parsed ? parsed[0] : raw_row[:index]
|
|
1807
2913
|
col_idx = parsed ? parsed[1] : 0
|
|
1808
|
-
ref = rc[:ref] || "#{Elements::Cell.
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2914
|
+
ref = rc[:ref] || "#{Elements::Cell._column_letter(col_idx)}#{row_idx + 1}"
|
|
2915
|
+
ref_key = ref.upcase
|
|
2916
|
+
hl = hyperlinks_map[ref_key]
|
|
2917
|
+
cm = comments_map[ref_key]
|
|
2918
|
+
tcs = threaded_comments_map[ref_key]
|
|
1812
2919
|
fmt = rc[:format_code] || (NumberFormatter.format_code_for(rc[:style_index], styles) if styles && rc[:style_index])
|
|
1813
2920
|
|
|
1814
2921
|
val = rc[:value]
|
|
@@ -1856,95 +2963,12 @@ module Xlsxrb
|
|
|
1856
2963
|
custom_height: attrs[:custom_height] || false,
|
|
1857
2964
|
collapsed: attrs[:collapsed] || false,
|
|
1858
2965
|
outline_level: attrs[:outline_level],
|
|
2966
|
+
style_index: attrs[:style_index],
|
|
1859
2967
|
unmapped_data: row_unmapped,
|
|
1860
2968
|
errors: row_errors
|
|
1861
2969
|
)
|
|
1862
2970
|
end
|
|
1863
2971
|
|
|
1864
|
-
#: (Elements::Cell cell, Array[String] sst, Hash[String, Integer] sst_index, ?date1904: bool) -> Hash[Symbol, untyped]
|
|
1865
|
-
def build_raw_cell(cell, sst, sst_index, date1904: false)
|
|
1866
|
-
# simplecov:disable
|
|
1867
|
-
# Edge case / untested delegation block
|
|
1868
|
-
ref = cell.ref
|
|
1869
|
-
value = cell.value
|
|
1870
|
-
result = { ref: ref, style_index: cell.style_index }
|
|
1871
|
-
|
|
1872
|
-
case value
|
|
1873
|
-
when String, Xlsxrb::Elements::RichText
|
|
1874
|
-
idx = sst_index[value] ||= begin
|
|
1875
|
-
sst << value
|
|
1876
|
-
sst.size - 1
|
|
1877
|
-
end
|
|
1878
|
-
result[:value] = idx
|
|
1879
|
-
result[:type] = "s"
|
|
1880
|
-
when true, false
|
|
1881
|
-
result[:value] = value
|
|
1882
|
-
result[:type] = "b"
|
|
1883
|
-
when Integer, Float
|
|
1884
|
-
result[:value] = value
|
|
1885
|
-
when Xlsxrb::Elements::CellError
|
|
1886
|
-
result[:value] = value.code
|
|
1887
|
-
result[:type] = "e"
|
|
1888
|
-
when Date
|
|
1889
|
-
result[:value] = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
|
|
1890
|
-
when Time
|
|
1891
|
-
result[:value] = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
|
|
1892
|
-
# simplecov:enable
|
|
1893
|
-
when NilClass
|
|
1894
|
-
# empty cell
|
|
1895
|
-
end
|
|
1896
|
-
|
|
1897
|
-
# simplecov:disable
|
|
1898
|
-
# Edge case / untested delegation block
|
|
1899
|
-
if cell.formula
|
|
1900
|
-
f = cell.formula
|
|
1901
|
-
if f.is_a?(Elements::Formula)
|
|
1902
|
-
result[:formula] = f.expression
|
|
1903
|
-
result[:formula_type] = f.type if f.type
|
|
1904
|
-
result[:formula_ref] = f.ref if f.ref
|
|
1905
|
-
result[:formula_si] = f.shared_index if f.shared_index
|
|
1906
|
-
result[:formula_ca] = true if f.calculate_always
|
|
1907
|
-
unless f.cached_value.nil?
|
|
1908
|
-
# Cached value is written as-is (not through SST)
|
|
1909
|
-
result[:value] = f.cached_value
|
|
1910
|
-
case f.cached_value
|
|
1911
|
-
when true, false
|
|
1912
|
-
result[:type] = "b"
|
|
1913
|
-
when Elements::CellError
|
|
1914
|
-
result[:type] = "e"
|
|
1915
|
-
result[:value] = f.cached_value.code
|
|
1916
|
-
else
|
|
1917
|
-
result.delete(:type) # Ensure no type is set; cached values are plain text in <v>
|
|
1918
|
-
end
|
|
1919
|
-
# simplecov:enable
|
|
1920
|
-
end
|
|
1921
|
-
else
|
|
1922
|
-
# simplecov:disable
|
|
1923
|
-
# Edge case / untested delegation block
|
|
1924
|
-
result[:formula] = f
|
|
1925
|
-
# simplecov:enable
|
|
1926
|
-
end
|
|
1927
|
-
end
|
|
1928
|
-
# simplecov:disable
|
|
1929
|
-
# Edge case / untested delegation block
|
|
1930
|
-
result
|
|
1931
|
-
# simplecov:enable
|
|
1932
|
-
end
|
|
1933
|
-
|
|
1934
|
-
#: (Elements::Row row) -> Hash[Symbol, untyped]
|
|
1935
|
-
def build_row_attrs(row)
|
|
1936
|
-
# simplecov:disable
|
|
1937
|
-
# Edge case / untested delegation block
|
|
1938
|
-
attrs = {}
|
|
1939
|
-
attrs[:height] = row.height if row.height
|
|
1940
|
-
attrs[:hidden] = true if row.hidden
|
|
1941
|
-
attrs[:custom_height] = true if row.custom_height
|
|
1942
|
-
attrs[:collapsed] = true if row.collapsed
|
|
1943
|
-
attrs[:outline_level] = row.outline_level if row.outline_level
|
|
1944
|
-
attrs
|
|
1945
|
-
# simplecov:enable
|
|
1946
|
-
end
|
|
1947
|
-
|
|
1948
2972
|
#: (untyped source, String? password) -> untyped
|
|
1949
2973
|
def prepare_source_io(source, password)
|
|
1950
2974
|
if source.is_a?(String)
|
|
@@ -2136,8 +3160,8 @@ module Xlsxrb
|
|
|
2136
3160
|
max_row = rows.last.index
|
|
2137
3161
|
min_col = rows.map { |r| r.cells.first&.column_index }.compact.min || 0
|
|
2138
3162
|
max_col = rows.map { |r| r.cells.last&.column_index }.compact.max || 0
|
|
2139
|
-
min_col_letter = Elements::Cell.
|
|
2140
|
-
max_col_letter = Elements::Cell.
|
|
3163
|
+
min_col_letter = Elements::Cell._column_letter(min_col)
|
|
3164
|
+
max_col_letter = Elements::Cell._column_letter(max_col)
|
|
2141
3165
|
dim_ref = if min_row == max_row && min_col == max_col
|
|
2142
3166
|
"#{min_col_letter}#{min_row + 1}"
|
|
2143
3167
|
else
|
|
@@ -2425,88 +3449,4 @@ module Xlsxrb
|
|
|
2425
3449
|
io.string
|
|
2426
3450
|
end
|
|
2427
3451
|
end
|
|
2428
|
-
|
|
2429
|
-
# Builds a raw cell hash from a value for streaming writes.
|
|
2430
|
-
#
|
|
2431
|
-
# @param row_index [Integer] 0-based row index.
|
|
2432
|
-
# @param col_index [Integer] 0-based column index.
|
|
2433
|
-
# @param value [Object] Cell value.
|
|
2434
|
-
# @param sst [Array<String>] Shared strings array.
|
|
2435
|
-
# @param sst_index [Hash{String => Integer}] Shared strings index mapping.
|
|
2436
|
-
# @return [Hash{Symbol => Object}] Raw cell hash.
|
|
2437
|
-
# @api public
|
|
2438
|
-
#: (Integer row_index, Integer col_index, untyped value, Array[String] sst, Hash[String, Integer] sst_index, ?date1904: bool) -> Hash[Symbol, untyped]
|
|
2439
|
-
def self.build_raw_cell_from_value(row_index, col_index, value, sst, sst_index, date1904: false)
|
|
2440
|
-
# simplecov:disable
|
|
2441
|
-
# Edge case / untested delegation block
|
|
2442
|
-
ref = "#{Elements::Cell.column_letter(col_index)}#{row_index + 1}"
|
|
2443
|
-
result = { ref: ref }
|
|
2444
|
-
|
|
2445
|
-
case value
|
|
2446
|
-
when Elements::Formula
|
|
2447
|
-
result[:formula] = value.expression
|
|
2448
|
-
result[:formula_type] = value.type if value.type
|
|
2449
|
-
result[:formula_ref] = value.ref if value.ref
|
|
2450
|
-
result[:formula_si] = value.shared_index if value.shared_index
|
|
2451
|
-
result[:formula_ca] = true if value.calculate_always
|
|
2452
|
-
unless value.cached_value.nil?
|
|
2453
|
-
cv = value.cached_value
|
|
2454
|
-
result[:value] = cv
|
|
2455
|
-
case cv
|
|
2456
|
-
when true, false
|
|
2457
|
-
result[:type] = "b"
|
|
2458
|
-
when Xlsxrb::Elements::CellError
|
|
2459
|
-
result[:type] = "e"
|
|
2460
|
-
result[:value] = cv.code
|
|
2461
|
-
end
|
|
2462
|
-
end
|
|
2463
|
-
when Hash
|
|
2464
|
-
if value.key?(:formula) || value.key?(:expression) || (value[:type]&.to_sym == :shared && (value.key?(:shared_index) || value.key?(:si)))
|
|
2465
|
-
result[:formula] = value[:formula] || value[:expression]
|
|
2466
|
-
result[:formula_type] = value[:type] || (:shared if value.key?(:shared_index) || value.key?(:si))
|
|
2467
|
-
result[:formula_ref] = value[:ref] if value[:ref]
|
|
2468
|
-
si = value[:shared_index] || value[:si]
|
|
2469
|
-
result[:formula_si] = si if si
|
|
2470
|
-
result[:formula_ca] = true if value[:calculate_always]
|
|
2471
|
-
cv = value.key?(:value) ? value[:value] : value[:cached_value]
|
|
2472
|
-
unless cv.nil?
|
|
2473
|
-
result[:value] = cv
|
|
2474
|
-
case cv
|
|
2475
|
-
when true, false
|
|
2476
|
-
result[:type] = "b"
|
|
2477
|
-
when Xlsxrb::Elements::CellError
|
|
2478
|
-
result[:type] = "e"
|
|
2479
|
-
result[:value] = cv.code
|
|
2480
|
-
end
|
|
2481
|
-
end
|
|
2482
|
-
end
|
|
2483
|
-
when String, Xlsxrb::Elements::RichText
|
|
2484
|
-
idx = sst_index[value] ||= begin
|
|
2485
|
-
sst << value
|
|
2486
|
-
sst.size - 1
|
|
2487
|
-
end
|
|
2488
|
-
result[:value] = idx
|
|
2489
|
-
result[:type] = "s"
|
|
2490
|
-
when true, false
|
|
2491
|
-
result[:value] = value
|
|
2492
|
-
result[:type] = "b"
|
|
2493
|
-
when Integer, Float
|
|
2494
|
-
result[:value] = value
|
|
2495
|
-
when Xlsxrb::Elements::CellError
|
|
2496
|
-
result[:value] = value.code
|
|
2497
|
-
result[:type] = "e"
|
|
2498
|
-
when Date
|
|
2499
|
-
result[:value] = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
|
|
2500
|
-
when Time
|
|
2501
|
-
result[:value] = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
|
|
2502
|
-
# simplecov:enable
|
|
2503
|
-
when NilClass
|
|
2504
|
-
# empty cell
|
|
2505
|
-
end
|
|
2506
|
-
|
|
2507
|
-
# simplecov:disable
|
|
2508
|
-
# Edge case / untested delegation block
|
|
2509
|
-
result
|
|
2510
|
-
# simplecov:enable
|
|
2511
|
-
end
|
|
2512
3452
|
end
|