xlsxrb 0.1.18 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +163 -0
  3. data/README.md +34 -11
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
  5. data/lib/xlsxrb/chart_builder.rb +251 -386
  6. data/lib/xlsxrb/dsl_helpers.rb +120 -67
  7. data/lib/xlsxrb/elements/cell.rb +37 -157
  8. data/lib/xlsxrb/elements/column.rb +11 -19
  9. data/lib/xlsxrb/elements/coordinate_access.rb +15 -86
  10. data/lib/xlsxrb/elements/image.rb +5 -72
  11. data/lib/xlsxrb/elements/metadata.rb +463 -817
  12. data/lib/xlsxrb/elements/row.rb +27 -145
  13. data/lib/xlsxrb/elements/styles.rb +30 -88
  14. data/lib/xlsxrb/elements/types.rb +6 -99
  15. data/lib/xlsxrb/elements/workbook.rb +19 -126
  16. data/lib/xlsxrb/elements/worksheet.rb +43 -314
  17. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -1
  18. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -7
  19. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +32 -17
  20. data/lib/xlsxrb/ooxml/reader.rb +49 -51
  21. data/lib/xlsxrb/ooxml/styles_parser.rb +26 -2
  22. data/lib/xlsxrb/ooxml/utils.rb +2 -2
  23. data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
  24. data/lib/xlsxrb/ooxml/workbook_writer.rb +172 -93
  25. data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
  26. data/lib/xlsxrb/ooxml/worksheet_writer.rb +65 -47
  27. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
  28. data/lib/xlsxrb/ooxml/writer/features_xml.rb +53 -14
  29. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
  30. data/lib/xlsxrb/ooxml/writer.rb +105 -41
  31. data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
  32. data/lib/xlsxrb/ooxml/zip_reader.rb +65 -15
  33. data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
  34. data/lib/xlsxrb/stream_row.rb +12 -116
  35. data/lib/xlsxrb/stream_sheet.rb +14 -261
  36. data/lib/xlsxrb/stream_writer.rb +623 -1476
  37. data/lib/xlsxrb/style_builder.rb +376 -593
  38. data/lib/xlsxrb/version.rb +1 -1
  39. data/lib/xlsxrb/workbook_builder.rb +192 -226
  40. data/lib/xlsxrb/worksheet_builder.rb +456 -1324
  41. data/lib/xlsxrb.rb +266 -1083
  42. data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
  43. data/sig/generated/xlsxrb/dsl_helpers.rbs +64 -16
  44. data/sig/generated/xlsxrb/elements/cell.rbs +12 -76
  45. data/sig/generated/xlsxrb/elements/column.rbs +0 -7
  46. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +5 -41
  47. data/sig/generated/xlsxrb/elements/image.rbs +3 -29
  48. data/sig/generated/xlsxrb/elements/metadata.rbs +138 -151
  49. data/sig/generated/xlsxrb/elements/row.rbs +10 -53
  50. data/sig/generated/xlsxrb/elements/styles.rbs +1 -39
  51. data/sig/generated/xlsxrb/elements/workbook.rbs +14 -78
  52. data/sig/generated/xlsxrb/elements/worksheet.rbs +5 -121
  53. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +0 -4
  54. data/sig/generated/xlsxrb/ooxml/reader.rbs +0 -3
  55. data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
  56. data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
  57. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +12 -0
  58. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
  59. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +5 -3
  60. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
  61. data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
  62. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
  63. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +22 -7
  64. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
  65. data/sig/generated/xlsxrb/stream_row.rbs +7 -42
  66. data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
  67. data/sig/generated/xlsxrb/stream_writer.rbs +457 -976
  68. data/sig/generated/xlsxrb/style_builder.rbs +206 -242
  69. data/sig/generated/xlsxrb/workbook_builder.rbs +84 -132
  70. data/sig/generated/xlsxrb/worksheet_builder.rbs +309 -685
  71. data/sig/generated/xlsxrb.rbs +43 -190
  72. data/sig/rexml.rbs +8 -0
  73. metadata +3 -5
  74. data/lib/xlsxrb/utils.rb +0 -251
  75. data/sig/generated/xlsxrb/utils.rbs +0 -200
