xlsxrb 0.1.18 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +200 -0
- data/README.md +35 -12
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
- data/lib/xlsxrb/chart_builder.rb +251 -386
- data/lib/xlsxrb/dsl_helpers.rb +169 -68
- data/lib/xlsxrb/elements/cell.rb +37 -157
- data/lib/xlsxrb/elements/column.rb +11 -19
- data/lib/xlsxrb/elements/coordinate_access.rb +15 -86
- data/lib/xlsxrb/elements/image.rb +5 -72
- data/lib/xlsxrb/elements/metadata.rb +463 -817
- data/lib/xlsxrb/elements/row.rb +27 -145
- data/lib/xlsxrb/elements/styles.rb +30 -88
- data/lib/xlsxrb/elements/types.rb +6 -99
- data/lib/xlsxrb/elements/workbook.rb +19 -126
- data/lib/xlsxrb/elements/worksheet.rb +45 -316
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -1
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -7
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +32 -17
- data/lib/xlsxrb/ooxml/reader.rb +49 -51
- data/lib/xlsxrb/ooxml/styles_parser.rb +26 -2
- data/lib/xlsxrb/ooxml/utils.rb +15 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +184 -94
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +353 -261
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +62 -16
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
- data/lib/xlsxrb/ooxml/writer.rb +106 -42
- data/lib/xlsxrb/ooxml/xml_builder.rb +18 -0
- data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
- data/lib/xlsxrb/ooxml/zip_reader.rb +65 -15
- data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
- data/lib/xlsxrb/stream_row.rb +14 -118
- data/lib/xlsxrb/stream_sheet.rb +14 -261
- data/lib/xlsxrb/stream_writer.rb +807 -1537
- data/lib/xlsxrb/style_builder.rb +715 -593
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +403 -286
- data/lib/xlsxrb/worksheet_builder.rb +1297 -1458
- data/lib/xlsxrb.rb +281 -1079
- data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
- data/sig/generated/xlsxrb/dsl_helpers.rbs +92 -16
- data/sig/generated/xlsxrb/elements/cell.rbs +12 -76
- data/sig/generated/xlsxrb/elements/column.rbs +0 -7
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +5 -41
- data/sig/generated/xlsxrb/elements/image.rbs +3 -29
- data/sig/generated/xlsxrb/elements/metadata.rbs +138 -151
- data/sig/generated/xlsxrb/elements/row.rbs +10 -53
- data/sig/generated/xlsxrb/elements/styles.rbs +1 -39
- data/sig/generated/xlsxrb/elements/workbook.rbs +14 -78
- data/sig/generated/xlsxrb/elements/worksheet.rbs +5 -121
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +0 -4
- data/sig/generated/xlsxrb/ooxml/reader.rbs +0 -3
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/utils.rbs +13 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +17 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +58 -3
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +12 -0
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +22 -7
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
- data/sig/generated/xlsxrb/stream_row.rbs +9 -44
- data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
- data/sig/generated/xlsxrb/stream_writer.rbs +491 -974
- data/sig/generated/xlsxrb/style_builder.rbs +464 -239
- data/sig/generated/xlsxrb/workbook_builder.rbs +124 -139
- data/sig/generated/xlsxrb/worksheet_builder.rbs +922 -686
- data/sig/generated/xlsxrb.rbs +56 -188
- data/sig/rexml.rbs +8 -0
- metadata +3 -5
- data/lib/xlsxrb/utils.rb +0 -251
- data/sig/generated/xlsxrb/utils.rbs +0 -200
|
@@ -13,21 +13,24 @@ module Xlsxrb
|
|
|
13
13
|
# sheet.row(["Product", "Revenue"])
|
|
14
14
|
# sheet.row(["Widget", 1000])
|
|
15
15
|
# end
|
|
16
|
-
# builder.
|
|
16
|
+
# builder.core_properties(creator: "Reporting System")
|
|
17
17
|
# end
|
|
18
18
|
#
|
|
19
19
|
# @api public
|
|
20
20
|
class WorkbookBuilder
|
|
21
|
+
OMITTED = Object.new.freeze
|
|
22
|
+
private_constant :OMITTED
|
|
23
|
+
|
|
21
24
|
# @param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specifications.
|
|
22
|
-
# @param escape_formulas [Boolean] Whether to treat strings starting with formula prefixes as plain text.
|
|
23
25
|
# @param default_font [Hash, nil] Optional default font options (e.g. `{ name: "Arial", size: 12 }`).
|
|
24
|
-
#: (?strict_excel_mode: bool, ?
|
|
25
|
-
def initialize(strict_excel_mode: true,
|
|
26
|
+
#: (?strict_excel_mode: bool, ?default_font: untyped?) -> void
|
|
27
|
+
def initialize(strict_excel_mode: true, default_font: nil)
|
|
26
28
|
@strict_excel_mode = strict_excel_mode
|
|
27
|
-
@escape_formulas = escape_formulas ? true : false
|
|
28
29
|
@default_font = default_font
|
|
29
|
-
@sheets = []
|
|
30
|
+
@sheets = [] # Each sheet without its rows, for its name and state
|
|
31
|
+
@sheet_snapshots = [] # Each sheet as its block left it, rows still pending
|
|
30
32
|
@sheet_builders = [] # Keep track of sheet builders for style processing
|
|
33
|
+
@style_log = StyleLog.new # When each style is defined, for the rows that use it
|
|
31
34
|
@defined_names = []
|
|
32
35
|
@styles = {}
|
|
33
36
|
@core_properties = {}
|
|
@@ -41,19 +44,6 @@ module Xlsxrb
|
|
|
41
44
|
@tab_ratio = nil
|
|
42
45
|
end
|
|
43
46
|
|
|
44
|
-
# @return [Boolean] Whether to treat strings starting with formula prefixes as plain text.
|
|
45
|
-
#: bool
|
|
46
|
-
attr_accessor :escape_formulas
|
|
47
|
-
|
|
48
|
-
# Returns whether formula escaping is enabled.
|
|
49
|
-
#
|
|
50
|
-
# @return [Boolean]
|
|
51
|
-
# @api public
|
|
52
|
-
#: () -> bool
|
|
53
|
-
def escape_formulas?
|
|
54
|
-
@escape_formulas ? true : false
|
|
55
|
-
end
|
|
56
|
-
|
|
57
47
|
# Sets or configures the workbook-wide default font.
|
|
58
48
|
#
|
|
59
49
|
# @param name [String, Hash, nil] Font name or font options Hash.
|
|
@@ -72,8 +62,6 @@ module Xlsxrb
|
|
|
72
62
|
existing[:size] = size if size
|
|
73
63
|
@default_font = existing.merge(opts)
|
|
74
64
|
end
|
|
75
|
-
# @deprecated Use {#default_font} instead. Will be removed in 0.2.0.
|
|
76
|
-
alias set_default_font default_font
|
|
77
65
|
|
|
78
66
|
# @return [Hash{String => StyleBuilder}]
|
|
79
67
|
#: Hash[String, StyleBuilder]
|
|
@@ -89,6 +77,8 @@ module Xlsxrb
|
|
|
89
77
|
# @yield [style_builder]
|
|
90
78
|
# @yieldparam style_builder [Xlsxrb::StyleBuilder]
|
|
91
79
|
# @return [StyleBuilder]
|
|
80
|
+
# Using the returned value is deprecated and warns; in version 0.3.0, this method
|
|
81
|
+
# will return nil. Configure the style with options or in the block.
|
|
92
82
|
# @api public
|
|
93
83
|
#: (String | Symbol name, **untyped opts) ?{ (StyleBuilder) -> void } -> StyleBuilder
|
|
94
84
|
def style(name, **opts)
|
|
@@ -97,6 +87,8 @@ module Xlsxrb
|
|
|
97
87
|
style_builder.apply_options!(**opts) unless opts.empty?
|
|
98
88
|
yield style_builder if block_given?
|
|
99
89
|
@styles[style_name] = style_builder
|
|
90
|
+
@style_log.record(style_name, style_builder)
|
|
91
|
+
ReturnedStyleBuilder.new(style_builder)
|
|
100
92
|
end
|
|
101
93
|
|
|
102
94
|
# Creates an {Elements::Formula} object.
|
|
@@ -158,114 +150,51 @@ module Xlsxrb
|
|
|
158
150
|
Xlsxrb.cell_error(...)
|
|
159
151
|
end
|
|
160
152
|
|
|
161
|
-
OMITTED = Object.new.freeze
|
|
162
|
-
private_constant :OMITTED
|
|
163
|
-
|
|
164
153
|
# Gets or sets the active worksheet tab in the workbook window.
|
|
165
154
|
#
|
|
166
|
-
# @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#active_sheet=} instead.
|
|
167
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
168
|
-
# @return [String, Symbol, Integer, nil]
|
|
169
|
-
# @api public
|
|
170
|
-
#: (?String | Symbol | Integer | nil name_or_index) -> (String | Symbol | Integer | nil)
|
|
171
|
-
def active_sheet(name_or_index = OMITTED)
|
|
172
|
-
if name_or_index.equal?(OMITTED)
|
|
173
|
-
@active_sheet
|
|
174
|
-
else
|
|
175
|
-
@active_sheet = name_or_index
|
|
176
|
-
end
|
|
177
|
-
end
|
|
178
|
-
# @deprecated Use {#active_sheet} instead. Will be removed in 0.2.0.
|
|
179
|
-
alias active_tab active_sheet
|
|
180
|
-
|
|
181
|
-
# Sets the active worksheet tab in the workbook window.
|
|
182
|
-
#
|
|
183
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
184
155
|
# @return [String, Symbol, Integer, nil]
|
|
185
156
|
# @api public
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
alias active_tab= active_sheet=
|
|
157
|
+
#: (String | Symbol | Integer | nil)
|
|
158
|
+
attr_accessor :active_sheet
|
|
189
159
|
|
|
190
160
|
# Gets or sets the first sheet tab visible in the sheet tab scroll area.
|
|
191
161
|
#
|
|
192
|
-
# @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#first_sheet=} instead.
|
|
193
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
194
162
|
# @return [String, Symbol, Integer, nil]
|
|
195
163
|
# @api public
|
|
196
|
-
#: (
|
|
197
|
-
|
|
198
|
-
if name_or_index.equal?(OMITTED)
|
|
199
|
-
@first_sheet
|
|
200
|
-
else
|
|
201
|
-
@first_sheet = name_or_index
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
# Sets the first sheet tab visible in the sheet tab scroll area.
|
|
206
|
-
#
|
|
207
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
208
|
-
# @return [String, Symbol, Integer, nil]
|
|
209
|
-
# @api public
|
|
210
|
-
attr_writer :first_sheet #: (String | Symbol | Integer | nil)
|
|
211
|
-
|
|
212
|
-
# Gets or sets whether sheet tabs are visible in the workbook window.
|
|
213
|
-
#
|
|
214
|
-
# @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#show_sheet_tabs=} instead.
|
|
215
|
-
# @param visible [Boolean, nil]
|
|
216
|
-
# @return [Boolean, nil]
|
|
217
|
-
# @api public
|
|
218
|
-
#: (?bool? visible) -> bool?
|
|
219
|
-
def show_sheet_tabs(visible = OMITTED)
|
|
220
|
-
if visible.equal?(OMITTED)
|
|
221
|
-
@show_sheet_tabs
|
|
222
|
-
else
|
|
223
|
-
@show_sheet_tabs = visible
|
|
224
|
-
end
|
|
225
|
-
end
|
|
164
|
+
#: (String | Symbol | Integer | nil)
|
|
165
|
+
attr_accessor :first_sheet
|
|
226
166
|
|
|
227
167
|
# Sets whether sheet tabs are visible in the workbook window.
|
|
228
168
|
#
|
|
229
169
|
# @param visible [Boolean, nil]
|
|
230
170
|
# @return [Boolean, nil]
|
|
231
171
|
# @api public
|
|
232
|
-
|
|
172
|
+
#: (bool? visible) -> bool?
|
|
173
|
+
attr_writer :show_sheet_tabs
|
|
233
174
|
|
|
234
|
-
#
|
|
175
|
+
# Returns whether sheet tabs are visible in the workbook window.
|
|
235
176
|
#
|
|
236
|
-
# @
|
|
237
|
-
# @param ratio [Integer, nil]
|
|
238
|
-
# @return [Integer, nil]
|
|
177
|
+
# @return [Boolean]
|
|
239
178
|
# @api public
|
|
240
|
-
#: (
|
|
241
|
-
def
|
|
242
|
-
|
|
243
|
-
@tab_ratio
|
|
244
|
-
else
|
|
245
|
-
@tab_ratio = ratio
|
|
246
|
-
end
|
|
179
|
+
#: () -> bool
|
|
180
|
+
def show_sheet_tabs?
|
|
181
|
+
@show_sheet_tabs != false
|
|
247
182
|
end
|
|
248
183
|
|
|
249
|
-
#
|
|
184
|
+
# Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
|
|
250
185
|
#
|
|
251
|
-
# @param ratio [Integer, nil]
|
|
252
186
|
# @return [Integer, nil]
|
|
253
187
|
# @api public
|
|
254
|
-
|
|
188
|
+
#: Integer?
|
|
189
|
+
attr_accessor :tab_ratio
|
|
255
190
|
|
|
256
|
-
# Gets
|
|
191
|
+
# Gets the internal code name of the workbook.
|
|
257
192
|
#
|
|
258
|
-
# @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#code_name=} instead.
|
|
259
|
-
# @param name [String, Symbol, nil] The code name.
|
|
260
193
|
# @return [String, nil]
|
|
261
194
|
# @api public
|
|
262
|
-
#: (
|
|
263
|
-
def code_name
|
|
264
|
-
|
|
265
|
-
@workbook_properties[:code_name]
|
|
266
|
-
else
|
|
267
|
-
@workbook_properties[:code_name] = name&.to_s
|
|
268
|
-
end
|
|
195
|
+
#: () -> String?
|
|
196
|
+
def code_name
|
|
197
|
+
@workbook_properties[:code_name]
|
|
269
198
|
end
|
|
270
199
|
|
|
271
200
|
# Sets the internal code name of the workbook.
|
|
@@ -278,29 +207,25 @@ module Xlsxrb
|
|
|
278
207
|
@workbook_properties[:code_name] = name&.to_s
|
|
279
208
|
end
|
|
280
209
|
|
|
281
|
-
#
|
|
210
|
+
# Configures or queries workbook-level properties.
|
|
211
|
+
#
|
|
212
|
+
# When called with no arguments, returns the {Elements::WorkbookProperties} value object.
|
|
213
|
+
# When called with keyword options, configures properties and returns nil.
|
|
282
214
|
#
|
|
283
215
|
# @note **SECURITY WARNING:** If you set `:update_links` to anything other than `"never"`,
|
|
284
216
|
# you may expose end-users to malicious external reference vulnerabilities (e.g., CSV/DDE Injection)
|
|
285
217
|
# when they open the generated Excel file. Ensure you fully trust the exported data.
|
|
286
218
|
#
|
|
287
|
-
# @param
|
|
288
|
-
# @
|
|
289
|
-
# @return [void]
|
|
290
|
-
# @api public
|
|
291
|
-
#: (Symbol name, String | Integer | bool value) -> void
|
|
292
|
-
def workbook_property(name, value)
|
|
293
|
-
@workbook_properties[name] = value
|
|
294
|
-
nil
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
# Returns workbook-level properties.
|
|
298
|
-
#
|
|
299
|
-
# @return [Elements::WorkbookProperties]
|
|
219
|
+
# @param opts [Hash] Workbook properties map.
|
|
220
|
+
# @return [Elements::WorkbookProperties, void]
|
|
300
221
|
# @api public
|
|
301
222
|
#: () -> Elements::WorkbookProperties
|
|
302
|
-
|
|
303
|
-
|
|
223
|
+
#: (**untyped opts) -> void
|
|
224
|
+
def workbook_properties(**opts)
|
|
225
|
+
return Elements::WorkbookProperties.new(**@workbook_properties) if opts.empty?
|
|
226
|
+
|
|
227
|
+
@workbook_properties.merge!(opts)
|
|
228
|
+
nil
|
|
304
229
|
end
|
|
305
230
|
|
|
306
231
|
# Sets workbook-level properties.
|
|
@@ -310,7 +235,9 @@ module Xlsxrb
|
|
|
310
235
|
# @api public
|
|
311
236
|
#: (untyped props) -> void
|
|
312
237
|
def workbook_properties=(props)
|
|
313
|
-
|
|
238
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
239
|
+
@workbook_properties = h.transform_keys(&:to_sym)
|
|
240
|
+
nil
|
|
314
241
|
end
|
|
315
242
|
|
|
316
243
|
# Returns the list of worksheet names added to this workbook so far.
|
|
@@ -326,20 +253,20 @@ module Xlsxrb
|
|
|
326
253
|
#
|
|
327
254
|
# @param name [String, nil] Sheet name (max 31 chars, no forbidden chars `[ ] * ? / \ :`).
|
|
328
255
|
# @param state [Symbol, String, nil] Sheet visibility state (:visible, :hidden, :very_hidden).
|
|
329
|
-
# @param tab_color [String, Hash, nil] Tab color hex string or
|
|
330
|
-
# @param code_name [String, nil] VBA code name.
|
|
331
|
-
# @param hidden [Boolean, nil] Deprecated alias for state: :hidden.
|
|
256
|
+
# @param tab_color [String, Hash, Symbol, nil] Tab color hex string (e.g. "FF0000") or hash options.
|
|
257
|
+
# @param code_name [String, Symbol, nil] Internal VBA code name of the sheet.
|
|
332
258
|
# @param opts [Hash] Additional sheet properties.
|
|
333
259
|
# @yield [sheet_builder]
|
|
334
260
|
# @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder]
|
|
335
|
-
# @return [
|
|
261
|
+
# @return [DeferredWorksheet] The sheet as {WorksheetBuilder#build} returns it, with
|
|
262
|
+
# style names in its cells. It is built when first used.
|
|
336
263
|
# @raise [ArgumentError] If sheet name violates Excel limits in strict mode.
|
|
337
264
|
# @api public
|
|
338
|
-
#: (?String? name, ?state:
|
|
339
|
-
def sheet(name = nil, state: nil, tab_color: nil, code_name: nil,
|
|
265
|
+
#: (?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) -> void } -> DeferredWorksheet
|
|
266
|
+
def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts)
|
|
340
267
|
sheet_count = [@sheets.size, @sheet_builders.size].max
|
|
341
268
|
name ||= "Sheet#{sheet_count + 1}"
|
|
342
|
-
raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name
|
|
269
|
+
raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters, counted in UTF-16 code units as Excel does (Excel limitation)" if @strict_excel_mode && Ooxml::Utils.utf16_length(name) > 31
|
|
343
270
|
raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars)
|
|
344
271
|
raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'"))
|
|
345
272
|
raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History")
|
|
@@ -349,11 +276,9 @@ module Xlsxrb
|
|
|
349
276
|
|
|
350
277
|
active = opts.delete(:active)
|
|
351
278
|
state = opts.delete(:state) if opts.key?(:state)
|
|
352
|
-
hidden = opts.delete(:hidden)
|
|
279
|
+
hidden = opts.delete(:hidden)
|
|
353
280
|
code_name = opts.delete(:code_name) if opts.key?(:code_name)
|
|
354
281
|
tab_color = opts.delete(:tab_color) if opts.key?(:tab_color)
|
|
355
|
-
|
|
356
|
-
warn "[Xlsxrb DEPRECATION] sheet keyword hidden: is deprecated; use state: :hidden instead.", uplevel: 1 unless hidden.nil?
|
|
357
282
|
state ||= :hidden if hidden
|
|
358
283
|
state ||= :visible
|
|
359
284
|
state = state.to_sym
|
|
@@ -361,30 +286,47 @@ module Xlsxrb
|
|
|
361
286
|
|
|
362
287
|
@active_sheet = name if active
|
|
363
288
|
|
|
364
|
-
sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state,
|
|
289
|
+
sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state, style_log: @style_log)
|
|
365
290
|
sheet_builder.code_name = code_name if code_name
|
|
366
291
|
sheet_builder.tab_color = tab_color if tab_color
|
|
367
|
-
|
|
292
|
+
sheet_builder.sheet_properties(**opts) unless opts.empty?
|
|
368
293
|
@sheet_builders << sheet_builder
|
|
369
294
|
yield sheet_builder if block_given?
|
|
370
295
|
@defined_names.concat(sheet_builder.defined_names)
|
|
371
296
|
sheet_builder.defined_names.clear
|
|
372
|
-
|
|
373
|
-
@
|
|
374
|
-
|
|
297
|
+
snapshot = sheet_builder.close
|
|
298
|
+
@sheet_snapshots << snapshot
|
|
299
|
+
@sheets << snapshot.frame
|
|
300
|
+
DeferredWorksheet.new(snapshot)
|
|
375
301
|
end
|
|
376
302
|
|
|
377
|
-
#
|
|
303
|
+
# Adds a worksheet, as {#sheet} does.
|
|
304
|
+
#
|
|
305
|
+
# @deprecated Use {#sheet} to add a sheet. In version 0.3.0, this method will return the
|
|
306
|
+
# {WorksheetBuilder} of an existing sheet, or of the current sheet without an argument,
|
|
307
|
+
# and raise for a sheet that does not exist. Calling it with an argument, or without
|
|
308
|
+
# one after a sheet was added, warns.
|
|
378
309
|
#
|
|
379
310
|
# @param name_or_index [String, Symbol, Integer, nil]
|
|
380
311
|
# @yield [sheet_builder]
|
|
381
|
-
# @return [
|
|
312
|
+
# @return [DeferredWorksheet]
|
|
382
313
|
# @api public
|
|
383
|
-
#: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } ->
|
|
314
|
+
#: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } -> DeferredWorksheet
|
|
384
315
|
def [](name_or_index = nil, &)
|
|
316
|
+
if !name_or_index.nil?
|
|
317
|
+
Deprecation.warn(INDEX_CREATES_SHEET_MESSAGE)
|
|
318
|
+
elsif !@sheet_builders.empty?
|
|
319
|
+
Deprecation.warn(INDEX_ADDS_SHEET_MESSAGE)
|
|
320
|
+
end
|
|
385
321
|
sheet(name_or_index&.to_s, &)
|
|
386
322
|
end
|
|
387
323
|
|
|
324
|
+
INDEX_CREATES_SHEET_MESSAGE = "Creating a sheet with WorkbookBuilder#[] is deprecated; use sheet. In version 0.3.0, " \
|
|
325
|
+
"#[] will return the WorksheetBuilder of an existing sheet and raise for a sheet that does not exist."
|
|
326
|
+
INDEX_ADDS_SHEET_MESSAGE = "WorkbookBuilder#[] without an argument adds a sheet; in version 0.3.0 it will return " \
|
|
327
|
+
"the current sheet instead. Use sheet to add a sheet."
|
|
328
|
+
private_constant :INDEX_CREATES_SHEET_MESSAGE, :INDEX_ADDS_SHEET_MESSAGE
|
|
329
|
+
|
|
388
330
|
# --- Workbook-Level Methods ---
|
|
389
331
|
|
|
390
332
|
# Adds a defined name (named range or formula) to the workbook.
|
|
@@ -395,32 +337,17 @@ module Xlsxrb
|
|
|
395
337
|
# @param sheet [String, nil] Reference sheet name for coordinates/formula.
|
|
396
338
|
# @param scope [String, Symbol, nil] Optional sheet name or :workbook for scope (defaults to workbook).
|
|
397
339
|
# @param hidden [Boolean] Whether the defined name is hidden from Excel's UI.
|
|
398
|
-
# @param column [Integer, String, nil] Deprecated alias for columns.
|
|
399
340
|
# @param columns [Integer, String, nil] Column index or letter for coordinate-based reference.
|
|
400
|
-
# @param
|
|
401
|
-
# @param row [Integer, Range, nil] Deprecated alias for rows.
|
|
402
|
-
# @param rows [Integer, Range, nil] Row or row range (1-based) for coordinate-based reference.
|
|
341
|
+
# @param rows [Integer, Range, nil] Row or row range (0-based) for coordinate-based reference.
|
|
403
342
|
# @param range [String, nil] Cell range reference.
|
|
404
|
-
# @param ref_sheet [String, nil] Deprecated alias for sheet.
|
|
405
|
-
# @param local [Boolean] Deprecated alias for scope.
|
|
406
343
|
# @return [void]
|
|
407
344
|
# @api public
|
|
408
|
-
#: (String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?
|
|
409
|
-
def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false,
|
|
410
|
-
|
|
411
|
-
warn "[DEPRECATION] `ref_sheet:` is deprecated; use `sheet:` for reference sheet and `scope:` for scope instead.", uplevel: 1 if ref_sheet
|
|
412
|
-
warn "[DEPRECATION] `local:` is deprecated; use `scope:` instead.", uplevel: 1 if local
|
|
413
|
-
warn "[Xlsxrb DEPRECATION] defined_name keyword column: is deprecated; use columns: instead.", uplevel: 1 unless column.nil?
|
|
414
|
-
warn "[Xlsxrb DEPRECATION] defined_name keyword row: is deprecated; use rows: instead.", uplevel: 1 unless row.nil?
|
|
415
|
-
target_ref_sheet = ref_sheet || sheet
|
|
416
|
-
c_arg = column || columns || col
|
|
417
|
-
r_arg = row || rows
|
|
418
|
-
normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: target_ref_sheet, col: c_arg, rows: r_arg, range: range)
|
|
345
|
+
#: (String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?columns: untyped, ?rows: untyped, ?range: untyped) -> void
|
|
346
|
+
def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false, columns: nil, rows: nil, range: nil)
|
|
347
|
+
normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: sheet, columns: columns, rows: rows, range: range)
|
|
419
348
|
entry = { name: name, value: normalized_val, hidden: hidden }
|
|
420
349
|
local_sheet = if scope
|
|
421
350
|
scope == :workbook ? nil : scope.to_s
|
|
422
|
-
elsif sheet || local || ref_sheet
|
|
423
|
-
sheet
|
|
424
351
|
end
|
|
425
352
|
entry[:local_sheet_name] = local_sheet if local_sheet
|
|
426
353
|
@defined_names << entry
|
|
@@ -435,53 +362,54 @@ module Xlsxrb
|
|
|
435
362
|
@defined_names.map { |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) }
|
|
436
363
|
end
|
|
437
364
|
|
|
438
|
-
#
|
|
365
|
+
# Returns the print area range for the sheet, or nil if none.
|
|
439
366
|
#
|
|
440
|
-
# @param range [String, Object, nil] Cell range (e.g. "A1:G50").
|
|
441
367
|
# @param sheet [String, nil] Target sheet name (defaults to latest or "Sheet1").
|
|
442
|
-
# @return [String, nil
|
|
368
|
+
# @return [String, nil]
|
|
443
369
|
# @api public
|
|
444
|
-
#: (?
|
|
445
|
-
def print_area(
|
|
370
|
+
#: (?sheet: String?) -> String?
|
|
371
|
+
def print_area(sheet: nil)
|
|
446
372
|
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
return dn ? dn[:value].split("!", 2).last.tr("$", "") : nil
|
|
450
|
-
end
|
|
451
|
-
|
|
452
|
-
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
|
|
453
|
-
if range
|
|
454
|
-
value = "'#{sheet_name}'!#{absolute_range(range)}"
|
|
455
|
-
defined_name("_xlnm.Print_Area", value, sheet: sheet_name)
|
|
456
|
-
end
|
|
457
|
-
nil
|
|
373
|
+
dn = @defined_names.find { |d| d[:name] == "_xlnm.Print_Area" && d[:local_sheet_name] == sheet_name }
|
|
374
|
+
dn ? dn[:value].split("!", 2).last.tr("$", "") : nil
|
|
458
375
|
end
|
|
459
376
|
|
|
460
|
-
# Sets the print area for the worksheet.
|
|
377
|
+
# Sets the print area for the current worksheet.
|
|
461
378
|
#
|
|
462
379
|
# @param range [String, nil] Cell range (e.g. "A1:G50").
|
|
463
380
|
# @return [String, nil]
|
|
464
381
|
# @api public
|
|
465
382
|
#: (String? range) -> String?
|
|
466
383
|
def print_area=(range)
|
|
467
|
-
|
|
468
|
-
range
|
|
384
|
+
sheet_name = @sheets.last&.name || "Sheet1"
|
|
385
|
+
set_print_area(range, sheet: sheet_name)
|
|
469
386
|
end
|
|
470
387
|
|
|
471
|
-
# Sets repeating print titles (rows and/or columns) for pagination.
|
|
388
|
+
# Sets or gets repeating print titles (rows and/or columns) for pagination.
|
|
389
|
+
#
|
|
390
|
+
# When called with no row/column arguments, returns the {Elements::PrintTitles} value object or nil.
|
|
391
|
+
# When called with row/column arguments, sets repeating print titles and returns nil.
|
|
472
392
|
#
|
|
473
393
|
# @param rows [String, nil] Repeating row range (e.g. "1:2").
|
|
474
394
|
# @param columns [String, nil] Repeating column range (e.g. "A:B").
|
|
475
|
-
# @param cols [String, nil] Deprecated alias for columns.
|
|
476
395
|
# @param sheet [String, nil] Target sheet name.
|
|
477
|
-
# @return [void]
|
|
396
|
+
# @return [Elements::PrintTitles, nil, void]
|
|
478
397
|
# @api public
|
|
479
|
-
#: (
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
c = DslHelpers.normalize_print_titles_columns(columns || cols)
|
|
483
|
-
r = DslHelpers.normalize_print_titles_rows(rows)
|
|
398
|
+
#: () -> Elements::PrintTitles?
|
|
399
|
+
#: (?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?, ?sheet: String?) -> void
|
|
400
|
+
def print_titles(rows: nil, columns: nil, sheet: nil)
|
|
484
401
|
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
402
|
+
if rows.nil? && columns.nil?
|
|
403
|
+
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
404
|
+
return nil unless existing
|
|
405
|
+
|
|
406
|
+
c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
407
|
+
r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
|
|
408
|
+
return Elements::PrintTitles.new(rows: r, columns: c)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
c = DslHelpers.normalize_print_titles_columns(columns)
|
|
412
|
+
r = DslHelpers.normalize_print_titles_rows(rows)
|
|
485
413
|
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
486
414
|
if existing && (r.nil? || c.nil?)
|
|
487
415
|
c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
@@ -492,19 +420,24 @@ module Xlsxrb
|
|
|
492
420
|
parts << "'#{sheet_name}'!$#{r.sub(":", ":$")}" if r
|
|
493
421
|
value = parts.join(",")
|
|
494
422
|
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
495
|
-
defined_name("_xlnm.Print_Titles", value, sheet: sheet_name)
|
|
423
|
+
defined_name("_xlnm.Print_Titles", value, sheet: sheet_name, scope: sheet_name)
|
|
496
424
|
nil
|
|
497
425
|
end
|
|
498
426
|
|
|
499
427
|
# Sets repeating print titles.
|
|
500
428
|
#
|
|
501
|
-
# @param opts [Elements::PrintTitles, Hash] Options with :rows and/or :columns.
|
|
502
|
-
# @return [Elements::PrintTitles, Hash]
|
|
429
|
+
# @param opts [Elements::PrintTitles, Hash, nil] Options with :rows and/or :columns.
|
|
430
|
+
# @return [Elements::PrintTitles, Hash, nil]
|
|
503
431
|
# @api public
|
|
504
|
-
#: (Elements::PrintTitles | Hash[Symbol, untyped] opts) -> (Elements::PrintTitles | Hash[Symbol, untyped])
|
|
432
|
+
#: (Elements::PrintTitles | Hash[Symbol, untyped] | nil opts) -> (Elements::PrintTitles | Hash[Symbol, untyped] | nil)
|
|
505
433
|
def print_titles=(opts)
|
|
506
|
-
|
|
507
|
-
|
|
434
|
+
if opts.nil?
|
|
435
|
+
sheet_name = @sheets.last&.name || "Sheet1"
|
|
436
|
+
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
437
|
+
else
|
|
438
|
+
h = opts.respond_to?(:to_h) ? opts.to_h : opts
|
|
439
|
+
print_titles(**h)
|
|
440
|
+
end
|
|
508
441
|
opts
|
|
509
442
|
end
|
|
510
443
|
|
|
@@ -567,7 +500,7 @@ module Xlsxrb
|
|
|
567
500
|
if opts.empty?
|
|
568
501
|
@workbook_protection ? Elements::WorkbookProtection.new(**@workbook_protection) : nil
|
|
569
502
|
else
|
|
570
|
-
@workbook_protection = opts
|
|
503
|
+
@workbook_protection = DslHelpers.normalize_workbook_protection(opts)
|
|
571
504
|
nil
|
|
572
505
|
end
|
|
573
506
|
end
|
|
@@ -579,54 +512,69 @@ module Xlsxrb
|
|
|
579
512
|
# @api public
|
|
580
513
|
#: (untyped opts) -> void
|
|
581
514
|
def workbook_protection=(opts)
|
|
582
|
-
@workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : opts
|
|
515
|
+
@workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : (opts && DslHelpers.normalize_workbook_protection(opts))
|
|
583
516
|
end
|
|
584
517
|
|
|
585
|
-
#
|
|
586
|
-
#: (**String | Integer | bool | nil opts) -> void
|
|
587
|
-
def protect_workbook(...)
|
|
588
|
-
warn "[DEPRECATION] `protect_workbook` is deprecated; use `workbook_protection` instead.", uplevel: 1
|
|
589
|
-
workbook_protection(...)
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
# Sets a Dublin Core document metadata property.
|
|
518
|
+
# Configures or queries Dublin Core document metadata properties.
|
|
593
519
|
#
|
|
594
|
-
#
|
|
595
|
-
#
|
|
596
|
-
# @return [void]
|
|
597
|
-
# @api public
|
|
598
|
-
#: (Symbol name, String | Integer | Time value) -> void
|
|
599
|
-
def core_property(name, value)
|
|
600
|
-
@core_properties[name] = value
|
|
601
|
-
end
|
|
602
|
-
|
|
603
|
-
# Returns core document properties.
|
|
520
|
+
# When called with no arguments, returns the {Elements::CoreProperties} value object.
|
|
521
|
+
# When called with keyword options, configures properties and returns nil.
|
|
604
522
|
#
|
|
605
|
-
# @
|
|
523
|
+
# @param opts [Hash] Core properties map (:creator, :title, :subject, :description, etc.).
|
|
524
|
+
# @return [Elements::CoreProperties, void]
|
|
606
525
|
# @api public
|
|
607
526
|
#: () -> Elements::CoreProperties
|
|
608
|
-
|
|
609
|
-
|
|
527
|
+
#: (**untyped opts) -> void
|
|
528
|
+
def core_properties(**opts)
|
|
529
|
+
return Elements::CoreProperties.new(**@core_properties) if opts.empty?
|
|
530
|
+
|
|
531
|
+
@core_properties.merge!(opts)
|
|
532
|
+
nil
|
|
610
533
|
end
|
|
611
534
|
|
|
612
|
-
#
|
|
535
|
+
# Replaces core document properties.
|
|
613
536
|
#
|
|
614
|
-
# @param
|
|
615
|
-
# @param value [String, Integer, Time] Property value.
|
|
537
|
+
# @param props [Hash, Elements::CoreProperties]
|
|
616
538
|
# @return [void]
|
|
617
539
|
# @api public
|
|
618
|
-
#: (Symbol
|
|
619
|
-
def
|
|
620
|
-
|
|
540
|
+
#: (Hash[Symbol, untyped] | Elements::CoreProperties props) -> void
|
|
541
|
+
def core_properties=(props)
|
|
542
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
543
|
+
@core_properties = h.transform_keys(&:to_sym)
|
|
544
|
+
nil
|
|
621
545
|
end
|
|
622
546
|
|
|
623
|
-
#
|
|
547
|
+
# Configures or queries extended application metadata properties.
|
|
548
|
+
#
|
|
549
|
+
# When called with no arguments, returns the {Elements::AppProperties} value object.
|
|
550
|
+
# When called with keyword options, configures properties and returns nil.
|
|
624
551
|
#
|
|
625
|
-
# @
|
|
552
|
+
# @param opts [Hash] Application properties map (:company, :manager, :app_version, etc.).
|
|
553
|
+
# @return [Elements::AppProperties, void]
|
|
626
554
|
# @api public
|
|
627
555
|
#: () -> Elements::AppProperties
|
|
628
|
-
|
|
629
|
-
|
|
556
|
+
#: (**untyped opts) -> void
|
|
557
|
+
def app_properties(**opts)
|
|
558
|
+
return Elements::AppProperties.new(**@app_properties) if opts.empty?
|
|
559
|
+
|
|
560
|
+
opts.each do |k, v|
|
|
561
|
+
norm_name = k.to_sym == :application ? :app_name : k.to_sym
|
|
562
|
+
@app_properties[norm_name] = v
|
|
563
|
+
end
|
|
564
|
+
nil
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
# Replaces application document properties.
|
|
568
|
+
#
|
|
569
|
+
# @param props [Hash, Elements::AppProperties]
|
|
570
|
+
# @return [void]
|
|
571
|
+
# @api public
|
|
572
|
+
#: (Hash[Symbol, untyped] | Elements::AppProperties props) -> void
|
|
573
|
+
def app_properties=(props)
|
|
574
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
575
|
+
@app_properties = {}
|
|
576
|
+
app_properties(**h)
|
|
577
|
+
nil
|
|
630
578
|
end
|
|
631
579
|
|
|
632
580
|
# Sets multiple core, app, and/or custom properties simultaneously.
|
|
@@ -638,8 +586,8 @@ module Xlsxrb
|
|
|
638
586
|
# @api public
|
|
639
587
|
#: (?core: Hash[Symbol, String | Integer | Time]?, ?app: Hash[Symbol, String | Integer | Time]?, ?custom: Hash[String | Symbol, untyped]?) -> void
|
|
640
588
|
def properties(core: nil, app: nil, custom: nil)
|
|
641
|
-
core
|
|
642
|
-
app
|
|
589
|
+
core_properties(**core) if core
|
|
590
|
+
app_properties(**app) if app
|
|
643
591
|
custom&.each { |k, v| custom_property(k.to_s, v) }
|
|
644
592
|
end
|
|
645
593
|
|
|
@@ -680,12 +628,12 @@ module Xlsxrb
|
|
|
680
628
|
end
|
|
681
629
|
|
|
682
630
|
# Process styles from all sheets and collect style definitions
|
|
683
|
-
processed_sheets, styles_definition = process_styles(@
|
|
631
|
+
processed_sheets, styles_definition = process_styles(@sheet_snapshots)
|
|
684
632
|
|
|
685
633
|
# Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined
|
|
686
634
|
@sheet_builders.each do |sb|
|
|
687
|
-
|
|
688
|
-
print_titles(rows: sb.print_titles
|
|
635
|
+
set_print_area(sb.print_area, sheet: sb.name) if sb.print_area && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sb.name }
|
|
636
|
+
print_titles(rows: sb.print_titles.rows, columns: sb.print_titles.columns, sheet: sb.name) if sb.print_titles && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sb.name }
|
|
689
637
|
sb.defined_names&.each do |dn|
|
|
690
638
|
@defined_names << dn
|
|
691
639
|
end
|
|
@@ -721,7 +669,7 @@ module Xlsxrb
|
|
|
721
669
|
end
|
|
722
670
|
|
|
723
671
|
@workbook_properties[:active_sheet] = active_tab_idx unless active_tab_idx.nil?
|
|
724
|
-
@workbook_properties[:
|
|
672
|
+
@workbook_properties[:active_sheet_index] = active_tab_idx unless active_tab_idx.nil?
|
|
725
673
|
@workbook_properties[:first_sheet] = first_sheet_idx unless first_sheet_idx.nil?
|
|
726
674
|
@workbook_properties[:show_sheet_tabs] = @show_sheet_tabs unless @show_sheet_tabs.nil?
|
|
727
675
|
@workbook_properties[:tab_ratio] = @tab_ratio unless @tab_ratio.nil?
|
|
@@ -746,6 +694,17 @@ module Xlsxrb
|
|
|
746
694
|
|
|
747
695
|
private
|
|
748
696
|
|
|
697
|
+
#: (String? range, ?sheet: String?) -> String?
|
|
698
|
+
def set_print_area(range, sheet: nil)
|
|
699
|
+
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
700
|
+
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
|
|
701
|
+
if range
|
|
702
|
+
value = "'#{sheet_name}'!#{absolute_range(range)}"
|
|
703
|
+
defined_name("_xlnm.Print_Area", value, sheet: sheet_name, scope: sheet_name)
|
|
704
|
+
end
|
|
705
|
+
range&.to_s
|
|
706
|
+
end
|
|
707
|
+
|
|
749
708
|
#: (untyped range) -> untyped
|
|
750
709
|
def absolute_range(range)
|
|
751
710
|
DslHelpers.absolute_range(range)
|
|
@@ -765,7 +724,9 @@ module Xlsxrb
|
|
|
765
724
|
end
|
|
766
725
|
end
|
|
767
726
|
|
|
768
|
-
|
|
727
|
+
# Registers the styles of every sheet and builds the sheets' rows with their style
|
|
728
|
+
# names resolved to style indexes. Each cell is created once, here.
|
|
729
|
+
#: (Array[WorksheetBuilder::Snapshot] sheets) -> [Array[Elements::Worksheet], Hash[Symbol, untyped]]
|
|
769
730
|
def process_styles(sheets)
|
|
770
731
|
# Collect all unique StyleBuilders from workbook and sheets
|
|
771
732
|
all_style_builders = @styles.dup
|
|
@@ -777,7 +738,7 @@ module Xlsxrb
|
|
|
777
738
|
|
|
778
739
|
# Also collect inline styles from columns and rows
|
|
779
740
|
sheets.each do |sheet|
|
|
780
|
-
sheet.columns.each do |col|
|
|
741
|
+
sheet.frame.columns.each do |col|
|
|
781
742
|
raw_col_style = col.style_index
|
|
782
743
|
next unless raw_col_style.is_a?(Hash)
|
|
783
744
|
|
|
@@ -790,7 +751,7 @@ module Xlsxrb
|
|
|
790
751
|
end
|
|
791
752
|
|
|
792
753
|
sheet.rows.each do |row|
|
|
793
|
-
raw_row_style = row.
|
|
754
|
+
raw_row_style = row.style
|
|
794
755
|
next unless raw_row_style.is_a?(Hash)
|
|
795
756
|
|
|
796
757
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
@@ -802,12 +763,85 @@ module Xlsxrb
|
|
|
802
763
|
end
|
|
803
764
|
end
|
|
804
765
|
|
|
766
|
+
# A name defined more than once with different formats stands for the definition
|
|
767
|
+
# made last before each row was added, or for the first one if the row came before
|
|
768
|
+
# all of them, as for rows written by StreamWriter. The definition the name holds
|
|
769
|
+
# in all_style_builders keeps the name; each other one gets a key of its own, and
|
|
770
|
+
# is registered after the other styles if a row, a column, or a cell uses it.
|
|
771
|
+
versions = {} #: Hash[String, Array[[Integer, String]]]
|
|
772
|
+
version_builders = {} #: Hash[String, StyleBuilder]
|
|
773
|
+
@style_log.definitions.each do |name, definitions|
|
|
774
|
+
current = all_style_builders[name]
|
|
775
|
+
next if current.nil? || definitions.all? { |(_, builder)| builder.equal?(current) || builder.same_format?(current) }
|
|
776
|
+
|
|
777
|
+
versions[name] = definitions.map do |defined_at, builder|
|
|
778
|
+
next [defined_at, name] if builder.equal?(current)
|
|
779
|
+
|
|
780
|
+
key = "#{name}\u0000#{defined_at}"
|
|
781
|
+
version_builders[key] = builder
|
|
782
|
+
[defined_at, key]
|
|
783
|
+
end
|
|
784
|
+
end
|
|
785
|
+
used_versions = {} #: Hash[String, true]
|
|
786
|
+
|
|
787
|
+
# One pass over the cells checks their style names (names that were never defined
|
|
788
|
+
# are a mistake, not an unstyled cell) and finds the Date and Time cells styled
|
|
789
|
+
# without a number format. Those get a variant of their style that keeps the
|
|
790
|
+
# default date or time format. The variants are registered after every other
|
|
791
|
+
# style, in the order they are found.
|
|
792
|
+
date_variants = {}
|
|
793
|
+
variant_builders = {}
|
|
794
|
+
checked_style = style_key = nil
|
|
795
|
+
sheets.each do |sheet|
|
|
796
|
+
sheet.frame.columns.each do |col|
|
|
797
|
+
DslHelpers.check_style_name!(col.style_index, all_style_builders)
|
|
798
|
+
use_style_version(col.style_index, sheet.column_style_clocks[col.index], versions, used_versions) unless versions.empty?
|
|
799
|
+
end
|
|
800
|
+
sheet.rows.each do |row|
|
|
801
|
+
DslHelpers.check_style_name!(row.style, all_style_builders)
|
|
802
|
+
use_style_version(row.style, row.clock, versions, used_versions) unless versions.empty?
|
|
803
|
+
styles = row.styles
|
|
804
|
+
next if styles.nil?
|
|
805
|
+
|
|
806
|
+
values = row.values
|
|
807
|
+
if styles.is_a?(Array)
|
|
808
|
+
col_index = 0
|
|
809
|
+
while col_index < styles.size
|
|
810
|
+
style = styles[col_index]
|
|
811
|
+
unless style.nil?
|
|
812
|
+
# Rows usually repeat one style object; check it once.
|
|
813
|
+
unless style.equal?(checked_style)
|
|
814
|
+
# Symbol#name looks a Symbol up without allocating a String.
|
|
815
|
+
style_key = style.is_a?(Symbol) ? style.name : style
|
|
816
|
+
DslHelpers.check_style_name!(style, all_style_builders) unless all_style_builders.key?(style_key)
|
|
817
|
+
checked_style = style
|
|
818
|
+
end
|
|
819
|
+
use_style_version(style_key, row.clock, versions, used_versions) unless versions.empty?
|
|
820
|
+
add_date_variant(values[col_index], style_key, row.clock, versions, all_style_builders, version_builders, date_variants, variant_builders) if style_key.is_a?(String)
|
|
821
|
+
end
|
|
822
|
+
col_index += 1
|
|
823
|
+
end
|
|
824
|
+
elsif row.width.positive?
|
|
825
|
+
unless styles.equal?(checked_style)
|
|
826
|
+
style_key = styles.is_a?(Symbol) ? styles.name : styles
|
|
827
|
+
DslHelpers.check_style_name!(styles, all_style_builders) unless all_style_builders.key?(style_key)
|
|
828
|
+
checked_style = styles
|
|
829
|
+
end
|
|
830
|
+
use_style_version(style_key, row.clock, versions, used_versions) unless versions.empty?
|
|
831
|
+
values.each { |value| add_date_variant(value, style_key, row.clock, versions, all_style_builders, version_builders, date_variants, variant_builders) } if style_key.is_a?(String)
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
end
|
|
835
|
+
version_builders.each { |key, builder| all_style_builders[key] ||= builder if used_versions.key?(key) }
|
|
836
|
+
variant_builders.each { |name, builder| all_style_builders[name] ||= builder }
|
|
837
|
+
|
|
805
838
|
if all_style_builders.empty?
|
|
839
|
+
named_sheets = sheets.map(&:worksheet)
|
|
806
840
|
if @default_font
|
|
807
841
|
temp_writer = Ooxml::Writer.new(default_font: @default_font)
|
|
808
|
-
return [
|
|
842
|
+
return [named_sheets, extract_styles_from_writer(temp_writer)]
|
|
809
843
|
end
|
|
810
|
-
return [
|
|
844
|
+
return [named_sheets, {}]
|
|
811
845
|
end
|
|
812
846
|
|
|
813
847
|
# Create a temporary writer to register styles and get numeric IDs
|
|
@@ -821,12 +855,12 @@ module Xlsxrb
|
|
|
821
855
|
# Capture the style definitions from the temporary writer
|
|
822
856
|
styles_definition = extract_styles_from_writer(temp_writer)
|
|
823
857
|
|
|
824
|
-
#
|
|
858
|
+
# Build all sheets with resolved style IDs and style inheritance
|
|
825
859
|
updated_sheets = sheets.map do |sheet|
|
|
826
860
|
col_style_map = {}
|
|
827
861
|
col_fmt_map = {}
|
|
828
862
|
|
|
829
|
-
new_columns = sheet.columns.map do |col|
|
|
863
|
+
new_columns = sheet.frame.columns.map do |col|
|
|
830
864
|
raw_col_style = col.style_index
|
|
831
865
|
resolved_col_style = if raw_col_style.is_a?(Hash)
|
|
832
866
|
inline_name = "__inline_col_#{raw_col_style.hash}"
|
|
@@ -836,11 +870,12 @@ module Xlsxrb
|
|
|
836
870
|
end
|
|
837
871
|
style_name_to_id[inline_name]
|
|
838
872
|
elsif (raw_col_style.is_a?(String) || raw_col_style.is_a?(Symbol)) && style_name_to_id.key?(raw_col_style.to_s)
|
|
839
|
-
|
|
873
|
+
col_key = style_version(raw_col_style.to_s, sheet.column_style_clocks[col.index], versions)
|
|
874
|
+
sb = all_style_builders[col_key]
|
|
840
875
|
if sb&.number_format
|
|
841
876
|
col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
|
|
842
877
|
end
|
|
843
|
-
style_name_to_id[
|
|
878
|
+
style_name_to_id[col_key]
|
|
844
879
|
else
|
|
845
880
|
raw_col_style
|
|
846
881
|
end
|
|
@@ -855,8 +890,10 @@ module Xlsxrb
|
|
|
855
890
|
Elements::Column.new(
|
|
856
891
|
index: col.index,
|
|
857
892
|
width: col.width,
|
|
858
|
-
hidden: col.hidden
|
|
859
|
-
custom_width: col.custom_width
|
|
893
|
+
hidden: col.hidden?,
|
|
894
|
+
custom_width: col.custom_width?,
|
|
895
|
+
best_fit: col.best_fit?,
|
|
896
|
+
collapsed: col.collapsed?,
|
|
860
897
|
outline_level: col.outline_level,
|
|
861
898
|
style_index: resolved_col_style,
|
|
862
899
|
unmapped_data: unmapped,
|
|
@@ -865,74 +902,154 @@ module Xlsxrb
|
|
|
865
902
|
end
|
|
866
903
|
end
|
|
867
904
|
|
|
905
|
+
last_raw_style = last_resolved_style = nil
|
|
868
906
|
new_rows = sheet.rows.map do |row|
|
|
869
|
-
|
|
907
|
+
row_clock = row.clock
|
|
908
|
+
# A name may stand for another definition in this row, so resolve it again.
|
|
909
|
+
last_raw_style = nil unless versions.empty?
|
|
910
|
+
raw_row_style = row.style
|
|
870
911
|
resolved_row_style = if raw_row_style.is_a?(Hash)
|
|
871
912
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
872
913
|
style_name_to_id[inline_name]
|
|
873
914
|
elsif (raw_row_style.is_a?(String) || raw_row_style.is_a?(Symbol)) && style_name_to_id.key?(raw_row_style.to_s)
|
|
874
|
-
style_name_to_id[raw_row_style.to_s]
|
|
915
|
+
style_name_to_id[style_version(raw_row_style.to_s, row_clock, versions)]
|
|
875
916
|
elsif raw_row_style.is_a?(Integer)
|
|
876
917
|
raw_row_style
|
|
877
918
|
end
|
|
878
|
-
row_unmapped = row.unmapped_data
|
|
879
|
-
if resolved_row_style && resolved_row_style != raw_row_style
|
|
880
|
-
row_unmapped = (row.unmapped_data || {}).dup
|
|
881
|
-
row_unmapped[:style_index] = resolved_row_style
|
|
882
|
-
end
|
|
883
919
|
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
920
|
+
row_index = row.index
|
|
921
|
+
values = row.values
|
|
922
|
+
styles = row.styles
|
|
923
|
+
codes = row.format_codes
|
|
924
|
+
style_lookup = styles.is_a?(Array)
|
|
925
|
+
code_lookup = codes.is_a?(Array)
|
|
926
|
+
all_valid = row.within_limits?
|
|
927
|
+
width = row.width
|
|
928
|
+
cells = [] #: Array[Elements::Cell]
|
|
929
|
+
col_index = 0
|
|
930
|
+
while col_index < width
|
|
931
|
+
value = col_index < values.size ? values[col_index] : nil
|
|
932
|
+
style = style_lookup ? styles[col_index] : styles
|
|
933
|
+
if value.nil? && style.nil?
|
|
934
|
+
col_index += 1
|
|
935
|
+
next
|
|
936
|
+
end
|
|
937
|
+
|
|
938
|
+
format_code = code_lookup ? codes[col_index] : codes
|
|
939
|
+
raw_cell_style = style
|
|
940
|
+
date_format = nil
|
|
941
|
+
unless date_variants.empty?
|
|
942
|
+
cell_value = value.is_a?(Elements::Formula) ? value.cached_value : value
|
|
943
|
+
if (cell_value.is_a?(Date) || cell_value.is_a?(Time)) && (style.is_a?(String) || style.is_a?(Symbol))
|
|
944
|
+
date_variant = date_variants[[style_version(style.to_s, row_clock, versions), cell_value.is_a?(Time) || cell_value.is_a?(DateTime)]]
|
|
945
|
+
raw_cell_style, date_format = date_variant if date_variant
|
|
946
|
+
end
|
|
947
|
+
end
|
|
948
|
+
|
|
949
|
+
resolved_cell_style = if raw_cell_style.nil?
|
|
950
|
+
resolved_row_style || col_style_map[col_index]
|
|
951
|
+
elsif raw_cell_style.equal?(last_raw_style)
|
|
952
|
+
# Rows usually repeat one style object; reuse its last resolution.
|
|
953
|
+
last_resolved_style
|
|
890
954
|
else
|
|
891
|
-
raw_cell_style
|
|
955
|
+
last_raw_style = raw_cell_style
|
|
956
|
+
# Symbol#name looks a Symbol up without allocating a String.
|
|
957
|
+
style_key = raw_cell_style.is_a?(Symbol) ? raw_cell_style.name : raw_cell_style
|
|
958
|
+
style_key = style_version(style_key, row_clock, versions) if style_key.is_a?(String) && !versions.empty?
|
|
959
|
+
last_resolved_style = (style_key.is_a?(String) && style_name_to_id[style_key]) || raw_cell_style
|
|
892
960
|
end
|
|
893
961
|
|
|
894
|
-
target_fmt =
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
comment: cell.comment,
|
|
908
|
-
raw_value: cell.raw_value,
|
|
909
|
-
format_code: target_fmt,
|
|
910
|
-
unmapped_data: cell.unmapped_data,
|
|
911
|
-
errors: cell.errors
|
|
912
|
-
)
|
|
913
|
-
end
|
|
962
|
+
target_fmt = format_code || date_format || col_fmt_map[col_index]
|
|
963
|
+
unchanged = resolved_cell_style == style && target_fmt == format_code
|
|
964
|
+
valid = all_valid || Elements::Cell.valid_coordinates?(row_index, col_index)
|
|
965
|
+
|
|
966
|
+
cells << if valid && !value.is_a?(Elements::Formula) && !(unchanged && style.is_a?(Integer))
|
|
967
|
+
# A valid cell has no errors, so it skips the checks of Cell.new.
|
|
968
|
+
Elements::Cell.fast_create(row_index, col_index, value, resolved_cell_style, nil, value&.to_s, target_fmt)
|
|
969
|
+
elsif unchanged
|
|
970
|
+
WorksheetBuilder::PendingRow.cell(row_index, col_index, value, style, format_code, valid)
|
|
971
|
+
else
|
|
972
|
+
resolved_cell(row_index, col_index, value, resolved_cell_style, target_fmt, valid)
|
|
973
|
+
end
|
|
974
|
+
col_index += 1
|
|
914
975
|
end
|
|
915
976
|
|
|
916
|
-
|
|
917
|
-
index: row.index,
|
|
918
|
-
cells: new_cells,
|
|
919
|
-
height: row.height,
|
|
920
|
-
hidden: row.hidden?,
|
|
921
|
-
custom_height: row.custom_height?,
|
|
922
|
-
collapsed: row.collapsed?,
|
|
923
|
-
outline_level: row.outline_level,
|
|
924
|
-
style_index: resolved_row_style,
|
|
925
|
-
unmapped_data: row_unmapped,
|
|
926
|
-
errors: row.errors
|
|
927
|
-
)
|
|
977
|
+
row.to_row(sheet.attach_links(cells), resolved_row_style)
|
|
928
978
|
end
|
|
929
979
|
|
|
930
|
-
sheet.
|
|
980
|
+
sheet.worksheet(new_rows, new_columns)
|
|
931
981
|
end
|
|
932
982
|
|
|
933
983
|
[updated_sheets, styles_definition]
|
|
934
984
|
end
|
|
935
985
|
|
|
986
|
+
# Returns the key of the definition that the style +name+ stands for at style log
|
|
987
|
+
# +clock+: the last one made by then, or the first one if none was. A name defined
|
|
988
|
+
# once, or always with the same format, is its own key.
|
|
989
|
+
#: (String name, Integer? clock, Hash[String, Array[[Integer, String]]] versions) -> String
|
|
990
|
+
def style_version(name, clock, versions)
|
|
991
|
+
definitions = versions[name]
|
|
992
|
+
return name if definitions.nil? || clock.nil?
|
|
993
|
+
|
|
994
|
+
key = definitions.first.last
|
|
995
|
+
definitions.each do |defined_at, version_key|
|
|
996
|
+
break if defined_at > clock
|
|
997
|
+
|
|
998
|
+
key = version_key
|
|
999
|
+
end
|
|
1000
|
+
key
|
|
1001
|
+
end
|
|
1002
|
+
|
|
1003
|
+
# Marks the definition that the style +name+ stands for at style log +clock+ as used.
|
|
1004
|
+
#: (untyped name, Integer? clock, Hash[String, Array[[Integer, String]]] versions, Hash[String, true] used_versions) -> void
|
|
1005
|
+
def use_style_version(name, clock, versions, used_versions)
|
|
1006
|
+
name = name.name if name.is_a?(Symbol)
|
|
1007
|
+
return unless name.is_a?(String) && versions.key?(name)
|
|
1008
|
+
|
|
1009
|
+
used_versions[style_version(name, clock, versions)] = true
|
|
1010
|
+
end
|
|
1011
|
+
|
|
1012
|
+
# Records the date or time variant of the style named +style_key+ that a Date or
|
|
1013
|
+
# Time +value+ in a row added at style log +clock+ needs, if any.
|
|
1014
|
+
#: (untyped value, String style_key, Integer clock, Hash[String, Array[[Integer, String]]] versions, Hash[String, untyped] all_style_builders, Hash[String, StyleBuilder] version_builders, Hash[untyped, untyped] date_variants, Hash[String, untyped] variant_builders) -> void
|
|
1015
|
+
def add_date_variant(value, style_key, clock, versions, all_style_builders, version_builders, date_variants, variant_builders)
|
|
1016
|
+
value = value.cached_value if value.is_a?(Elements::Formula)
|
|
1017
|
+
return unless value.is_a?(Date) || value.is_a?(Time)
|
|
1018
|
+
|
|
1019
|
+
style_key = style_version(style_key, clock, versions)
|
|
1020
|
+
style_builder = all_style_builders[style_key] || version_builders[style_key]
|
|
1021
|
+
variant = DslHelpers.date_style_variant(value, style_key, style_builder)
|
|
1022
|
+
return unless variant
|
|
1023
|
+
|
|
1024
|
+
variant_name, number_format = variant
|
|
1025
|
+
date_variants[[style_key, value.is_a?(Time) || value.is_a?(DateTime)]] = variant
|
|
1026
|
+
variant_builders[variant_name] ||= style_builder.with_number_format(variant_name, number_format)
|
|
1027
|
+
end
|
|
1028
|
+
|
|
1029
|
+
# Creates a cell whose style was resolved to +style+ (an index, or a name with no index).
|
|
1030
|
+
#: (Integer row_index, Integer column_index, untyped value, untyped style, String? format_code, bool valid_coordinates) -> Elements::Cell
|
|
1031
|
+
def resolved_cell(row_index, column_index, value, style, format_code, valid_coordinates)
|
|
1032
|
+
formula = nil
|
|
1033
|
+
if value.is_a?(Elements::Formula)
|
|
1034
|
+
formula = value
|
|
1035
|
+
value = formula.cached_value
|
|
1036
|
+
valid_coordinates &&= Elements::Cell.valid_value?(value)
|
|
1037
|
+
end
|
|
1038
|
+
raw_value = value&.to_s
|
|
1039
|
+
return Elements::Cell.fast_create(row_index, column_index, value, style, formula, raw_value, format_code) if valid_coordinates
|
|
1040
|
+
|
|
1041
|
+
Elements::Cell.new(
|
|
1042
|
+
row_index: row_index,
|
|
1043
|
+
column_index: column_index,
|
|
1044
|
+
value: value,
|
|
1045
|
+
formula: formula,
|
|
1046
|
+
style: (style unless style.is_a?(Integer)),
|
|
1047
|
+
style_index: (style if style.is_a?(Integer)),
|
|
1048
|
+
raw_value: raw_value,
|
|
1049
|
+
format_code: format_code
|
|
1050
|
+
)
|
|
1051
|
+
end
|
|
1052
|
+
|
|
936
1053
|
#: (untyped writer) -> Hash[Symbol, untyped]
|
|
937
1054
|
def extract_styles_from_writer(writer)
|
|
938
1055
|
# Extract style definitions from the writer that can be reused
|