xlsxrb 0.1.16 → 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 +157 -1
- data/README.md +81 -21
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +31 -61
- data/lib/xlsxrb/chart_builder.rb +619 -98
- data/lib/xlsxrb/dsl_helpers.rb +583 -15
- data/lib/xlsxrb/elements/cell.rb +265 -53
- data/lib/xlsxrb/elements/column.rb +95 -6
- 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 +243 -7
- data/lib/xlsxrb/elements/workbook.rb +202 -65
- data/lib/xlsxrb/elements/worksheet.rb +728 -85
- data/lib/xlsxrb/elements.rb +1 -0
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +32 -3
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +194 -26
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +372 -18
- data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +33 -14
- data/lib/xlsxrb/ooxml/reader.rb +92 -0
- data/lib/xlsxrb/ooxml/styles_parser.rb +29 -14
- data/lib/xlsxrb/ooxml/workbook_parser.rb +18 -3
- data/lib/xlsxrb/ooxml/workbook_writer.rb +351 -77
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +18 -4
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +349 -103
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +584 -443
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +52 -14
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +42 -18
- data/lib/xlsxrb/ooxml/writer.rb +288 -35
- data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
- data/lib/xlsxrb/ooxml/zip_reader.rb +47 -10
- data/lib/xlsxrb/ooxml/zip_writer.rb +15 -5
- data/lib/xlsxrb/stream_row.rb +97 -18
- data/lib/xlsxrb/stream_sheet.rb +827 -157
- data/lib/xlsxrb/stream_writer.rb +2766 -375
- data/lib/xlsxrb/style_builder.rb +821 -105
- data/lib/xlsxrb/utils.rb +82 -57
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +542 -62
- data/lib/xlsxrb/worksheet_builder.rb +2279 -209
- data/lib/xlsxrb.rb +1539 -344
- data/sig/generated/xlsxrb/chart_builder.rbs +336 -79
- data/sig/generated/xlsxrb/dsl_helpers.rbs +137 -4
- data/sig/generated/xlsxrb/elements/cell.rbs +104 -15
- data/sig/generated/xlsxrb/elements/column.rbs +62 -3
- 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 +29 -0
- data/sig/generated/xlsxrb/elements/workbook.rbs +92 -44
- data/sig/generated/xlsxrb/elements/worksheet.rbs +259 -38
- data/sig/generated/xlsxrb/ooxml/reader/listeners/core_listeners.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/reader/listeners/drawing_listeners.rbs +74 -2
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +97 -0
- data/sig/generated/xlsxrb/ooxml/reader/listeners/style_listeners.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/reader.rbs +20 -0
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/workbook_parser.rbs +8 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -4
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/writer/drawing_xml.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +45 -8
- data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +2 -2
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -2
- data/sig/generated/xlsxrb/stream_row.rbs +48 -14
- data/sig/generated/xlsxrb/stream_sheet.rbs +398 -89
- data/sig/generated/xlsxrb/stream_writer.rbs +1601 -219
- data/sig/generated/xlsxrb/style_builder.rbs +403 -75
- data/sig/generated/xlsxrb/utils.rbs +49 -13
- data/sig/generated/xlsxrb/workbook_builder.rbs +308 -26
- data/sig/generated/xlsxrb/worksheet_builder.rbs +1142 -96
- data/sig/generated/xlsxrb.rbs +321 -52
- metadata +3 -1
data/lib/xlsxrb.rb
CHANGED
|
@@ -39,27 +39,51 @@ 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
|
|
46
46
|
module Xlsxrb
|
|
47
|
+
# Maximum number of rows supported by Excel / OpenXML.
|
|
48
|
+
MAX_ROW = 1_048_576
|
|
49
|
+
|
|
50
|
+
# Maximum number of columns supported by Excel / OpenXML.
|
|
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
|
|
56
|
+
|
|
57
|
+
# Maximum number of individual cell/range references allowed in a single data validation element by Excel.
|
|
58
|
+
MAX_REFS_PER_VALIDATION = 32_767
|
|
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
|
+
|
|
47
67
|
# Base class for all exceptions raised by Xlsxrb.
|
|
48
68
|
# Predefined cell error constants are exposed on this class for convenience.
|
|
49
69
|
class Error < StandardError
|
|
50
|
-
DIV0
|
|
51
|
-
NA
|
|
52
|
-
NAME
|
|
53
|
-
NULL
|
|
54
|
-
NUM
|
|
55
|
-
REF
|
|
56
|
-
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
|
|
57
80
|
end
|
|
58
81
|
|
|
59
82
|
# Raised when parsing malformed or invalid XML structures.
|
|
60
83
|
class ParseError < Error; end
|
|
61
84
|
|
|
62
85
|
OMITTED_TARGET = Object.new.freeze
|
|
86
|
+
VALID_SHEET_STATES = %i[visible hidden very_hidden].freeze
|
|
63
87
|
OFFICE_DOC_REL_TYPES = [
|
|
64
88
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
|
65
89
|
"http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument"
|
|
@@ -79,7 +103,7 @@ module Xlsxrb
|
|
|
79
103
|
xl/dialogsheets/
|
|
80
104
|
xl/macrosheets/
|
|
81
105
|
].freeze
|
|
82
|
-
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
|
|
83
107
|
|
|
84
108
|
# Raised when cell, row, or workbook parameters fail specification validation.
|
|
85
109
|
class ValidationError < Error; end
|
|
@@ -115,15 +139,26 @@ module Xlsxrb
|
|
|
115
139
|
#
|
|
116
140
|
# @param runs [Array<Hash, String, Array, Elements::RichTextRun, Object>] Optional array of rich text run items.
|
|
117
141
|
# @param text [String, nil] Plain text string (convenience parameter).
|
|
142
|
+
# @param base_font [Hash, Object, nil] Base font properties to merge into all runs.
|
|
118
143
|
# @param font_props [Hash] Inline font styling options (e.g. bold: true, color: "FF0000").
|
|
119
144
|
# @return [Elements::RichText] The compiled rich text element.
|
|
120
145
|
# @api public
|
|
121
|
-
#: (*(Hash[Symbol, untyped] | Elements::RichTextRun | String | Array[untyped] | untyped) runs, ?text: String?, **untyped font_props) -> Elements::RichText
|
|
122
|
-
def self.rich_text(*runs, text: nil, **font_props)
|
|
146
|
+
#: (*(Hash[Symbol, untyped] | Elements::RichTextRun | String | Array[untyped] | untyped) runs, ?text: String?, ?base_font: untyped, **untyped font_props) -> Elements::RichText
|
|
147
|
+
def self.rich_text(*runs, text: nil, base_font: nil, **font_props)
|
|
148
|
+
base = if base_font.respond_to?(:to_font_hash)
|
|
149
|
+
base_font.to_font_hash
|
|
150
|
+
elsif base_font.respond_to?(:to_h)
|
|
151
|
+
base_font.to_h
|
|
152
|
+
else
|
|
153
|
+
base_font
|
|
154
|
+
end
|
|
123
155
|
if text
|
|
124
|
-
|
|
156
|
+
f = font_props.empty? ? nil : font_props
|
|
157
|
+
f = base.merge(f || {}) if base.is_a?(Hash)
|
|
158
|
+
runs = [Elements::RichTextRun.new(text: text, font: f)]
|
|
125
159
|
elsif runs.size == 1 && runs.first.is_a?(String) && !font_props.empty?
|
|
126
|
-
|
|
160
|
+
f = base.is_a?(Hash) ? base.merge(font_props) : font_props
|
|
161
|
+
runs = [Elements::RichTextRun.new(text: runs.first, font: f)]
|
|
127
162
|
else
|
|
128
163
|
normalized_runs = []
|
|
129
164
|
pending_font = nil
|
|
@@ -155,6 +190,12 @@ module Xlsxrb
|
|
|
155
190
|
normalized_runs << Elements::RichTextRun.new(text: item.to_s)
|
|
156
191
|
end
|
|
157
192
|
end
|
|
193
|
+
if base.is_a?(Hash) && !base.empty?
|
|
194
|
+
normalized_runs = normalized_runs.map do |run|
|
|
195
|
+
merged_font = run.font ? base.merge(run.font) : base.dup
|
|
196
|
+
Elements::RichTextRun.new(text: run.text, font: merged_font)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
158
199
|
runs = normalized_runs
|
|
159
200
|
end
|
|
160
201
|
Elements::RichText.new(runs: runs)
|
|
@@ -172,40 +213,82 @@ module Xlsxrb
|
|
|
172
213
|
# @param cached_value [Object, nil] Optional precomputed value for readers that do not evaluate formulas.
|
|
173
214
|
# @param type [Symbol, String, nil] Formula type (:shared, :array, etc.).
|
|
174
215
|
# @param ref [String, nil] Target cell or range reference (e.g. "A1:C1").
|
|
216
|
+
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
217
|
+
# @param si [Integer, nil] Alias for shared_index.
|
|
175
218
|
# @return [Elements::Formula]
|
|
176
219
|
# @api public
|
|
177
|
-
|
|
178
|
-
|
|
220
|
+
# rubocop:disable Naming/MethodParameterName
|
|
221
|
+
#: (String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?, ?si: Integer?) -> Elements::Formula
|
|
222
|
+
def self.formula(expression, cached_value: nil, type: nil, ref: nil, shared_index: nil, si: nil)
|
|
223
|
+
s_idx = shared_index || si
|
|
179
224
|
Elements::Formula.new(
|
|
180
225
|
expression: expression,
|
|
181
226
|
cached_value: cached_value,
|
|
182
|
-
type: type,
|
|
227
|
+
type: type || (s_idx ? :shared : nil),
|
|
228
|
+
ref: ref,
|
|
229
|
+
shared_index: s_idx,
|
|
230
|
+
calculate_always: cached_value.nil? || nil
|
|
231
|
+
)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Creates a shared formula {Elements::Formula} object for master or follower cell.
|
|
235
|
+
#
|
|
236
|
+
# @example Create a shared formula master
|
|
237
|
+
# formula = Xlsxrb.shared_formula("A2*2", shared_index: 0, ref: "B2:B10")
|
|
238
|
+
#
|
|
239
|
+
# @example Create a shared formula follower
|
|
240
|
+
# formula = Xlsxrb.shared_formula(shared_index: 0)
|
|
241
|
+
#
|
|
242
|
+
# @param expression [String, nil] The formula expression (nil for follower cells).
|
|
243
|
+
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
244
|
+
# @param si [Integer, nil] Legacy alias for shared_index.
|
|
245
|
+
# @param ref [String, nil] Target range for master cell (e.g. "B2:B10").
|
|
246
|
+
# @param cached_value [Object, nil] Optional precomputed value.
|
|
247
|
+
# @return [Elements::Formula]
|
|
248
|
+
# @api public
|
|
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
|
+
|
|
255
|
+
Elements::Formula.new(
|
|
256
|
+
expression: expression || "",
|
|
257
|
+
cached_value: cached_value,
|
|
258
|
+
type: :shared,
|
|
183
259
|
ref: ref,
|
|
260
|
+
shared_index: s_idx,
|
|
184
261
|
calculate_always: cached_value.nil? || nil
|
|
185
262
|
)
|
|
186
263
|
end
|
|
264
|
+
# rubocop:enable Naming/MethodParameterName
|
|
187
265
|
|
|
188
266
|
# Creates an array or dynamic array {Elements::Formula} object.
|
|
189
267
|
#
|
|
190
268
|
# @example Create a multi-cell array formula
|
|
191
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)
|
|
192
271
|
#
|
|
193
272
|
# @example Create a dynamic array formula
|
|
194
273
|
# formula = Xlsxrb.array_formula("SORT(UNIQUE(A1:A10))", "D1")
|
|
195
274
|
#
|
|
196
275
|
# @param expression [String] The formula expression without leading '='.
|
|
197
|
-
# @param
|
|
276
|
+
# @param pos_ref [String, nil] Target cell or range reference (positional).
|
|
277
|
+
# @param ref [String, nil] Target cell or range reference (keyword).
|
|
198
278
|
# @param cached_value [Object, nil] Optional precomputed value.
|
|
199
279
|
# @return [Elements::Formula]
|
|
200
280
|
# @api public
|
|
201
|
-
#: (String expression, ?String?
|
|
202
|
-
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
|
|
203
285
|
Elements::Formula.new(
|
|
204
286
|
expression: expression,
|
|
205
287
|
cached_value: cached_value,
|
|
206
288
|
type: :array,
|
|
207
|
-
ref:
|
|
208
|
-
calculate_always: cached_value.nil? || nil
|
|
289
|
+
ref: target_ref,
|
|
290
|
+
calculate_always: cached_value.nil? || nil,
|
|
291
|
+
**attrs
|
|
209
292
|
)
|
|
210
293
|
end
|
|
211
294
|
|
|
@@ -228,6 +311,50 @@ module Xlsxrb
|
|
|
228
311
|
Elements::Hyperlink.new(url: url, display: display, tooltip: tooltip, location: location)
|
|
229
312
|
end
|
|
230
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
|
+
|
|
231
358
|
# Formats a cell value according to an Excel format code string.
|
|
232
359
|
#
|
|
233
360
|
# @param value [Object, nil] Value to format.
|
|
@@ -236,10 +363,17 @@ module Xlsxrb
|
|
|
236
363
|
# @return [String] Formatted string representation.
|
|
237
364
|
# @api public
|
|
238
365
|
#: (untyped value, ?String? format_code, ?date1904: bool) -> String
|
|
239
|
-
def self.
|
|
366
|
+
def self.format_value(value, format_code = nil, date1904: false)
|
|
240
367
|
NumberFormatter.format(value, format_code, date1904: date1904)
|
|
241
368
|
end
|
|
242
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
|
+
|
|
243
377
|
# Converts a Date or Time to an Excel numeric serial number (1900 or 1904 system).
|
|
244
378
|
#
|
|
245
379
|
# @example
|
|
@@ -251,7 +385,11 @@ module Xlsxrb
|
|
|
251
385
|
# @api public
|
|
252
386
|
#: (Date | Time date_or_time, ?date1904: bool) -> (Integer | Float)
|
|
253
387
|
def self.date_to_serial(date_or_time, date1904: false)
|
|
254
|
-
|
|
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
|
|
255
393
|
end
|
|
256
394
|
|
|
257
395
|
# Checks whether a serial number is a valid Excel date within the supported range.
|
|
@@ -266,7 +404,7 @@ module Xlsxrb
|
|
|
266
404
|
# @api public
|
|
267
405
|
#: (untyped serial, ?date1904: bool) -> bool
|
|
268
406
|
def self.valid_excel_date?(serial, date1904: false)
|
|
269
|
-
Utils.valid_excel_date?(serial, date1904: date1904)
|
|
407
|
+
Ooxml::Utils.valid_excel_date?(serial, date1904: date1904)
|
|
270
408
|
end
|
|
271
409
|
|
|
272
410
|
# Converts an Excel serial number to a Date.
|
|
@@ -280,21 +418,32 @@ module Xlsxrb
|
|
|
280
418
|
# @api public
|
|
281
419
|
#: (Numeric serial, ?date1904: bool) -> Date
|
|
282
420
|
def self.serial_to_date(serial, date1904: false)
|
|
283
|
-
Utils.serial_to_date(serial, date1904: date1904)
|
|
421
|
+
Ooxml::Utils.serial_to_date(serial, date1904: date1904)
|
|
284
422
|
end
|
|
285
423
|
|
|
286
424
|
# Converts a Time or Date to a fractional Excel serial number.
|
|
287
425
|
#
|
|
288
426
|
# @example
|
|
289
|
-
# Xlsxrb.
|
|
427
|
+
# Xlsxrb.time_to_serial(Time.utc(2026, 1, 1, 12, 0, 0)) #=> 46023.5
|
|
290
428
|
#
|
|
291
429
|
# @param time [Time, Date] Time or Date instance.
|
|
292
430
|
# @param date1904 [Boolean] Whether to use the 1904 date system.
|
|
293
431
|
# @return [Float] Fractional serial number.
|
|
294
432
|
# @api public
|
|
295
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
|
|
296
444
|
def self.datetime_to_serial(time, date1904: false)
|
|
297
|
-
|
|
445
|
+
warn "[DEPRECATION] Xlsxrb.datetime_to_serial is deprecated; use Xlsxrb.time_to_serial instead.", uplevel: 1
|
|
446
|
+
time_to_serial(time, date1904: date1904)
|
|
298
447
|
end
|
|
299
448
|
|
|
300
449
|
# Converts an Excel serial number to a Time (UTC).
|
|
@@ -308,11 +457,71 @@ module Xlsxrb
|
|
|
308
457
|
# @api public
|
|
309
458
|
#: (Numeric serial, ?date1904: bool) -> Time
|
|
310
459
|
def self.serial_to_time(serial, date1904: false)
|
|
311
|
-
Utils.
|
|
460
|
+
Ooxml::Utils.serial_to_datetime(serial, date1904: date1904)
|
|
312
461
|
end
|
|
313
462
|
|
|
314
|
-
|
|
315
|
-
|
|
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)
|
|
316
525
|
end
|
|
317
526
|
|
|
318
527
|
# Converts a cell range reference string (e.g. "A1:C10", "A1") to 0-based [[top_row, left_col], [bottom_row, right_col]].
|
|
@@ -325,23 +534,150 @@ module Xlsxrb
|
|
|
325
534
|
# @api public
|
|
326
535
|
#: (String? range_ref) -> [[Integer, Integer], [Integer, Integer]]?
|
|
327
536
|
def self.parse_range(range_ref)
|
|
328
|
-
|
|
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]]
|
|
329
551
|
end
|
|
330
552
|
|
|
331
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.
|
|
332
556
|
#
|
|
333
|
-
# @example
|
|
557
|
+
# @example 4 flat integers
|
|
334
558
|
# Xlsxrb.range_to_ref(0, 0, 9, 2) #=> "A1:C10"
|
|
335
559
|
#
|
|
336
|
-
# @
|
|
337
|
-
#
|
|
338
|
-
#
|
|
339
|
-
# @param
|
|
340
|
-
# @
|
|
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.
|
|
341
598
|
# @api public
|
|
342
|
-
#: (
|
|
343
|
-
def self.
|
|
344
|
-
|
|
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
|
|
345
681
|
end
|
|
346
682
|
|
|
347
683
|
# Resolves an ECMA-376 indexed color palette index (0..63) to an RRGGBB hex string (or AARRGGBB if alpha: true).
|
|
@@ -376,8 +712,35 @@ module Xlsxrb
|
|
|
376
712
|
Colors.to_hex(color, alpha: alpha)
|
|
377
713
|
end
|
|
378
714
|
|
|
379
|
-
|
|
380
|
-
|
|
715
|
+
# @deprecated Use {.to_hex_color} instead. Will be removed in 0.2.0.
|
|
716
|
+
#: (Integer | String | Symbol | nil color, ?alpha: bool) -> String?
|
|
717
|
+
def self.color_to_hex(color, alpha: false)
|
|
718
|
+
warn "[DEPRECATION] Xlsxrb.color_to_hex is deprecated; use Xlsxrb.to_hex_color instead.", uplevel: 1
|
|
719
|
+
to_hex_color(color, alpha: alpha)
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
# Sanitizes a worksheet name to ensure it conforms to Excel constraints within 31 characters.
|
|
723
|
+
# Replaces forbidden characters (\ / ? * [ ] : and control characters),
|
|
724
|
+
# replaces leading/trailing apostrophes, and replaces reserved 'History' with 'History_'.
|
|
725
|
+
#
|
|
726
|
+
# @example
|
|
727
|
+
# Xlsxrb.sanitize_sheet_name("Sales: 2026") #=> "Sales_ 2026"
|
|
728
|
+
# Xlsxrb.sanitize_sheet_name("'Report'") #=> "_Report_"
|
|
729
|
+
#
|
|
730
|
+
# @param name [Object] Desired worksheet name.
|
|
731
|
+
# @param replace_char [String] Replacement character for invalid characters.
|
|
732
|
+
# @return [String] Excel-compliant worksheet name.
|
|
733
|
+
# @api public
|
|
734
|
+
#: (untyped name, ?replace_char: String) -> String
|
|
735
|
+
def self.sanitize_sheet_name(name, replace_char: "_")
|
|
736
|
+
DslHelpers.sanitize_sheet_name(name, replace_char: replace_char)
|
|
737
|
+
end
|
|
738
|
+
|
|
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)
|
|
381
744
|
end
|
|
382
745
|
|
|
383
746
|
# Reads an XLSX file (streaming and lazy-loaded by default) from a file path, IO stream, or binary String.
|
|
@@ -420,6 +783,11 @@ module Xlsxrb
|
|
|
420
783
|
# doc_sheet = sheet.load # explicitly load into memory
|
|
421
784
|
# puts doc_sheet["A1"].value # coordinate random access
|
|
422
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.
|
|
423
791
|
# @param max_uncompressed_size [Integer, nil] Maximum allowed uncompressed size in bytes per entry (ZIP bomb defense).
|
|
424
792
|
# @raise [ZipBombError] If an uncompressed entry exceeds max_uncompressed_size.
|
|
425
793
|
# @raise [EncryptedFileError] If the file is encrypted and no password was supplied.
|
|
@@ -429,10 +797,57 @@ module Xlsxrb
|
|
|
429
797
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) { (StreamSheet) -> void } -> void
|
|
430
798
|
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Elements::Workbook
|
|
431
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)
|
|
432
846
|
prepared_source = prepare_source_io(source, password)
|
|
433
847
|
zip_options = {}
|
|
434
848
|
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
435
849
|
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
850
|
+
|
|
436
851
|
begin
|
|
437
852
|
workbook_path = resolve_workbook_entry(zip_reader)
|
|
438
853
|
wb_dir = File.dirname(workbook_path)
|
|
@@ -452,6 +867,7 @@ module Xlsxrb
|
|
|
452
867
|
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
453
868
|
workbook_sheets = wb_parsed[:sheets]
|
|
454
869
|
is_date1904 = wb_parsed[:date1904]
|
|
870
|
+
defined_names = wb_parsed[:defined_names] || []
|
|
455
871
|
|
|
456
872
|
non_worksheet_sheets = []
|
|
457
873
|
ws_idx = 0
|
|
@@ -465,31 +881,42 @@ module Xlsxrb
|
|
|
465
881
|
end
|
|
466
882
|
|
|
467
883
|
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
884
|
+
current_sheet_idx = ws_idx
|
|
468
885
|
ws_idx += 1
|
|
469
886
|
next unless sheet_path
|
|
470
887
|
|
|
471
|
-
|
|
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)
|
|
472
892
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
893
|
+
build_worksheet(
|
|
894
|
+
sheet_name,
|
|
895
|
+
sheet_xml,
|
|
476
896
|
shared_strings,
|
|
477
897
|
styles,
|
|
898
|
+
state: sheet_info[:state] || :visible,
|
|
478
899
|
zip_reader: zip_reader,
|
|
479
900
|
entry_name: sheet_path,
|
|
480
|
-
state: sheet_info[:state] || :visible,
|
|
481
901
|
date1904: is_date1904,
|
|
482
902
|
trim_empty_rows: trim_empty_rows,
|
|
483
903
|
pad_empty_rows: pad_empty_rows,
|
|
484
|
-
pad_empty_cells: pad_empty_cells
|
|
904
|
+
pad_empty_cells: pad_empty_cells,
|
|
905
|
+
print_area: pa,
|
|
906
|
+
print_titles: pt
|
|
485
907
|
)
|
|
486
908
|
end
|
|
487
909
|
|
|
488
910
|
wb_unmapped = {}
|
|
489
911
|
wb_props = {}
|
|
490
912
|
wb_props[:date1904] = true if is_date1904
|
|
913
|
+
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
491
914
|
wb_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
915
|
+
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
916
|
+
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
917
|
+
wb_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
492
918
|
wb_props[:full_calc_on_load] = wb_parsed[:full_calc_on_load] unless wb_parsed[:full_calc_on_load].nil?
|
|
919
|
+
wb_props[:code_name] = wb_parsed[:code_name] if wb_parsed[:code_name]
|
|
493
920
|
wb_unmapped[:workbook_properties] = wb_props unless wb_props.empty?
|
|
494
921
|
wb_unmapped[:non_worksheet_sheets] = non_worksheet_sheets unless non_worksheet_sheets.empty?
|
|
495
922
|
|
|
@@ -523,28 +950,141 @@ module Xlsxrb
|
|
|
523
950
|
Ooxml::XmlParser.parse(custom_xml, cu_listener)
|
|
524
951
|
wb_unmapped[:custom_properties] = cu_listener.properties unless cu_listener.properties.empty?
|
|
525
952
|
end
|
|
526
|
-
|
|
527
|
-
|
|
953
|
+
|
|
954
|
+
Elements::Workbook.new(
|
|
528
955
|
sheets: sheets,
|
|
529
956
|
shared_strings: shared_strings,
|
|
530
957
|
styles: styles,
|
|
531
958
|
unmapped_data: wb_unmapped,
|
|
532
959
|
defined_names: defined_names
|
|
533
960
|
)
|
|
961
|
+
ensure
|
|
962
|
+
zip_reader&.close
|
|
963
|
+
end
|
|
964
|
+
end
|
|
534
965
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
966
|
+
# Internal reader helper returning a Workbook with StreamSheet instances without deprecation warning.
|
|
967
|
+
# @api private
|
|
968
|
+
#: (untyped source, ?password: String?, ?trim_empty_rows: bool, ?pad_empty_rows: bool, ?pad_empty_cells: bool, ?max_uncompressed_size: Integer?) -> Elements::Workbook
|
|
969
|
+
def self._read_workbook(source, password: nil, trim_empty_rows: false, pad_empty_rows: false, pad_empty_cells: false, max_uncompressed_size: nil)
|
|
970
|
+
prepared_source = prepare_source_io(source, password)
|
|
971
|
+
zip_options = {}
|
|
972
|
+
zip_options[:max_uncompressed_size] = max_uncompressed_size if max_uncompressed_size
|
|
973
|
+
zip_reader = Ooxml::ZipReader.open(prepared_source, **zip_options)
|
|
974
|
+
|
|
975
|
+
workbook_path = resolve_workbook_entry(zip_reader)
|
|
976
|
+
wb_dir = File.dirname(workbook_path)
|
|
977
|
+
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
978
|
+
rels_xml = zip_reader.read_entry(wb_rels_name)
|
|
979
|
+
rels, rel_types = parse_workbook_relationships(rels_xml)
|
|
980
|
+
|
|
981
|
+
sst_entry = resolve_part_entry(zip_reader, rels, "sharedStrings", "sharedStrings.xml", wb_dir)
|
|
982
|
+
shared_strings_xml = sst_entry ? zip_reader.read_entry(sst_entry) : nil
|
|
983
|
+
shared_strings = shared_strings_xml ? Ooxml::SharedStringsParser.parse(shared_strings_xml) : []
|
|
984
|
+
|
|
985
|
+
styles_entry = resolve_part_entry(zip_reader, rels, "styles", "styles.xml", wb_dir)
|
|
986
|
+
styles_xml = styles_entry ? zip_reader.read_entry(styles_entry) : nil
|
|
987
|
+
styles = styles_xml ? Ooxml::StylesParser.parse(styles_xml) : {}
|
|
988
|
+
|
|
989
|
+
workbook_xml = zip_reader.read_entry(workbook_path)
|
|
990
|
+
wb_parsed = Ooxml::WorkbookParser.parse_with_properties(workbook_xml)
|
|
991
|
+
workbook_sheets = wb_parsed[:sheets]
|
|
992
|
+
is_date1904 = wb_parsed[:date1904]
|
|
993
|
+
|
|
994
|
+
non_worksheet_sheets = []
|
|
995
|
+
ws_idx = 0
|
|
996
|
+
sheets = workbook_sheets.filter_map do |sheet_info|
|
|
997
|
+
r_id = sheet_info[:r_id]
|
|
998
|
+
target = rels[r_id]
|
|
999
|
+
rel_type = rel_types[r_id]
|
|
1000
|
+
if non_worksheet_relationship?(rel_type, target)
|
|
1001
|
+
non_worksheet_sheets << sheet_info.merge(target: target, type: rel_type)
|
|
1002
|
+
next
|
|
1003
|
+
end
|
|
1004
|
+
|
|
1005
|
+
sheet_path = resolve_sheet_entry(zip_reader, target, wb_dir, ws_idx)
|
|
1006
|
+
current_sheet_idx = ws_idx
|
|
1007
|
+
ws_idx += 1
|
|
1008
|
+
next unless sheet_path
|
|
1009
|
+
|
|
1010
|
+
chunk_supplier = ->(&blk) { zip_reader.each_entry_chunk(sheet_path, &blk) }
|
|
1011
|
+
|
|
1012
|
+
StreamSheet.new(
|
|
1013
|
+
sheet_info[:name],
|
|
1014
|
+
chunk_supplier,
|
|
1015
|
+
shared_strings,
|
|
1016
|
+
styles,
|
|
1017
|
+
zip_reader: zip_reader,
|
|
1018
|
+
entry_name: sheet_path,
|
|
1019
|
+
state: sheet_info[:state] || :visible,
|
|
1020
|
+
date1904: is_date1904,
|
|
1021
|
+
trim_empty_rows: trim_empty_rows,
|
|
1022
|
+
pad_empty_rows: pad_empty_rows,
|
|
1023
|
+
pad_empty_cells: pad_empty_cells,
|
|
1024
|
+
defined_names: wb_parsed[:defined_names] || [],
|
|
1025
|
+
sheet_index: current_sheet_idx
|
|
1026
|
+
)
|
|
1027
|
+
end
|
|
1028
|
+
|
|
1029
|
+
wb_unmapped = {}
|
|
1030
|
+
wb_props = {}
|
|
1031
|
+
wb_props[:date1904] = true if is_date1904
|
|
1032
|
+
wb_props[:active_sheet] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1033
|
+
wb_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1034
|
+
wb_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
1035
|
+
wb_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
1036
|
+
wb_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
1037
|
+
wb_props[:full_calc_on_load] = wb_parsed[:full_calc_on_load] unless wb_parsed[:full_calc_on_load].nil?
|
|
1038
|
+
wb_props[:code_name] = wb_parsed[:code_name] if wb_parsed[:code_name]
|
|
1039
|
+
wb_unmapped[:workbook_properties] = wb_props unless wb_props.empty?
|
|
1040
|
+
wb_unmapped[:non_worksheet_sheets] = non_worksheet_sheets unless non_worksheet_sheets.empty?
|
|
1041
|
+
|
|
1042
|
+
if workbook_xml&.include?("<workbookProtection")
|
|
1043
|
+
prot_match = workbook_xml.match(%r{<workbookProtection\b([^>]*)/>})
|
|
1044
|
+
if prot_match
|
|
1045
|
+
prot = {}
|
|
1046
|
+
prot[:lock_structure] = true if prot_match[1].include?('lockStructure="1"') || prot_match[1].include?('lockStructure="true"')
|
|
1047
|
+
prot[:lock_windows] = true if prot_match[1].include?('lockWindows="1"') || prot_match[1].include?('lockWindows="true"')
|
|
1048
|
+
wb_unmapped[:workbook_protection] = prot unless prot.empty?
|
|
540
1049
|
end
|
|
541
|
-
ensure
|
|
542
|
-
zip_reader.close if block_given?
|
|
543
1050
|
end
|
|
1051
|
+
|
|
1052
|
+
core_xml = zip_reader.read_entry("docProps/core.xml")
|
|
1053
|
+
if core_xml && !core_xml.empty?
|
|
1054
|
+
c_listener = Ooxml::Reader::CorePropertiesListener.new
|
|
1055
|
+
Ooxml::XmlParser.parse(core_xml, c_listener)
|
|
1056
|
+
wb_unmapped[:core_properties] = c_listener.properties unless c_listener.properties.empty?
|
|
1057
|
+
end
|
|
1058
|
+
|
|
1059
|
+
app_xml = zip_reader.read_entry("docProps/app.xml")
|
|
1060
|
+
if app_xml && !app_xml.empty?
|
|
1061
|
+
a_listener = Ooxml::Reader::AppPropertiesListener.new
|
|
1062
|
+
Ooxml::XmlParser.parse(app_xml, a_listener)
|
|
1063
|
+
wb_unmapped[:app_properties] = a_listener.properties unless a_listener.properties.empty?
|
|
1064
|
+
end
|
|
1065
|
+
|
|
1066
|
+
custom_xml = zip_reader.read_entry("docProps/custom.xml")
|
|
1067
|
+
if custom_xml && !custom_xml.empty?
|
|
1068
|
+
cu_listener = Ooxml::Reader::CustomPropertiesListener.new
|
|
1069
|
+
Ooxml::XmlParser.parse(custom_xml, cu_listener)
|
|
1070
|
+
wb_unmapped[:custom_properties] = cu_listener.properties unless cu_listener.properties.empty?
|
|
1071
|
+
end
|
|
1072
|
+
defined_names = wb_parsed[:defined_names] || []
|
|
1073
|
+
Elements::Workbook.new(
|
|
1074
|
+
sheets: sheets,
|
|
1075
|
+
shared_strings: shared_strings,
|
|
1076
|
+
styles: styles,
|
|
1077
|
+
unmapped_data: wb_unmapped,
|
|
1078
|
+
defined_names: defined_names
|
|
1079
|
+
)
|
|
544
1080
|
end
|
|
1081
|
+
private_class_method :_read_workbook
|
|
545
1082
|
|
|
546
|
-
|
|
547
|
-
|
|
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(...)
|
|
548
1088
|
end
|
|
549
1089
|
|
|
550
1090
|
# Writes an XLSX file or IO stream (streaming or in-memory), or returns a binary string.
|
|
@@ -594,15 +1134,20 @@ module Xlsxrb
|
|
|
594
1134
|
# binary_data = Xlsxrb.write(workbook)
|
|
595
1135
|
#
|
|
596
1136
|
# @api public
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
1137
|
+
# @param shared_strings [Boolean] Whether to intern strings in a shared strings table (`xl/sharedStrings.xml`).
|
|
1138
|
+
# When set to `false`, strings are emitted directly as inline strings (`<c t="inlineStr">`), providing flat
|
|
1139
|
+
# $O(1)$ constant memory during streaming write. Disabling shared strings is recommended when streaming
|
|
1140
|
+
# large datasets where cells contain unique text (e.g. log exports, transaction details).
|
|
1141
|
+
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION).
|
|
1142
|
+
# Level 6 is standard. Level 4 accelerates compression by 20-40% with an ~10% size tradeoff,
|
|
1143
|
+
# recommended for high-throughput streaming exports. Level 0 disables compression (store only).
|
|
1144
|
+
#: (?untyped target_or_workbook, ?Elements::Workbook | untyped workbook_or_nil, ?password: String?, ?encryption_mode: Symbol, ?strict_excel_mode: bool, ?escape_formulas: bool, ?default_font: untyped?, ?shared_strings: bool, ?compression_level: Integer | nil) ?{ (StreamWriter) -> void } -> untyped
|
|
1145
|
+
def self.write(target_or_workbook = OMITTED_TARGET, workbook_or_nil = nil, password: nil, encryption_mode: :standard, strict_excel_mode: true, escape_formulas: false, default_font: nil, shared_strings: true, compression_level: Zlib::DEFAULT_COMPRESSION, &)
|
|
601
1146
|
if block_given?
|
|
602
1147
|
if target_or_workbook.equal?(OMITTED_TARGET)
|
|
603
1148
|
io = StringIO.new
|
|
604
1149
|
io.binmode
|
|
605
|
-
write(io, password: password, encryption_mode: encryption_mode, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, &)
|
|
1150
|
+
write(io, password: password, encryption_mode: encryption_mode, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level, &)
|
|
606
1151
|
return io.string.b
|
|
607
1152
|
end
|
|
608
1153
|
|
|
@@ -612,7 +1157,7 @@ module Xlsxrb
|
|
|
612
1157
|
if password && !password.empty?
|
|
613
1158
|
buf = StringIO.new
|
|
614
1159
|
buf.binmode
|
|
615
|
-
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas)
|
|
1160
|
+
stream_writer = StreamWriter.new(buf, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
616
1161
|
begin
|
|
617
1162
|
yield stream_writer
|
|
618
1163
|
stream_writer.close
|
|
@@ -627,7 +1172,7 @@ module Xlsxrb
|
|
|
627
1172
|
stream_writer.cleanup!
|
|
628
1173
|
end
|
|
629
1174
|
else
|
|
630
|
-
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas)
|
|
1175
|
+
stream_writer = StreamWriter.new(target, strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, default_font: default_font, shared_strings: shared_strings, compression_level: compression_level)
|
|
631
1176
|
begin
|
|
632
1177
|
yield stream_writer
|
|
633
1178
|
stream_writer.close
|
|
@@ -646,7 +1191,7 @@ module Xlsxrb
|
|
|
646
1191
|
|
|
647
1192
|
io = StringIO.new
|
|
648
1193
|
io.binmode
|
|
649
|
-
write(io, wb, password: password, encryption_mode: encryption_mode)
|
|
1194
|
+
write(io, wb, password: password, encryption_mode: encryption_mode, shared_strings: shared_strings, compression_level: compression_level)
|
|
650
1195
|
return io.string.b
|
|
651
1196
|
end
|
|
652
1197
|
|
|
@@ -655,27 +1200,34 @@ module Xlsxrb
|
|
|
655
1200
|
raise Error, "target is required" if target.nil?
|
|
656
1201
|
raise Error, "workbook must be an Elements::Workbook" unless workbook.is_a?(Elements::Workbook)
|
|
657
1202
|
|
|
658
|
-
|
|
659
|
-
|
|
1203
|
+
if shared_strings
|
|
1204
|
+
sst = []
|
|
1205
|
+
sst_index = {}
|
|
660
1206
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
1207
|
+
if workbook.shared_strings && !workbook.shared_strings.empty?
|
|
1208
|
+
workbook.shared_strings.each do |val|
|
|
1209
|
+
if (val.is_a?(String) || val.is_a?(Elements::RichText)) && !sst_index.key?(val)
|
|
1210
|
+
sst << val
|
|
1211
|
+
sst_index[val] = sst.size - 1
|
|
1212
|
+
end
|
|
666
1213
|
end
|
|
667
1214
|
end
|
|
1215
|
+
else
|
|
1216
|
+
sst = nil
|
|
1217
|
+
sst_index = nil
|
|
668
1218
|
end
|
|
669
1219
|
|
|
670
1220
|
# Collect shared strings and build index without allocating new Hashes
|
|
671
1221
|
sheet_data = workbook.sheets.map do |raw_ws|
|
|
672
1222
|
ws = raw_ws.respond_to?(:load) ? raw_ws.load : raw_ws
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
1223
|
+
if sst_index
|
|
1224
|
+
ws.rows.each do |row|
|
|
1225
|
+
row.cells.each do |cell|
|
|
1226
|
+
val = cell.value
|
|
1227
|
+
if (val.is_a?(String) || val.is_a?(Elements::RichText)) && !sst_index.key?(val)
|
|
1228
|
+
sst << val
|
|
1229
|
+
sst_index[val] = sst.size - 1
|
|
1230
|
+
end
|
|
679
1231
|
end
|
|
680
1232
|
end
|
|
681
1233
|
end
|
|
@@ -690,57 +1242,75 @@ module Xlsxrb
|
|
|
690
1242
|
end
|
|
691
1243
|
sd = { name: ws.name, rows: ws.rows, columns: columns }
|
|
692
1244
|
sd[:charts] = ws.charts unless ws.charts.empty?
|
|
693
|
-
sd[:conditional_formats] = ws.
|
|
1245
|
+
sd[:conditional_formats] = ws.conditional_formats unless ws.conditional_formats.empty?
|
|
694
1246
|
sd[:data_validations] = ws.data_validations unless ws.data_validations.empty?
|
|
695
1247
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
facade&.each { |key, val| sd[key] ||= val }
|
|
699
|
-
|
|
700
|
-
sd[:state] ||= ws.state if ws.respond_to?(:state) && ws.state && ws.state != :visible
|
|
701
|
-
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?
|
|
702
1250
|
if ws.respond_to?(:hyperlinks) && !ws.hyperlinks.empty?
|
|
703
|
-
sd[:hyperlinks]
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
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
|
|
711
1260
|
end
|
|
1261
|
+
else
|
|
1262
|
+
ws.hyperlinks
|
|
1263
|
+
end
|
|
712
1264
|
end
|
|
713
|
-
sd[:sparkline_groups]
|
|
714
|
-
sd[:sheet_properties]
|
|
715
|
-
sd[:sheet_format]
|
|
716
|
-
sd[:protected_ranges]
|
|
717
|
-
sd[:cell_watches]
|
|
718
|
-
sd[:
|
|
719
|
-
sd[:
|
|
720
|
-
sd[:
|
|
721
|
-
sd[:
|
|
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
|
|
722
1277
|
sd[:selection] ||= ws.sheet_view[:selection] if ws.respond_to?(:sheet_view) && ws.sheet_view.is_a?(Hash) && ws.sheet_view[:selection]
|
|
723
|
-
sd[:page_setup]
|
|
724
|
-
sd[:page_margins]
|
|
725
|
-
sd[:header_footer]
|
|
726
|
-
sd[:print_options]
|
|
727
|
-
sd[:sheet_protection]
|
|
728
|
-
sd[:tables]
|
|
729
|
-
sd[:merge_cells]
|
|
730
|
-
sd[:auto_filter]
|
|
731
|
-
sd[:
|
|
732
|
-
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
|
|
733
1301
|
if ws.respond_to?(:images) && ws.images && !ws.images.empty?
|
|
734
|
-
sd[:images]
|
|
1302
|
+
sd[:images] = ws.images.map do |img|
|
|
735
1303
|
if img.is_a?(Elements::Image)
|
|
736
1304
|
ext = img.filename ? File.extname(img.filename).delete_prefix(".").downcase : "png"
|
|
737
1305
|
opts = img.unmapped_data ? img.unmapped_data.dup : {}
|
|
738
1306
|
opts[:name] ||= img.name
|
|
739
1307
|
opts[:file_data] = img.data
|
|
740
1308
|
opts[:ext] = ext
|
|
741
|
-
opts[:
|
|
1309
|
+
opts[:from_column] = img.from_column
|
|
1310
|
+
opts[:from_col] = img.from_column
|
|
742
1311
|
opts[:from_row] = img.from_row
|
|
743
|
-
opts[:
|
|
1312
|
+
opts[:to_column] = img.to_column
|
|
1313
|
+
opts[:to_col] = img.to_column
|
|
744
1314
|
opts[:to_row] = img.to_row
|
|
745
1315
|
opts
|
|
746
1316
|
else
|
|
@@ -748,7 +1318,11 @@ module Xlsxrb
|
|
|
748
1318
|
end
|
|
749
1319
|
end
|
|
750
1320
|
end
|
|
751
|
-
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) }
|
|
752
1326
|
|
|
753
1327
|
sd
|
|
754
1328
|
end
|
|
@@ -773,8 +1347,10 @@ module Xlsxrb
|
|
|
773
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) }
|
|
774
1348
|
|
|
775
1349
|
pt = ws.print_titles
|
|
1350
|
+
pt = pt.to_h if pt.is_a?(Elements::MetadataRecord)
|
|
776
1351
|
parts = []
|
|
777
|
-
|
|
1352
|
+
col_val = pt[:columns] || pt[:cols]
|
|
1353
|
+
parts << "'#{sheet_name}'!$#{col_val.sub(":", ":$")}" if col_val
|
|
778
1354
|
parts << "'#{sheet_name}'!$#{pt[:rows].sub(":", ":$")}" if pt[:rows]
|
|
779
1355
|
defined_names << { name: "_xlnm.Print_Titles", value: parts.join(","), local_sheet_id: idx } unless parts.empty?
|
|
780
1356
|
end
|
|
@@ -794,7 +1370,8 @@ module Xlsxrb
|
|
|
794
1370
|
app_properties: wb_app,
|
|
795
1371
|
custom_properties: wb_custom,
|
|
796
1372
|
workbook_protection: wb_prot,
|
|
797
|
-
workbook_properties: wb_properties
|
|
1373
|
+
workbook_properties: wb_properties,
|
|
1374
|
+
compression_level: compression_level
|
|
798
1375
|
)
|
|
799
1376
|
encrypted_bytes = Ooxml::Crypto.encrypt(buf.string.b, password, mode: encryption_mode)
|
|
800
1377
|
if target.is_a?(String)
|
|
@@ -814,11 +1391,45 @@ module Xlsxrb
|
|
|
814
1391
|
app_properties: wb_app,
|
|
815
1392
|
custom_properties: wb_custom,
|
|
816
1393
|
workbook_protection: wb_prot,
|
|
817
|
-
workbook_properties: wb_properties
|
|
1394
|
+
workbook_properties: wb_properties,
|
|
1395
|
+
compression_level: compression_level
|
|
818
1396
|
)
|
|
819
1397
|
end
|
|
820
1398
|
end
|
|
821
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
|
+
|
|
822
1433
|
# Modifies an existing XLSX file in-memory using an immutable transformation block.
|
|
823
1434
|
#
|
|
824
1435
|
# Reads the workbook, passes it to the block, and writes the resulting workbook.
|
|
@@ -834,14 +1445,17 @@ module Xlsxrb
|
|
|
834
1445
|
#
|
|
835
1446
|
# @param source [String, IO, StringIO] The source file path or IO object.
|
|
836
1447
|
# @param target [String, IO, StringIO, nil] Target file path or IO object (overwrites source if nil).
|
|
837
|
-
# @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).
|
|
838
1451
|
# @yield [workbook]
|
|
839
1452
|
# @yieldparam workbook [Elements::Workbook] The loaded workbook.
|
|
840
1453
|
# @yieldreturn [Elements::Workbook] The modified workbook.
|
|
841
|
-
# @return [
|
|
1454
|
+
# @return [Elements::Workbook] The modified workbook returned by the block.
|
|
842
1455
|
# @api public
|
|
843
|
-
#: (untyped source, ?untyped target, ?password: String?) ?{ (Elements::Workbook) -> untyped } ->
|
|
844
|
-
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
|
|
845
1459
|
raise Error, "source is required" if source.nil?
|
|
846
1460
|
raise Error, "block is required" unless block_given?
|
|
847
1461
|
|
|
@@ -863,8 +1477,12 @@ module Xlsxrb
|
|
|
863
1477
|
|
|
864
1478
|
replaced_entries = {}
|
|
865
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]
|
|
866
1484
|
|
|
867
|
-
Ooxml::ZipReader.open(prepared_source) do |zip_reader|
|
|
1485
|
+
Ooxml::ZipReader.open(prepared_source, **zip_options) do |zip_reader|
|
|
868
1486
|
workbook_path = resolve_workbook_entry(zip_reader)
|
|
869
1487
|
wb_dir = File.dirname(workbook_path)
|
|
870
1488
|
wb_rels_name = wb_dir == "." ? "_rels/#{File.basename(workbook_path)}.rels" : "#{wb_dir}/_rels/#{File.basename(workbook_path)}.rels"
|
|
@@ -894,18 +1512,37 @@ module Xlsxrb
|
|
|
894
1512
|
next if non_worksheet_relationship?(rel_types[r_id], target_path)
|
|
895
1513
|
|
|
896
1514
|
sheet_path = resolve_sheet_entry(zip_reader, target_path, wb_dir, ws_idx)
|
|
1515
|
+
current_ws_idx = ws_idx
|
|
897
1516
|
ws_idx += 1
|
|
898
1517
|
next unless sheet_path
|
|
899
1518
|
|
|
900
1519
|
sheet_entry_map[sheet_info[:name]] = sheet_path
|
|
901
1520
|
sheet_xml = zip_reader.read_entry(sheet_path) || ""
|
|
902
|
-
|
|
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
|
+
)
|
|
903
1537
|
end
|
|
904
1538
|
|
|
905
1539
|
mod_unmapped = {}
|
|
906
1540
|
mod_props = {}
|
|
907
1541
|
mod_props[:date1904] = true if wb_parsed[:date1904]
|
|
908
1542
|
mod_props[:active_tab] = wb_parsed[:active_tab] unless wb_parsed[:active_tab].nil?
|
|
1543
|
+
mod_props[:first_sheet] = wb_parsed[:first_sheet] unless wb_parsed[:first_sheet].nil?
|
|
1544
|
+
mod_props[:show_sheet_tabs] = wb_parsed[:show_sheet_tabs] unless wb_parsed[:show_sheet_tabs].nil?
|
|
1545
|
+
mod_props[:tab_ratio] = wb_parsed[:tab_ratio] unless wb_parsed[:tab_ratio].nil?
|
|
909
1546
|
mod_props[:full_calc_on_load] = wb_parsed[:full_calc_on_load] unless wb_parsed[:full_calc_on_load].nil?
|
|
910
1547
|
mod_unmapped[:workbook_properties] = mod_props unless mod_props.empty?
|
|
911
1548
|
|
|
@@ -918,15 +1555,23 @@ module Xlsxrb
|
|
|
918
1555
|
)
|
|
919
1556
|
mutable_shared_strings = shared_strings.dup
|
|
920
1557
|
|
|
921
|
-
|
|
922
|
-
|
|
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
|
|
923
1567
|
|
|
924
1568
|
structural_change = result_workbook.sheets.size != original_workbook.sheets.size ||
|
|
925
1569
|
result_workbook.sheets.map(&:name) != original_workbook.sheets.map(&:name)
|
|
926
1570
|
|
|
927
1571
|
if structural_change
|
|
928
1572
|
write_target = target || source
|
|
929
|
-
|
|
1573
|
+
write_opts = opts.slice(:encryption_mode, :compression_level, :shared_strings)
|
|
1574
|
+
write(write_target, result_workbook, password: write_password, **write_opts)
|
|
930
1575
|
else
|
|
931
1576
|
result_workbook.sheets.each_with_index do |new_sheet, idx|
|
|
932
1577
|
orig_sheet = original_workbook.sheets[idx]
|
|
@@ -1004,7 +1649,7 @@ module Xlsxrb
|
|
|
1004
1649
|
write_dest,
|
|
1005
1650
|
zip_reader,
|
|
1006
1651
|
replaced_entries,
|
|
1007
|
-
password:
|
|
1652
|
+
password: write_password
|
|
1008
1653
|
)
|
|
1009
1654
|
end
|
|
1010
1655
|
end
|
|
@@ -1022,6 +1667,7 @@ module Xlsxrb
|
|
|
1022
1667
|
source.rewind
|
|
1023
1668
|
end
|
|
1024
1669
|
end
|
|
1670
|
+
block_result
|
|
1025
1671
|
ensure
|
|
1026
1672
|
if temp_target_path && File.exist?(temp_target_path)
|
|
1027
1673
|
begin
|
|
@@ -1044,15 +1690,17 @@ module Xlsxrb
|
|
|
1044
1690
|
# end
|
|
1045
1691
|
#
|
|
1046
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.
|
|
1047
1695
|
# @yield [builder]
|
|
1048
1696
|
# @yieldparam builder [Xlsxrb::WorkbookBuilder]
|
|
1049
1697
|
# @return [Elements::Workbook] The compiled in-memory workbook.
|
|
1050
1698
|
# @api public
|
|
1051
|
-
#: (?strict_excel_mode: bool) ?{ (WorkbookBuilder) -> void } -> Elements::Workbook
|
|
1052
|
-
def self.build(strict_excel_mode: true)
|
|
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)
|
|
1053
1701
|
raise Error, "block is required" unless block_given?
|
|
1054
1702
|
|
|
1055
|
-
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode)
|
|
1703
|
+
builder = WorkbookBuilder.new(strict_excel_mode: strict_excel_mode, escape_formulas: escape_formulas, default_font: default_font)
|
|
1056
1704
|
yield builder
|
|
1057
1705
|
builder.build
|
|
1058
1706
|
end
|
|
@@ -1138,30 +1786,43 @@ module Xlsxrb
|
|
|
1138
1786
|
fallbacks.find { |f| zip_reader.entry?(f) }
|
|
1139
1787
|
end
|
|
1140
1788
|
|
|
1141
|
-
#: (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]?, ?selection: (Hash[Symbol, untyped] | Array[Hash[Symbol, untyped]])?) -> Elements::Worksheet
|
|
1142
|
-
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, selection: 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)
|
|
1143
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
|
|
1144
1793
|
if sheet_xml.nil? || sheet_xml.empty?
|
|
1145
1794
|
return Elements::Worksheet.new(
|
|
1146
1795
|
name: name, state: state, styles: styles, images: images || [], date1904: date1904,
|
|
1147
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,
|
|
1148
1802
|
merged_cells: merged_cells || [], auto_filter: auto_filter,
|
|
1149
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 || [],
|
|
1150
1807
|
page_setup: page_setup || {}, page_margins: page_margins, header_footer: header_footer || {},
|
|
1151
1808
|
print_options: print_options || {}, sheet_protection: sheet_protection, sheet_view: sheet_view || {},
|
|
1152
|
-
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [],
|
|
1153
|
-
protected_ranges: protected_ranges || [], cell_watches: cell_watches || [],
|
|
1809
|
+
sheet_properties: sheet_properties || {}, sheet_format: sheet_format || {}, row_breaks: row_breaks || [], column_breaks: column_breaks || col_breaks || [],
|
|
1810
|
+
protected_ranges: protected_ranges || [], cell_watches: cell_watches || [], ignored_errors: ignored_errors || [],
|
|
1811
|
+
phonetic_properties: phonetic_properties, selection: selection, sheet_calc_properties: resolved_calc_pr,
|
|
1812
|
+
background_image: background_image,
|
|
1813
|
+
threaded_comments: threaded_comments || []
|
|
1154
1814
|
)
|
|
1155
1815
|
end
|
|
1156
1816
|
|
|
1157
1817
|
sheet_hyperlinks = hyperlinks || resolve_hyperlinks(sheet_xml, zip_reader: zip_reader, entry_name: entry_name)
|
|
1158
1818
|
sheet_comments = comments || resolve_comments(zip_reader: zip_reader, entry_name: entry_name)
|
|
1819
|
+
sheet_threaded_comments = threaded_comments || resolve_threaded_comments(zip_reader: zip_reader, entry_name: entry_name)
|
|
1159
1820
|
sheet_images = images || resolve_images(zip_reader: zip_reader, entry_name: entry_name)
|
|
1160
1821
|
raw_rows = Ooxml::WorksheetParser.parse(sheet_xml, shared_strings: shared_strings, styles: styles)
|
|
1161
1822
|
raw_columns = Ooxml::WorksheetParser.parse_columns(sheet_xml)
|
|
1162
1823
|
|
|
1163
|
-
rows = raw_rows.map { |rr| build_row_from_raw(rr, sheet_hyperlinks, sheet_comments, styles, date1904: date1904) }
|
|
1164
|
-
|
|
1824
|
+
rows = raw_rows.map { |rr| build_row_from_raw(rr, sheet_hyperlinks, sheet_comments, styles, date1904: date1904, threaded_comments: sheet_threaded_comments) }
|
|
1825
|
+
sheet_columns = columns || raw_columns.map do |rc|
|
|
1165
1826
|
# Columns from OOXML are 1-based min/max ranges; convert to 0-based
|
|
1166
1827
|
col_unmapped = {}
|
|
1167
1828
|
col_unmapped[:style_index] = rc[:style_index] if rc[:style_index]
|
|
@@ -1202,16 +1863,27 @@ module Xlsxrb
|
|
|
1202
1863
|
[]
|
|
1203
1864
|
end
|
|
1204
1865
|
|
|
1205
|
-
sheet_auto_filter = auto_filter
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
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
|
|
1210
1881
|
|
|
1211
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)
|
|
1212
1884
|
sheet_shapes = shapes || resolve_shapes(zip_reader: zip_reader, entry_name: entry_name)
|
|
1213
1885
|
|
|
1214
|
-
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?
|
|
1215
1887
|
if sheet_xml.include?("<sheetView")
|
|
1216
1888
|
v_listener = Ooxml::Reader::SheetViewListener.new
|
|
1217
1889
|
Ooxml::XmlParser.parse(sheet_xml, v_listener)
|
|
@@ -1220,9 +1892,10 @@ module Xlsxrb
|
|
|
1220
1892
|
{ view: {}, pane: nil, selection: nil }
|
|
1221
1893
|
end
|
|
1222
1894
|
else
|
|
1223
|
-
{ view: sheet_view, pane: freeze_pane, selection: selection }
|
|
1895
|
+
{ view: sheet_view, pane: freeze_pane || split_pane, selection: selection }
|
|
1224
1896
|
end
|
|
1225
|
-
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)
|
|
1226
1899
|
sheet_view_val = sheet_view || sheet_view_data[:view] || {}
|
|
1227
1900
|
sheet_selection = selection || sheet_view_data[:selection]
|
|
1228
1901
|
|
|
@@ -1232,7 +1905,7 @@ module Xlsxrb
|
|
|
1232
1905
|
prot_listener.protection
|
|
1233
1906
|
end
|
|
1234
1907
|
|
|
1235
|
-
sheet_props = sheet_properties || if sheet_xml.include?("<sheetPr")
|
|
1908
|
+
sheet_props = sheet_properties || if sheet_xml.include?("<sheetPr") || sheet_xml.include?("<sheetCalcPr")
|
|
1236
1909
|
pr_listener = Ooxml::Reader::SheetPropertiesListener.new
|
|
1237
1910
|
Ooxml::XmlParser.parse(sheet_xml, pr_listener)
|
|
1238
1911
|
pr_listener.properties
|
|
@@ -1248,7 +1921,7 @@ module Xlsxrb
|
|
|
1248
1921
|
{}
|
|
1249
1922
|
end
|
|
1250
1923
|
|
|
1251
|
-
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?)
|
|
1252
1925
|
if sheet_xml.match?(/<(?:pageSetup|pageMargins|headerFooter|printOptions|rowBreaks|colBreaks)\b/)
|
|
1253
1926
|
pg_listener = Ooxml::Reader::PrintPageListener.new
|
|
1254
1927
|
Ooxml::XmlParser.parse(sheet_xml, pg_listener)
|
|
@@ -1258,13 +1931,15 @@ module Xlsxrb
|
|
|
1258
1931
|
header_footer: pg_listener.header_footer,
|
|
1259
1932
|
print_options: pg_listener.print_options,
|
|
1260
1933
|
row_breaks: pg_listener.row_breaks,
|
|
1934
|
+
column_breaks: pg_listener.column_breaks,
|
|
1261
1935
|
col_breaks: pg_listener.col_breaks
|
|
1262
1936
|
}
|
|
1263
1937
|
else
|
|
1264
|
-
{ 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: [] }
|
|
1265
1939
|
end
|
|
1266
1940
|
else
|
|
1267
|
-
|
|
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 }
|
|
1268
1943
|
end
|
|
1269
1944
|
|
|
1270
1945
|
sheet_prot_ranges = protected_ranges || if sheet_xml.include?("<protectedRanges")
|
|
@@ -1283,11 +1958,33 @@ module Xlsxrb
|
|
|
1283
1958
|
[]
|
|
1284
1959
|
end
|
|
1285
1960
|
|
|
1961
|
+
sheet_ignored_errors = ignored_errors || if sheet_xml.include?("<ignoredErrors")
|
|
1962
|
+
ie_listener = Ooxml::Reader::IgnoredErrorsListener.new
|
|
1963
|
+
Ooxml::XmlParser.parse(sheet_xml, ie_listener)
|
|
1964
|
+
ie_listener.errors
|
|
1965
|
+
else
|
|
1966
|
+
[]
|
|
1967
|
+
end
|
|
1968
|
+
|
|
1969
|
+
sheet_phonetic = phonetic_properties || if sheet_xml.include?("<phoneticPr")
|
|
1970
|
+
pp_listener = Ooxml::Reader::PhoneticPrListener.new
|
|
1971
|
+
Ooxml::XmlParser.parse(sheet_xml, pp_listener)
|
|
1972
|
+
pp_listener.phonetic_pr
|
|
1973
|
+
end
|
|
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
|
+
|
|
1286
1979
|
Elements::Worksheet.new(
|
|
1287
1980
|
name: name,
|
|
1288
1981
|
rows: rows,
|
|
1289
|
-
columns:
|
|
1290
|
-
|
|
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,
|
|
1291
1988
|
data_validations: dvs,
|
|
1292
1989
|
state: state,
|
|
1293
1990
|
hyperlinks: sheet_hyperlinks,
|
|
@@ -1302,8 +1999,13 @@ module Xlsxrb
|
|
|
1302
1999
|
merged_cells: sheet_merged_cells,
|
|
1303
2000
|
auto_filter: sheet_auto_filter,
|
|
1304
2001
|
tables: sheet_tables,
|
|
2002
|
+
pivot_tables: sheet_pivot_tables,
|
|
1305
2003
|
shapes: sheet_shapes,
|
|
1306
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,
|
|
1307
2009
|
page_setup: page_setup || print_data[:page_setup] || {},
|
|
1308
2010
|
page_margins: page_margins || print_data[:page_margins],
|
|
1309
2011
|
header_footer: header_footer || print_data[:header_footer] || {},
|
|
@@ -1313,10 +2015,15 @@ module Xlsxrb
|
|
|
1313
2015
|
sheet_properties: sheet_props,
|
|
1314
2016
|
sheet_format: sheet_fmt,
|
|
1315
2017
|
row_breaks: row_breaks || print_data[:row_breaks] || [],
|
|
1316
|
-
|
|
2018
|
+
column_breaks: column_breaks || col_breaks || print_data[:column_breaks] || print_data[:col_breaks] || [],
|
|
1317
2019
|
protected_ranges: sheet_prot_ranges,
|
|
1318
2020
|
cell_watches: sheet_watches,
|
|
1319
|
-
|
|
2021
|
+
ignored_errors: sheet_ignored_errors,
|
|
2022
|
+
phonetic_properties: sheet_phonetic,
|
|
2023
|
+
selection: sheet_selection,
|
|
2024
|
+
sheet_calc_properties: resolved_calc_pr,
|
|
2025
|
+
background_image: background_image || (sheet_xml.include?("<picture") ? resolve_background_image(sheet_xml, zip_reader: zip_reader, entry_name: entry_name) : nil),
|
|
2026
|
+
threaded_comments: sheet_threaded_comments
|
|
1320
2027
|
)
|
|
1321
2028
|
end
|
|
1322
2029
|
|
|
@@ -1346,7 +2053,14 @@ module Xlsxrb
|
|
|
1346
2053
|
entry[:display] = link[:display] if link[:display]
|
|
1347
2054
|
entry[:tooltip] = link[:tooltip] if link[:tooltip]
|
|
1348
2055
|
entry[:location] = link[:location] if link[:location]
|
|
1349
|
-
|
|
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
|
+
)
|
|
1350
2064
|
end
|
|
1351
2065
|
result
|
|
1352
2066
|
end
|
|
@@ -1394,6 +2108,69 @@ module Xlsxrb
|
|
|
1394
2108
|
c_listener.comments
|
|
1395
2109
|
end
|
|
1396
2110
|
|
|
2111
|
+
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Hash[Symbol, untyped]]
|
|
2112
|
+
def resolve_threaded_comments(zip_reader: nil, entry_name: nil)
|
|
2113
|
+
return [] unless zip_reader && entry_name
|
|
2114
|
+
|
|
2115
|
+
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2116
|
+
rels_xml = zip_reader.read_entry(rels_path)
|
|
2117
|
+
return [] if rels_xml.nil? || rels_xml.empty?
|
|
2118
|
+
|
|
2119
|
+
parser = REXML::Parsers::SAX2Parser.new(rels_xml)
|
|
2120
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2121
|
+
parser.listen(listener)
|
|
2122
|
+
parser.parse
|
|
2123
|
+
|
|
2124
|
+
rel = listener.relationships.find { |r| r[:type]&.end_with?("/threadedComment") }
|
|
2125
|
+
return [] unless rel
|
|
2126
|
+
|
|
2127
|
+
base_dir = File.dirname(entry_name)
|
|
2128
|
+
tc_path = resolve_relative_path(base_dir, rel[:target])
|
|
2129
|
+
|
|
2130
|
+
xml = zip_reader.read_entry(tc_path)
|
|
2131
|
+
return [] if xml.nil? || xml.empty?
|
|
2132
|
+
|
|
2133
|
+
tc_parser = REXML::Parsers::SAX2Parser.new(xml)
|
|
2134
|
+
tc_listener = Ooxml::Reader::ThreadedCommentsListener.new
|
|
2135
|
+
tc_parser.listen(tc_listener)
|
|
2136
|
+
tc_parser.parse
|
|
2137
|
+
|
|
2138
|
+
persons_map = resolve_persons_map(zip_reader: zip_reader)
|
|
2139
|
+
tc_listener.threaded_comments.map do |tc|
|
|
2140
|
+
tc_author = persons_map[tc[:person_id]]
|
|
2141
|
+
tc[:author] = tc_author if tc_author && !tc[:author]
|
|
2142
|
+
tc
|
|
2143
|
+
end
|
|
2144
|
+
end
|
|
2145
|
+
|
|
2146
|
+
#: (?zip_reader: Ooxml::ZipReader?) -> Hash[String, String]
|
|
2147
|
+
def resolve_persons_map(zip_reader: nil)
|
|
2148
|
+
return {} unless zip_reader
|
|
2149
|
+
|
|
2150
|
+
xml = zip_reader.read_entry("xl/persons/person.xml")
|
|
2151
|
+
if xml.nil? || xml.empty?
|
|
2152
|
+
wb_rels_xml = zip_reader.read_entry("xl/_rels/workbook.xml.rels")
|
|
2153
|
+
if wb_rels_xml && !wb_rels_xml.empty?
|
|
2154
|
+
parser = REXML::Parsers::SAX2Parser.new(wb_rels_xml)
|
|
2155
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2156
|
+
parser.listen(listener)
|
|
2157
|
+
parser.parse
|
|
2158
|
+
p_rel = listener.relationships.find { |r| r[:type]&.end_with?("/person") }
|
|
2159
|
+
if p_rel
|
|
2160
|
+
p_path = resolve_relative_path("xl", p_rel[:target])
|
|
2161
|
+
xml = zip_reader.read_entry(p_path)
|
|
2162
|
+
end
|
|
2163
|
+
end
|
|
2164
|
+
end
|
|
2165
|
+
return {} if xml.nil? || xml.empty?
|
|
2166
|
+
|
|
2167
|
+
p_parser = REXML::Parsers::SAX2Parser.new(xml)
|
|
2168
|
+
p_listener = Ooxml::Reader::PersonListListener.new
|
|
2169
|
+
p_parser.listen(p_listener)
|
|
2170
|
+
p_parser.parse
|
|
2171
|
+
p_listener.persons.to_h { |p| [p[:id], p[:display_name]] }
|
|
2172
|
+
end
|
|
2173
|
+
|
|
1397
2174
|
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Elements::Image]
|
|
1398
2175
|
def resolve_images(zip_reader: nil, entry_name: nil)
|
|
1399
2176
|
return [] unless zip_reader && entry_name
|
|
@@ -1446,9 +2223,9 @@ module Xlsxrb
|
|
|
1446
2223
|
img_data = zip_reader.read_entry(media_path)
|
|
1447
2224
|
end
|
|
1448
2225
|
|
|
1449
|
-
from_col = raw_img[:from_col] || 0
|
|
2226
|
+
from_col = raw_img[:from_column] || raw_img[:from_col] || 0
|
|
1450
2227
|
from_row = raw_img[:from_row] || 0
|
|
1451
|
-
to_col = raw_img[:to_col]
|
|
2228
|
+
to_col = raw_img[:to_column] || raw_img[:to_col]
|
|
1452
2229
|
to_row = raw_img[:to_row]
|
|
1453
2230
|
cx = raw_img[:cx]
|
|
1454
2231
|
cy = raw_img[:cy]
|
|
@@ -1458,9 +2235,9 @@ module Xlsxrb
|
|
|
1458
2235
|
filename: filename || "image.png",
|
|
1459
2236
|
name: name,
|
|
1460
2237
|
data: img_data,
|
|
1461
|
-
|
|
2238
|
+
from_column: from_col,
|
|
1462
2239
|
from_row: from_row,
|
|
1463
|
-
|
|
2240
|
+
to_column: to_col,
|
|
1464
2241
|
to_row: to_row,
|
|
1465
2242
|
cx: cx,
|
|
1466
2243
|
cy: cy,
|
|
@@ -1469,6 +2246,52 @@ module Xlsxrb
|
|
|
1469
2246
|
end
|
|
1470
2247
|
end
|
|
1471
2248
|
|
|
2249
|
+
#: (String? sheet_xml, ?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Hash[Symbol, untyped]?
|
|
2250
|
+
def resolve_background_image(sheet_xml, zip_reader: nil, entry_name: nil)
|
|
2251
|
+
return nil unless sheet_xml && zip_reader && entry_name
|
|
2252
|
+
return nil unless sheet_xml.include?("<picture")
|
|
2253
|
+
|
|
2254
|
+
m = sheet_xml.match(/<(?:[a-zA-Z0-9_]+:)?picture\b[^>]*\br:id=["']([^"']+)["']/)
|
|
2255
|
+
return nil unless m
|
|
2256
|
+
|
|
2257
|
+
picture_rid = m[1]
|
|
2258
|
+
rels_path = entry_name.sub(%r{([^/]+)$}, '_rels/\1.rels')
|
|
2259
|
+
rels_xml = zip_reader.read_entry(rels_path)
|
|
2260
|
+
return nil if rels_xml.nil? || rels_xml.empty?
|
|
2261
|
+
|
|
2262
|
+
parser = REXML::Parsers::SAX2Parser.new(rels_xml)
|
|
2263
|
+
listener = Ooxml::Reader::RelsListener.new
|
|
2264
|
+
parser.listen(listener)
|
|
2265
|
+
parser.parse
|
|
2266
|
+
|
|
2267
|
+
pic_rel = listener.relationships.find { |r| r[:id] == picture_rid }
|
|
2268
|
+
return nil unless pic_rel
|
|
2269
|
+
|
|
2270
|
+
base_dir = File.dirname(entry_name)
|
|
2271
|
+
target_path = resolve_relative_path(base_dir, pic_rel[:target])
|
|
2272
|
+
data = zip_reader.read_entry(target_path)
|
|
2273
|
+
return nil unless data
|
|
2274
|
+
|
|
2275
|
+
ext = File.extname(target_path).delete_prefix(".").downcase
|
|
2276
|
+
mime = case ext
|
|
2277
|
+
when "png" then "image/png"
|
|
2278
|
+
when "jpg", "jpeg" then "image/jpeg"
|
|
2279
|
+
when "gif" then "image/gif"
|
|
2280
|
+
when "bmp" then "image/bmp"
|
|
2281
|
+
when "emf" then "image/x-emf"
|
|
2282
|
+
when "wmf" then "image/x-wmf"
|
|
2283
|
+
else "application/octet-stream"
|
|
2284
|
+
end
|
|
2285
|
+
|
|
2286
|
+
{
|
|
2287
|
+
data: data.b,
|
|
2288
|
+
file_data: data.b,
|
|
2289
|
+
ext: ext,
|
|
2290
|
+
filename: File.basename(target_path),
|
|
2291
|
+
content_type: mime
|
|
2292
|
+
}.freeze
|
|
2293
|
+
end
|
|
2294
|
+
|
|
1472
2295
|
#: (?zip_reader: Ooxml::ZipReader?, ?entry_name: String?) -> Array[Hash[Symbol, untyped]]
|
|
1473
2296
|
def resolve_tables(zip_reader: nil, entry_name: nil)
|
|
1474
2297
|
return [] unless zip_reader && entry_name
|
|
@@ -1527,34 +2350,572 @@ module Xlsxrb
|
|
|
1527
2350
|
d_listener.shapes
|
|
1528
2351
|
end
|
|
1529
2352
|
|
|
1530
|
-
#: (
|
|
1531
|
-
def
|
|
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)
|
|
2854
|
+
def build_row_from_raw(raw_row, hyperlinks = {}, comments = [], styles = nil, date1904: false, threaded_comments: [])
|
|
1532
2855
|
return raw_row if raw_row.is_a?(Elements::Row)
|
|
1533
2856
|
|
|
1534
2857
|
comments_map = if comments.is_a?(Hash)
|
|
1535
|
-
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
|
|
1536
2861
|
else
|
|
1537
|
-
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
|
|
1538
2867
|
end
|
|
1539
2868
|
|
|
2869
|
+
threaded_comments_map = if threaded_comments.is_a?(Hash)
|
|
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
|
|
2876
|
+
else
|
|
2877
|
+
threaded_comments.each_with_object({}) do |c, acc|
|
|
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
|
|
2881
|
+
end
|
|
2882
|
+
end
|
|
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
|
+
|
|
1540
2892
|
raw_cells = raw_row[:cells]
|
|
1541
2893
|
cells = raw_cells.map do |rc|
|
|
1542
2894
|
if rc.is_a?(Elements::Cell)
|
|
1543
|
-
hl =
|
|
1544
|
-
cm = comments_map
|
|
2895
|
+
hl = hyperlinks_map[rc.ref]
|
|
2896
|
+
cm = comments_map[rc.ref]
|
|
2897
|
+
tcs = threaded_comments_map[rc.ref]
|
|
1545
2898
|
fmt = rc.format_code || (NumberFormatter.format_code_for(rc.style_index, styles) if styles && rc.style_index)
|
|
1546
|
-
if hl || cm || (fmt && rc.format_code != fmt) || (date1904 && !rc.date1904?)
|
|
1547
|
-
rc.with(
|
|
2899
|
+
if hl || cm || tcs || (fmt && rc.format_code != fmt) || (date1904 && !rc.date1904?)
|
|
2900
|
+
rc.with(
|
|
2901
|
+
hyperlink: hl || rc.hyperlink,
|
|
2902
|
+
comment: cm || rc.comment,
|
|
2903
|
+
format_code: fmt || rc.format_code,
|
|
2904
|
+
date1904: date1904 || rc.date1904?,
|
|
2905
|
+
threaded_comments: (tcs ? (rc.threaded_comments + tcs) : rc.threaded_comments)
|
|
2906
|
+
)
|
|
1548
2907
|
else
|
|
1549
2908
|
rc
|
|
1550
2909
|
end
|
|
1551
2910
|
else
|
|
1552
|
-
parsed = Elements::Cell.
|
|
2911
|
+
parsed = Elements::Cell._parse_ref(rc[:ref]) if rc[:ref]
|
|
1553
2912
|
row_idx = parsed ? parsed[0] : raw_row[:index]
|
|
1554
2913
|
col_idx = parsed ? parsed[1] : 0
|
|
1555
|
-
ref = rc[:ref] || "#{Elements::Cell.
|
|
1556
|
-
|
|
1557
|
-
|
|
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]
|
|
1558
2919
|
fmt = rc[:format_code] || (NumberFormatter.format_code_for(rc[:style_index], styles) if styles && rc[:style_index])
|
|
1559
2920
|
|
|
1560
2921
|
val = rc[:value]
|
|
@@ -1577,7 +2938,8 @@ module Xlsxrb
|
|
|
1577
2938
|
raw_value: raw_val,
|
|
1578
2939
|
format_code: fmt,
|
|
1579
2940
|
errors: cell_errors,
|
|
1580
|
-
date1904: date1904
|
|
2941
|
+
date1904: date1904,
|
|
2942
|
+
threaded_comments: tcs
|
|
1581
2943
|
)
|
|
1582
2944
|
end
|
|
1583
2945
|
end
|
|
@@ -1601,95 +2963,12 @@ module Xlsxrb
|
|
|
1601
2963
|
custom_height: attrs[:custom_height] || false,
|
|
1602
2964
|
collapsed: attrs[:collapsed] || false,
|
|
1603
2965
|
outline_level: attrs[:outline_level],
|
|
2966
|
+
style_index: attrs[:style_index],
|
|
1604
2967
|
unmapped_data: row_unmapped,
|
|
1605
2968
|
errors: row_errors
|
|
1606
2969
|
)
|
|
1607
2970
|
end
|
|
1608
2971
|
|
|
1609
|
-
#: (Elements::Cell cell, Array[String] sst, Hash[String, Integer] sst_index, ?date1904: bool) -> Hash[Symbol, untyped]
|
|
1610
|
-
def build_raw_cell(cell, sst, sst_index, date1904: false)
|
|
1611
|
-
# simplecov:disable
|
|
1612
|
-
# Edge case / untested delegation block
|
|
1613
|
-
ref = cell.ref
|
|
1614
|
-
value = cell.value
|
|
1615
|
-
result = { ref: ref, style_index: cell.style_index }
|
|
1616
|
-
|
|
1617
|
-
case value
|
|
1618
|
-
when String, Xlsxrb::Elements::RichText
|
|
1619
|
-
idx = sst_index[value] ||= begin
|
|
1620
|
-
sst << value
|
|
1621
|
-
sst.size - 1
|
|
1622
|
-
end
|
|
1623
|
-
result[:value] = idx
|
|
1624
|
-
result[:type] = "s"
|
|
1625
|
-
when true, false
|
|
1626
|
-
result[:value] = value
|
|
1627
|
-
result[:type] = "b"
|
|
1628
|
-
when Integer, Float
|
|
1629
|
-
result[:value] = value
|
|
1630
|
-
when Xlsxrb::Elements::CellError
|
|
1631
|
-
result[:value] = value.code
|
|
1632
|
-
result[:type] = "e"
|
|
1633
|
-
when Date
|
|
1634
|
-
result[:value] = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
|
|
1635
|
-
when Time
|
|
1636
|
-
result[:value] = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
|
|
1637
|
-
# simplecov:enable
|
|
1638
|
-
when NilClass
|
|
1639
|
-
# empty cell
|
|
1640
|
-
end
|
|
1641
|
-
|
|
1642
|
-
# simplecov:disable
|
|
1643
|
-
# Edge case / untested delegation block
|
|
1644
|
-
if cell.formula
|
|
1645
|
-
f = cell.formula
|
|
1646
|
-
if f.is_a?(Elements::Formula)
|
|
1647
|
-
result[:formula] = f.expression
|
|
1648
|
-
result[:formula_type] = f.type if f.type
|
|
1649
|
-
result[:formula_ref] = f.ref if f.ref
|
|
1650
|
-
result[:formula_si] = f.shared_index if f.shared_index
|
|
1651
|
-
result[:formula_ca] = true if f.calculate_always
|
|
1652
|
-
unless f.cached_value.nil?
|
|
1653
|
-
# Cached value is written as-is (not through SST)
|
|
1654
|
-
result[:value] = f.cached_value
|
|
1655
|
-
case f.cached_value
|
|
1656
|
-
when true, false
|
|
1657
|
-
result[:type] = "b"
|
|
1658
|
-
when Elements::CellError
|
|
1659
|
-
result[:type] = "e"
|
|
1660
|
-
result[:value] = f.cached_value.code
|
|
1661
|
-
else
|
|
1662
|
-
result.delete(:type) # Ensure no type is set; cached values are plain text in <v>
|
|
1663
|
-
end
|
|
1664
|
-
# simplecov:enable
|
|
1665
|
-
end
|
|
1666
|
-
else
|
|
1667
|
-
# simplecov:disable
|
|
1668
|
-
# Edge case / untested delegation block
|
|
1669
|
-
result[:formula] = f
|
|
1670
|
-
# simplecov:enable
|
|
1671
|
-
end
|
|
1672
|
-
end
|
|
1673
|
-
# simplecov:disable
|
|
1674
|
-
# Edge case / untested delegation block
|
|
1675
|
-
result
|
|
1676
|
-
# simplecov:enable
|
|
1677
|
-
end
|
|
1678
|
-
|
|
1679
|
-
#: (Elements::Row row) -> Hash[Symbol, untyped]
|
|
1680
|
-
def build_row_attrs(row)
|
|
1681
|
-
# simplecov:disable
|
|
1682
|
-
# Edge case / untested delegation block
|
|
1683
|
-
attrs = {}
|
|
1684
|
-
attrs[:height] = row.height if row.height
|
|
1685
|
-
attrs[:hidden] = true if row.hidden
|
|
1686
|
-
attrs[:custom_height] = true if row.custom_height
|
|
1687
|
-
attrs[:collapsed] = true if row.collapsed
|
|
1688
|
-
attrs[:outline_level] = row.outline_level if row.outline_level
|
|
1689
|
-
attrs
|
|
1690
|
-
# simplecov:enable
|
|
1691
|
-
end
|
|
1692
|
-
|
|
1693
2972
|
#: (untyped source, String? password) -> untyped
|
|
1694
2973
|
def prepare_source_io(source, password)
|
|
1695
2974
|
if source.is_a?(String)
|
|
@@ -1881,8 +3160,8 @@ module Xlsxrb
|
|
|
1881
3160
|
max_row = rows.last.index
|
|
1882
3161
|
min_col = rows.map { |r| r.cells.first&.column_index }.compact.min || 0
|
|
1883
3162
|
max_col = rows.map { |r| r.cells.last&.column_index }.compact.max || 0
|
|
1884
|
-
min_col_letter = Elements::Cell.
|
|
1885
|
-
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)
|
|
1886
3165
|
dim_ref = if min_row == max_row && min_col == max_col
|
|
1887
3166
|
"#{min_col_letter}#{min_row + 1}"
|
|
1888
3167
|
else
|
|
@@ -2170,88 +3449,4 @@ module Xlsxrb
|
|
|
2170
3449
|
io.string
|
|
2171
3450
|
end
|
|
2172
3451
|
end
|
|
2173
|
-
|
|
2174
|
-
# Builds a raw cell hash from a value for streaming writes.
|
|
2175
|
-
#
|
|
2176
|
-
# @param row_index [Integer] 0-based row index.
|
|
2177
|
-
# @param col_index [Integer] 0-based column index.
|
|
2178
|
-
# @param value [Object] Cell value.
|
|
2179
|
-
# @param sst [Array<String>] Shared strings array.
|
|
2180
|
-
# @param sst_index [Hash{String => Integer}] Shared strings index mapping.
|
|
2181
|
-
# @return [Hash{Symbol => Object}] Raw cell hash.
|
|
2182
|
-
# @api public
|
|
2183
|
-
#: (Integer row_index, Integer col_index, untyped value, Array[String] sst, Hash[String, Integer] sst_index, ?date1904: bool) -> Hash[Symbol, untyped]
|
|
2184
|
-
def self.build_raw_cell_from_value(row_index, col_index, value, sst, sst_index, date1904: false)
|
|
2185
|
-
# simplecov:disable
|
|
2186
|
-
# Edge case / untested delegation block
|
|
2187
|
-
ref = "#{Elements::Cell.column_letter(col_index)}#{row_index + 1}"
|
|
2188
|
-
result = { ref: ref }
|
|
2189
|
-
|
|
2190
|
-
case value
|
|
2191
|
-
when Elements::Formula
|
|
2192
|
-
result[:formula] = value.expression
|
|
2193
|
-
result[:formula_type] = value.type if value.type
|
|
2194
|
-
result[:formula_ref] = value.ref if value.ref
|
|
2195
|
-
result[:formula_si] = value.shared_index if value.shared_index
|
|
2196
|
-
result[:formula_ca] = true if value.calculate_always
|
|
2197
|
-
unless value.cached_value.nil?
|
|
2198
|
-
cv = value.cached_value
|
|
2199
|
-
result[:value] = cv
|
|
2200
|
-
case cv
|
|
2201
|
-
when true, false
|
|
2202
|
-
result[:type] = "b"
|
|
2203
|
-
when Xlsxrb::Elements::CellError
|
|
2204
|
-
result[:type] = "e"
|
|
2205
|
-
result[:value] = cv.code
|
|
2206
|
-
end
|
|
2207
|
-
end
|
|
2208
|
-
when Hash
|
|
2209
|
-
if value.key?(:formula)
|
|
2210
|
-
result[:formula] = value[:formula]
|
|
2211
|
-
result[:formula_type] = value[:type] if value[:type]
|
|
2212
|
-
result[:formula_ref] = value[:ref] if value[:ref]
|
|
2213
|
-
si = value[:shared_index] || value[:si]
|
|
2214
|
-
result[:formula_si] = si if si
|
|
2215
|
-
result[:formula_ca] = true if value[:calculate_always]
|
|
2216
|
-
cv = value.key?(:value) ? value[:value] : value[:cached_value]
|
|
2217
|
-
unless cv.nil?
|
|
2218
|
-
result[:value] = cv
|
|
2219
|
-
case cv
|
|
2220
|
-
when true, false
|
|
2221
|
-
result[:type] = "b"
|
|
2222
|
-
when Xlsxrb::Elements::CellError
|
|
2223
|
-
result[:type] = "e"
|
|
2224
|
-
result[:value] = cv.code
|
|
2225
|
-
end
|
|
2226
|
-
end
|
|
2227
|
-
end
|
|
2228
|
-
when String, Xlsxrb::Elements::RichText
|
|
2229
|
-
idx = sst_index[value] ||= begin
|
|
2230
|
-
sst << value
|
|
2231
|
-
sst.size - 1
|
|
2232
|
-
end
|
|
2233
|
-
result[:value] = idx
|
|
2234
|
-
result[:type] = "s"
|
|
2235
|
-
when true, false
|
|
2236
|
-
result[:value] = value
|
|
2237
|
-
result[:type] = "b"
|
|
2238
|
-
when Integer, Float
|
|
2239
|
-
result[:value] = value
|
|
2240
|
-
when Xlsxrb::Elements::CellError
|
|
2241
|
-
result[:value] = value.code
|
|
2242
|
-
result[:type] = "e"
|
|
2243
|
-
when Date
|
|
2244
|
-
result[:value] = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: date1904)
|
|
2245
|
-
when Time
|
|
2246
|
-
result[:value] = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: date1904)
|
|
2247
|
-
# simplecov:enable
|
|
2248
|
-
when NilClass
|
|
2249
|
-
# empty cell
|
|
2250
|
-
end
|
|
2251
|
-
|
|
2252
|
-
# simplecov:disable
|
|
2253
|
-
# Edge case / untested delegation block
|
|
2254
|
-
result
|
|
2255
|
-
# simplecov:enable
|
|
2256
|
-
end
|
|
2257
3452
|
end
|