@@ -13,18 +13,19 @@ module Xlsxrb
13
13
  # sheet.row(["Product", "Revenue"])
14
14
  # sheet.row(["Widget", 1000])
15
15
  # end
16
- # builder.core_property(:creator, "Reporting System")
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, ?escape_formulas: bool, ?default_font: untyped?) -> void
25
- def initialize(strict_excel_mode: true, escape_formulas: false, default_font: nil)
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
30
  @sheets = []
30
31
  @sheet_builders = [] # Keep track of sheet builders for style processing
@@ -41,19 +42,6 @@ module Xlsxrb
41
42
  @tab_ratio = nil
42
43
  end
43
44
 
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
45
  # Sets or configures the workbook-wide default font.
58
46
  #
59
47
  # @param name [String, Hash, nil] Font name or font options Hash.
@@ -72,8 +60,6 @@ module Xlsxrb
72
60
  existing[:size] = size if size
73
61
  @default_font = existing.merge(opts)
74
62
  end
75
- # @deprecated Use {#default_font} instead. Will be removed in 0.2.0.
76
- alias set_default_font default_font
77
63
 
78
64
  # @return [Hash{String => StyleBuilder}]
79
65
  #: Hash[String, StyleBuilder]
@@ -158,114 +144,51 @@ module Xlsxrb
158
144
  Xlsxrb.cell_error(...)
159
145
  end
160
146
 
161
- OMITTED = Object.new.freeze
162
- private_constant :OMITTED
163
-
164
147
  # Gets or sets the active worksheet tab in the workbook window.
165
148
  #
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
149
  # @return [String, Symbol, Integer, nil]
185
150
  # @api public
186
- attr_writer :active_sheet #: (String | Symbol | Integer | nil)
187
- # @deprecated Use {#active_sheet=} instead. Will be removed in 0.2.0.
188
- alias active_tab= active_sheet=
151
+ #: (String | Symbol | Integer | nil)
152
+ attr_accessor :active_sheet
189
153
 
190
154
  # Gets or sets the first sheet tab visible in the sheet tab scroll area.
191
155
  #
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
- # @return [String, Symbol, Integer, nil]
195
- # @api public
196
- #: (?String | Symbol | Integer | nil name_or_index) -> (String | Symbol | Integer | nil)
197
- def first_sheet(name_or_index = OMITTED)
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
156
  # @return [String, Symbol, Integer, nil]
209
157
  # @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
158
+ #: (String | Symbol | Integer | nil)
159
+ attr_accessor :first_sheet
226
160
 
227
161
  # Sets whether sheet tabs are visible in the workbook window.
228
162
  #
229
163
  # @param visible [Boolean, nil]
230
164
  # @return [Boolean, nil]
231
165
  # @api public
232
- attr_writer :show_sheet_tabs #: bool?
166
+ #: (bool? visible) -> bool?
167
+ attr_writer :show_sheet_tabs
233
168
 
234
- # Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
169
+ # Returns whether sheet tabs are visible in the workbook window.
235
170
  #
236
- # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#tab_ratio=} instead.
237
- # @param ratio [Integer, nil]
238
- # @return [Integer, nil]
171
+ # @return [Boolean]
239
172
  # @api public
240
- #: (?Integer? ratio) -> Integer?
241
- def tab_ratio(ratio = OMITTED)
242
- if ratio.equal?(OMITTED)
243
- @tab_ratio
244
- else
245
- @tab_ratio = ratio
246
- end
173
+ #: () -> bool
174
+ def show_sheet_tabs?
175
+ @show_sheet_tabs != false
247
176
  end
248
177
 
249
- # Sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
178
+ # Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
250
179
  #
251
- # @param ratio [Integer, nil]
252
180
  # @return [Integer, nil]
253
181
  # @api public
254
- attr_writer :tab_ratio #: Integer?
182
+ #: Integer?
183
+ attr_accessor :tab_ratio
255
184
 
256
- # Gets or sets the internal code name of the workbook.
185
+ # Gets the internal code name of the workbook.
257
186
  #
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
187
  # @return [String, nil]
261
188
  # @api public
262
- #: (?String | Symbol | nil name) -> String?
263
- def code_name(name = OMITTED)
264
- if name.equal?(OMITTED)
265
- @workbook_properties[:code_name]
266
- else
267
- @workbook_properties[:code_name] = name&.to_s
268
- end
189
+ #: () -> String?
190
+ def code_name
191
+ @workbook_properties[:code_name]
269
192
  end
270
193
 
271
194
  # Sets the internal code name of the workbook.
@@ -278,29 +201,25 @@ module Xlsxrb
278
201
  @workbook_properties[:code_name] = name&.to_s
279
202
  end
280
203
 
281
- # Sets a workbook-level property.
204
+ # Configures or queries workbook-level properties.
205
+ #
206
+ # When called with no arguments, returns the {Elements::WorkbookProperties} value object.
207
+ # When called with keyword options, configures properties and returns nil.
282
208
  #
283
209
  # @note **SECURITY WARNING:** If you set `:update_links` to anything other than `"never"`,
284
210
  # you may expose end-users to malicious external reference vulnerabilities (e.g., CSV/DDE Injection)
285
211
  # when they open the generated Excel file. Ensure you fully trust the exported data.
286
212
  #
287
- # @param name [Symbol] Property name (e.g. :update_links).
288
- # @param value [String, Integer, Boolean] Property value.
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]
213
+ # @param opts [Hash] Workbook properties map.
214
+ # @return [Elements::WorkbookProperties, void]
300
215
  # @api public
301
216
  #: () -> Elements::WorkbookProperties
302
- def workbook_properties
303
- Elements::WorkbookProperties.new(**@workbook_properties)
217
+ #: (**untyped opts) -> void
218
+ def workbook_properties(**opts)
219
+ return Elements::WorkbookProperties.new(**@workbook_properties) if opts.empty?
220
+
221
+ @workbook_properties.merge!(opts)
222
+ nil
304
223
  end
305
224
 
306
225
  # Sets workbook-level properties.
@@ -310,7 +229,9 @@ module Xlsxrb
310
229
  # @api public
311
230
  #: (untyped props) -> void
312
231
  def workbook_properties=(props)
313
- @workbook_properties = props.is_a?(Elements::MetadataRecord) ? props.to_h : props
232
+ h = props.respond_to?(:to_h) ? props.to_h : (props || {})
233
+ @workbook_properties = h.transform_keys(&:to_sym)
234
+ nil
314
235
  end
315
236
 
316
237
  # Returns the list of worksheet names added to this workbook so far.
@@ -326,17 +247,16 @@ module Xlsxrb
326
247
  #
327
248
  # @param name [String, nil] Sheet name (max 31 chars, no forbidden chars `[ ] * ? / \ :`).
328
249
  # @param state [Symbol, String, nil] Sheet visibility state (:visible, :hidden, :very_hidden).
329
- # @param tab_color [String, Hash, nil] Tab color hex string or color hash.
330
- # @param code_name [String, nil] VBA code name.
331
- # @param hidden [Boolean, nil] Deprecated alias for state: :hidden.
250
+ # @param tab_color [String, Hash, Symbol, nil] Tab color hex string (e.g. "FF0000") or hash options.
251
+ # @param code_name [String, Symbol, nil] Internal VBA code name of the sheet.
332
252
  # @param opts [Hash] Additional sheet properties.
333
253
  # @yield [sheet_builder]
334
254
  # @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder]
335
255
  # @return [Elements::Worksheet]
336
256
  # @raise [ArgumentError] If sheet name violates Excel limits in strict mode.
337
257
  # @api public
338
- #: (?String? name, ?state: (Symbol | String)?, ?tab_color: untyped, ?code_name: String?, ?hidden: bool?, **untyped opts) ?{ (WorksheetBuilder) -> void } -> Elements::Worksheet
339
- def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, hidden: nil, **opts)
258
+ #: (?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) -> void } -> Elements::Worksheet
259
+ def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts)
340
260
  sheet_count = [@sheets.size, @sheet_builders.size].max
341
261
  name ||= "Sheet#{sheet_count + 1}"
342
262
  raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name.length > 31
@@ -349,11 +269,9 @@ module Xlsxrb
349
269
 
350
270
  active = opts.delete(:active)
351
271
  state = opts.delete(:state) if opts.key?(:state)
352
- hidden = opts.delete(:hidden) if opts.key?(:hidden)
272
+ hidden = opts.delete(:hidden)
353
273
  code_name = opts.delete(:code_name) if opts.key?(:code_name)
354
274
  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
275
  state ||= :hidden if hidden
358
276
  state ||= :visible
359
277
  state = state.to_sym
@@ -361,10 +279,10 @@ module Xlsxrb
361
279
 
362
280
  @active_sheet = name if active
363
281
 
364
- sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state, escape_formulas: @escape_formulas)
282
+ sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state)
365
283
  sheet_builder.code_name = code_name if code_name
366
284
  sheet_builder.tab_color = tab_color if tab_color
367
- opts.each { |k, v| sheet_builder.sheet_properties(k, v) }
285
+ sheet_builder.sheet_properties(**opts) unless opts.empty?
368
286
  @sheet_builders << sheet_builder
369
287
  yield sheet_builder if block_given?
370
288
  @defined_names.concat(sheet_builder.defined_names)
@@ -395,32 +313,17 @@ module Xlsxrb
395
313
  # @param sheet [String, nil] Reference sheet name for coordinates/formula.
396
314
  # @param scope [String, Symbol, nil] Optional sheet name or :workbook for scope (defaults to workbook).
397
315
  # @param hidden [Boolean] Whether the defined name is hidden from Excel's UI.
398
- # @param column [Integer, String, nil] Deprecated alias for columns.
399
316
  # @param columns [Integer, String, nil] Column index or letter for coordinate-based reference.
400
- # @param col [Integer, String, nil] Deprecated alias for column.
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.
317
+ # @param rows [Integer, Range, nil] Row or row range (0-based) for coordinate-based reference.
403
318
  # @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
319
  # @return [void]
407
320
  # @api public
408
- #: (String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?column: untyped, ?columns: untyped, ?col: untyped, ?row: untyped, ?rows: untyped, ?range: untyped, ?ref_sheet: String?, ?local: bool) -> void
409
- def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false, column: nil, columns: nil, col: nil, row: nil, rows: nil, range: nil, ref_sheet: nil, local: false)
410
- warn "[DEPRECATION] `col:` is deprecated; use `column:` (or `columns:`) instead.", uplevel: 1 if col
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)
321
+ #: (String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?columns: untyped, ?rows: untyped, ?range: untyped) -> void
322
+ def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false, columns: nil, rows: nil, range: nil)
323
+ normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: sheet, columns: columns, rows: rows, range: range)
419
324
  entry = { name: name, value: normalized_val, hidden: hidden }
420
325
  local_sheet = if scope
421
326
  scope == :workbook ? nil : scope.to_s
422
- elsif sheet || local || ref_sheet
423
- sheet
424
327
  end
425
328
  entry[:local_sheet_name] = local_sheet if local_sheet
426
329
  @defined_names << entry
@@ -435,53 +338,54 @@ module Xlsxrb
435
338
  @defined_names.map { |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) }
436
339
  end
437
340
 
438
- # Sets or gets the print area for a sheet.
341
+ # Returns the print area range for the sheet, or nil if none.
439
342
  #
440
- # @param range [String, Object, nil] Cell range (e.g. "A1:G50").
441
343
  # @param sheet [String, nil] Target sheet name (defaults to latest or "Sheet1").
442
- # @return [String, nil, void]
344
+ # @return [String, nil]
443
345
  # @api public
444
- #: (?untyped range, ?sheet: String?) -> (String | nil | void)
445
- def print_area(range = OMITTED, sheet: nil)
346
+ #: (?sheet: String?) -> String?
347
+ def print_area(sheet: nil)
446
348
  sheet_name = sheet || @sheets.last&.name || "Sheet1"
447
- if range.equal?(OMITTED)
448
- dn = @defined_names.find { |d| d[:name] == "_xlnm.Print_Area" && d[:local_sheet_name] == sheet_name }
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
349
+ dn = @defined_names.find { |d| d[:name] == "_xlnm.Print_Area" && d[:local_sheet_name] == sheet_name }
350
+ dn ? dn[:value].split("!", 2).last.tr("$", "") : nil
458
351
  end
459
352
 
460
- # Sets the print area for the worksheet.
353
+ # Sets the print area for the current worksheet.
461
354
  #
462
355
  # @param range [String, nil] Cell range (e.g. "A1:G50").
463
356
  # @return [String, nil]
464
357
  # @api public
465
358
  #: (String? range) -> String?
466
359
  def print_area=(range)
467
- print_area(range)
468
- range
360
+ sheet_name = @sheets.last&.name || "Sheet1"
361
+ set_print_area(range, sheet: sheet_name)
469
362
  end
470
363
 
471
- # Sets repeating print titles (rows and/or columns) for pagination.
364
+ # Sets or gets repeating print titles (rows and/or columns) for pagination.
365
+ #
366
+ # When called with no row/column arguments, returns the {Elements::PrintTitles} value object or nil.
367
+ # When called with row/column arguments, sets repeating print titles and returns nil.
472
368
  #
473
369
  # @param rows [String, nil] Repeating row range (e.g. "1:2").
474
370
  # @param columns [String, nil] Repeating column range (e.g. "A:B").
475
- # @param cols [String, nil] Deprecated alias for columns.
476
371
  # @param sheet [String, nil] Target sheet name.
477
- # @return [void]
372
+ # @return [Elements::PrintTitles, nil, void]
478
373
  # @api public
479
- #: (?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?, ?cols: (String | Integer | Symbol | Range[untyped])?, ?sheet: String?) -> void
480
- def print_titles(rows: nil, columns: nil, cols: nil, sheet: nil)
481
- warn "[DEPRECATION] `cols:` is deprecated; use `columns:` instead.", uplevel: 1 if cols && columns.nil?
482
- c = DslHelpers.normalize_print_titles_columns(columns || cols)
483
- r = DslHelpers.normalize_print_titles_rows(rows)
374
+ #: () -> Elements::PrintTitles?
375
+ #: (?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?, ?sheet: String?) -> void
376
+ def print_titles(rows: nil, columns: nil, sheet: nil)
484
377
  sheet_name = sheet || @sheets.last&.name || "Sheet1"
378
+ if rows.nil? && columns.nil?
379
+ existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
380
+ return nil unless existing
381
+
382
+ c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
383
+ r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
384
+ return Elements::PrintTitles.new(rows: r, columns: c)
385
+ end
386
+
387
+ c = DslHelpers.normalize_print_titles_columns(columns)
388
+ r = DslHelpers.normalize_print_titles_rows(rows)
485
389
  existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
486
390
  if existing && (r.nil? || c.nil?)
487
391
  c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
@@ -492,19 +396,24 @@ module Xlsxrb
492
396
  parts << "'#{sheet_name}'!$#{r.sub(":", ":$")}" if r
493
397
  value = parts.join(",")
494
398
  @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)
399
+ defined_name("_xlnm.Print_Titles", value, sheet: sheet_name, scope: sheet_name)
496
400
  nil
497
401
  end
498
402
 
499
403
  # Sets repeating print titles.
500
404
  #
501
- # @param opts [Elements::PrintTitles, Hash] Options with :rows and/or :columns.
502
- # @return [Elements::PrintTitles, Hash]
405
+ # @param opts [Elements::PrintTitles, Hash, nil] Options with :rows and/or :columns.
406
+ # @return [Elements::PrintTitles, Hash, nil]
503
407
  # @api public
504
- #: (Elements::PrintTitles | Hash[Symbol, untyped] opts) -> (Elements::PrintTitles | Hash[Symbol, untyped])
408
+ #: (Elements::PrintTitles | Hash[Symbol, untyped] | nil opts) -> (Elements::PrintTitles | Hash[Symbol, untyped] | nil)
505
409
  def print_titles=(opts)
506
- h = opts.respond_to?(:to_h) ? opts.to_h : opts
507
- print_titles(**h)
410
+ if opts.nil?
411
+ sheet_name = @sheets.last&.name || "Sheet1"
412
+ @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
413
+ else
414
+ h = opts.respond_to?(:to_h) ? opts.to_h : opts
415
+ print_titles(**h)
416
+ end
508
417
  opts
509
418
  end
510
419
 
@@ -567,7 +476,7 @@ module Xlsxrb
567
476
  if opts.empty?
568
477
  @workbook_protection ? Elements::WorkbookProtection.new(**@workbook_protection) : nil
569
478
  else
570
- @workbook_protection = opts
479
+ @workbook_protection = DslHelpers.normalize_workbook_protection(opts)
571
480
  nil
572
481
  end
573
482
  end
@@ -579,54 +488,69 @@ module Xlsxrb
579
488
  # @api public
580
489
  #: (untyped opts) -> void
581
490
  def workbook_protection=(opts)
582
- @workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : opts
491
+ @workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : (opts && DslHelpers.normalize_workbook_protection(opts))
583
492
  end
584
493
 
585
- # @deprecated Use {#workbook_protection} instead.
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.
494
+ # Configures or queries Dublin Core document metadata properties.
593
495
  #
594
- # @param name [Symbol] Core property name (:creator, :title, :subject, :description, etc.).
595
- # @param value [String, Integer, Time] Property value.
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.
496
+ # When called with no arguments, returns the {Elements::CoreProperties} value object.
497
+ # When called with keyword options, configures properties and returns nil.
604
498
  #
605
- # @return [Elements::CoreProperties]
499
+ # @param opts [Hash] Core properties map (:creator, :title, :subject, :description, etc.).
500
+ # @return [Elements::CoreProperties, void]
606
501
  # @api public
607
502
  #: () -> Elements::CoreProperties
608
- def core_properties
609
- Elements::CoreProperties.new(**@core_properties)
503
+ #: (**untyped opts) -> void
504
+ def core_properties(**opts)
505
+ return Elements::CoreProperties.new(**@core_properties) if opts.empty?
506
+
507
+ @core_properties.merge!(opts)
508
+ nil
610
509
  end
611
510
 
612
- # Sets an extended application metadata property.
511
+ # Replaces core document properties.
613
512
  #
614
- # @param name [Symbol] Application property name (:company, :manager, :app_version, etc.).
615
- # @param value [String, Integer, Time] Property value.
513
+ # @param props [Hash, Elements::CoreProperties]
616
514
  # @return [void]
617
515
  # @api public
618
- #: (Symbol name, String | Integer | Time value) -> void
619
- def app_property(name, value)
620
- @app_properties[name] = value
516
+ #: (Hash[Symbol, untyped] | Elements::CoreProperties props) -> void
517
+ def core_properties=(props)
518
+ h = props.respond_to?(:to_h) ? props.to_h : (props || {})
519
+ @core_properties = h.transform_keys(&:to_sym)
520
+ nil
621
521
  end
622
522
 
623
- # Returns application document properties.
523
+ # Configures or queries extended application metadata properties.
524
+ #
525
+ # When called with no arguments, returns the {Elements::AppProperties} value object.
526
+ # When called with keyword options, configures properties and returns nil.
624
527
  #
625
- # @return [Elements::AppProperties]
528
+ # @param opts [Hash] Application properties map (:company, :manager, :app_version, etc.).
529
+ # @return [Elements::AppProperties, void]
626
530
  # @api public
627
531
  #: () -> Elements::AppProperties
628
- def app_properties
629
- Elements::AppProperties.new(**@app_properties)
532
+ #: (**untyped opts) -> void
533
+ def app_properties(**opts)
534
+ return Elements::AppProperties.new(**@app_properties) if opts.empty?
535
+
536
+ opts.each do |k, v|
537
+ norm_name = k.to_sym == :application ? :app_name : k.to_sym
538
+ @app_properties[norm_name] = v
539
+ end
540
+ nil
541
+ end
542
+
543
+ # Replaces application document properties.
544
+ #
545
+ # @param props [Hash, Elements::AppProperties]
546
+ # @return [void]
547
+ # @api public
548
+ #: (Hash[Symbol, untyped] | Elements::AppProperties props) -> void
549
+ def app_properties=(props)
550
+ h = props.respond_to?(:to_h) ? props.to_h : (props || {})
551
+ @app_properties = {}
552
+ app_properties(**h)
553
+ nil
630
554
  end
631
555
 
632
556
  # Sets multiple core, app, and/or custom properties simultaneously.
@@ -638,8 +562,8 @@ module Xlsxrb
638
562
  # @api public
639
563
  #: (?core: Hash[Symbol, String | Integer | Time]?, ?app: Hash[Symbol, String | Integer | Time]?, ?custom: Hash[String | Symbol, untyped]?) -> void
640
564
  def properties(core: nil, app: nil, custom: nil)
641
- core&.each { |k, v| core_property(k, v) }
642
- app&.each { |k, v| app_property(k, v) }
565
+ core_properties(**core) if core
566
+ app_properties(**app) if app
643
567
  custom&.each { |k, v| custom_property(k.to_s, v) }
644
568
  end
645
569
 
@@ -684,8 +608,8 @@ module Xlsxrb
684
608
 
685
609
  # Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined
686
610
  @sheet_builders.each do |sb|
687
- 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 }
688
- print_titles(rows: sb.print_titles[:rows], columns: sb.print_titles[:columns] || sb.print_titles[:cols], sheet: sb.name) if sb.print_titles && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sb.name }
611
+ 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 }
612
+ 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
613
  sb.defined_names&.each do |dn|
690
614
  @defined_names << dn
691
615
  end
@@ -721,7 +645,7 @@ module Xlsxrb
721
645
  end
722
646
 
723
647
  @workbook_properties[:active_sheet] = active_tab_idx unless active_tab_idx.nil?
724
- @workbook_properties[:active_tab] = active_tab_idx unless active_tab_idx.nil?
648
+ @workbook_properties[:active_sheet_index] = active_tab_idx unless active_tab_idx.nil?
725
649
  @workbook_properties[:first_sheet] = first_sheet_idx unless first_sheet_idx.nil?
726
650
  @workbook_properties[:show_sheet_tabs] = @show_sheet_tabs unless @show_sheet_tabs.nil?
727
651
  @workbook_properties[:tab_ratio] = @tab_ratio unless @tab_ratio.nil?
@@ -746,6 +670,17 @@ module Xlsxrb
746
670
 
747
671
  private
748
672
 
673
+ #: (String? range, ?sheet: String?) -> String?
674
+ def set_print_area(range, sheet: nil)
675
+ sheet_name = sheet || @sheets.last&.name || "Sheet1"
676
+ @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
677
+ if range
678
+ value = "'#{sheet_name}'!#{absolute_range(range)}"
679
+ defined_name("_xlnm.Print_Area", value, sheet: sheet_name, scope: sheet_name)
680
+ end
681
+ range&.to_s
682
+ end
683
+
749
684
  #: (untyped range) -> untyped
750
685
  def absolute_range(range)
751
686
  DslHelpers.absolute_range(range)
@@ -790,7 +725,7 @@ module Xlsxrb
790
725
  end
791
726
 
792
727
  sheet.rows.each do |row|
793
- raw_row_style = row.unmapped_data&.[](:style_index)
728
+ raw_row_style = row.style_index || row.unmapped_data&.[](:style_index)
794
729
  next unless raw_row_style.is_a?(Hash)
795
730
 
796
731
  inline_name = "__inline_row_#{raw_row_style.hash}"
@@ -800,6 +735,32 @@ module Xlsxrb
800
735
  sb.apply_options!(**raw_row_style)
801
736
  all_style_builders[inline_name] = sb
802
737
  end
738
+
739
+ # Style names that were never defined are a mistake, not an unstyled cell.
740
+ sheet.columns.each { |col| DslHelpers.check_style_name!(col.style_index, all_style_builders) }
741
+ sheet.rows.each do |row|
742
+ DslHelpers.check_style_name!(row.style_index || row.unmapped_data&.[](:style_index), all_style_builders)
743
+ row.cells.each { |cell| DslHelpers.check_style_name!(cell.style, all_style_builders) }
744
+ end
745
+ end
746
+
747
+ # Date and Time cells styled without a number format get a variant of their style
748
+ # that keeps the default date or time format.
749
+ date_variants = {}
750
+ sheets.each do |sheet|
751
+ sheet.rows.each do |row|
752
+ row.cells.each do |cell|
753
+ style_name = cell.style
754
+ next unless style_name.is_a?(String) || style_name.is_a?(Symbol)
755
+
756
+ variant = DslHelpers.date_style_variant(cell.value, style_name.to_s, all_style_builders[style_name.to_s])
757
+ next unless variant
758
+
759
+ variant_name, number_format = variant
760
+ date_variants[[style_name.to_s, cell.value.is_a?(Time) || cell.value.is_a?(DateTime)]] = variant
761
+ all_style_builders[variant_name] ||= all_style_builders[style_name.to_s].with_number_format(variant_name, number_format)
762
+ end
763
+ end
803
764
  end
804
765
 
805
766
  if all_style_builders.empty?
@@ -855,8 +816,10 @@ module Xlsxrb
855
816
  Elements::Column.new(
856
817
  index: col.index,
857
818
  width: col.width,
858
- hidden: col.hidden,
859
- custom_width: col.custom_width,
819
+ hidden: col.hidden?,
820
+ custom_width: col.custom_width?,
821
+ best_fit: col.best_fit?,
822
+ collapsed: col.collapsed?,
860
823
  outline_level: col.outline_level,
861
824
  style_index: resolved_col_style,
862
825
  unmapped_data: unmapped,
@@ -866,7 +829,7 @@ module Xlsxrb
866
829
  end
867
830
 
868
831
  new_rows = sheet.rows.map do |row|
869
- raw_row_style = row.unmapped_data&.[](:style_index)
832
+ raw_row_style = row.style_index || row.unmapped_data&.[](:style_index)
870
833
  resolved_row_style = if raw_row_style.is_a?(Hash)
871
834
  inline_name = "__inline_row_#{raw_row_style.hash}"
872
835
  style_name_to_id[inline_name]
@@ -876,13 +839,16 @@ module Xlsxrb
876
839
  raw_row_style
877
840
  end
878
841
  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
842
+ if row_unmapped&.key?(:style_index)
843
+ row_unmapped = row_unmapped.dup
844
+ row_unmapped.delete(:style_index)
882
845
  end
883
846
 
884
847
  new_cells = row.cells.map do |cell|
885
848
  raw_cell_style = cell.style || cell.style_index
849
+ dated = (cell.value.is_a?(Date) || cell.value.is_a?(Time)) && (raw_cell_style.is_a?(String) || raw_cell_style.is_a?(Symbol))
850
+ date_variant = dated ? date_variants[[raw_cell_style.to_s, cell.value.is_a?(Time) || cell.value.is_a?(DateTime)]] : nil
851
+ raw_cell_style = date_variant.first if date_variant
886
852
  resolved_cell_style = if (raw_cell_style.is_a?(String) || raw_cell_style.is_a?(Symbol)) && style_name_to_id.key?(raw_cell_style.to_s)
887
853
  style_name_to_id[raw_cell_style.to_s]
888
854
  elsif raw_cell_style.nil?
@@ -891,7 +857,7 @@ module Xlsxrb
891
857
  raw_cell_style
892
858
  end
893
859
 
894
- target_fmt = cell.format_code || col_fmt_map[cell.column_index]
860
+ target_fmt = cell.format_code || date_variant&.last || col_fmt_map[cell.column_index]
895
861
 
896
862
  if resolved_cell_style == (cell.style || cell.style_index) && target_fmt == cell.format_code
897
863
  cell