xlsxrb 0.1.17 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -1
  3. data/README.md +79 -19
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +31 -61
  5. data/lib/xlsxrb/chart_builder.rb +595 -113
  6. data/lib/xlsxrb/dsl_helpers.rb +72 -20
  7. data/lib/xlsxrb/elements/cell.rb +243 -56
  8. data/lib/xlsxrb/elements/column.rb +94 -5
  9. data/lib/xlsxrb/elements/coordinate_access.rb +108 -10
  10. data/lib/xlsxrb/elements/image.rb +81 -15
  11. data/lib/xlsxrb/elements/metadata.rb +2733 -0
  12. data/lib/xlsxrb/elements/row.rb +130 -46
  13. data/lib/xlsxrb/elements/styles.rb +184 -76
  14. data/lib/xlsxrb/elements/types.rb +242 -7
  15. data/lib/xlsxrb/elements/workbook.rb +171 -82
  16. data/lib/xlsxrb/elements/worksheet.rb +598 -102
  17. data/lib/xlsxrb/elements.rb +1 -0
  18. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +12 -4
  19. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -2
  20. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +264 -17
  21. data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
  22. data/lib/xlsxrb/ooxml/reader.rb +4 -0
  23. data/lib/xlsxrb/ooxml/styles_parser.rb +10 -3
  24. data/lib/xlsxrb/ooxml/workbook_writer.rb +90 -36
  25. data/lib/xlsxrb/ooxml/worksheet_parser.rb +2 -2
  26. data/lib/xlsxrb/ooxml/worksheet_writer.rb +64 -19
  27. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +5 -2
  28. data/lib/xlsxrb/ooxml/writer/features_xml.rb +26 -5
  29. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +19 -9
  30. data/lib/xlsxrb/ooxml/writer.rb +22 -6
  31. data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
  32. data/lib/xlsxrb/ooxml/zip_reader.rb +47 -10
  33. data/lib/xlsxrb/stream_row.rb +97 -18
  34. data/lib/xlsxrb/stream_sheet.rb +706 -190
  35. data/lib/xlsxrb/stream_writer.rb +1893 -374
  36. data/lib/xlsxrb/style_builder.rb +774 -111
  37. data/lib/xlsxrb/utils.rb +66 -106
  38. data/lib/xlsxrb/version.rb +1 -1
  39. data/lib/xlsxrb/workbook_builder.rb +380 -83
  40. data/lib/xlsxrb/worksheet_builder.rb +1210 -233
  41. data/lib/xlsxrb.rb +1266 -326
  42. data/sig/generated/xlsxrb/chart_builder.rbs +319 -90
  43. data/sig/generated/xlsxrb/dsl_helpers.rbs +20 -2
  44. data/sig/generated/xlsxrb/elements/cell.rbs +92 -20
  45. data/sig/generated/xlsxrb/elements/column.rbs +61 -2
  46. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +49 -7
  47. data/sig/generated/xlsxrb/elements/image.rbs +30 -8
  48. data/sig/generated/xlsxrb/elements/metadata.rbs +1587 -0
  49. data/sig/generated/xlsxrb/elements/row.rbs +61 -21
  50. data/sig/generated/xlsxrb/elements/styles.rbs +96 -58
  51. data/sig/generated/xlsxrb/elements/types.rbs +27 -0
  52. data/sig/generated/xlsxrb/elements/workbook.rbs +63 -52
  53. data/sig/generated/xlsxrb/elements/worksheet.rbs +192 -51
  54. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -0
  55. data/sig/generated/xlsxrb/ooxml/reader.rbs +3 -0
  56. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +1 -1
  57. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +1 -1
  58. data/sig/generated/xlsxrb/ooxml/writer.rbs +3 -0
  59. data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
  60. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +2 -2
  61. data/sig/generated/xlsxrb/stream_row.rbs +48 -14
  62. data/sig/generated/xlsxrb/stream_sheet.rbs +324 -115
  63. data/sig/generated/xlsxrb/stream_writer.rbs +1068 -255
  64. data/sig/generated/xlsxrb/style_builder.rbs +389 -79
  65. data/sig/generated/xlsxrb/utils.rbs +34 -18
  66. data/sig/generated/xlsxrb/workbook_builder.rbs +234 -34
  67. data/sig/generated/xlsxrb/worksheet_builder.rbs +628 -126
  68. data/sig/generated/xlsxrb.rbs +257 -49
  69. metadata +3 -1
@@ -19,14 +19,17 @@ module Xlsxrb
19
19
  # @api public
20
20
  class WorkbookBuilder
21
21
  # @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.
22
23
  # @param default_font [Hash, nil] Optional default font options (e.g. `{ name: "Arial", size: 12 }`).
23
- #: (?strict_excel_mode: bool, ?default_font: untyped?) -> void
24
- def initialize(strict_excel_mode: true, default_font: nil)
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)
25
26
  @strict_excel_mode = strict_excel_mode
27
+ @escape_formulas = escape_formulas ? true : false
26
28
  @default_font = default_font
27
29
  @sheets = []
28
30
  @sheet_builders = [] # Keep track of sheet builders for style processing
29
31
  @defined_names = []
32
+ @styles = {}
30
33
  @core_properties = {}
31
34
  @app_properties = {}
32
35
  @custom_properties = []
@@ -38,6 +41,19 @@ module Xlsxrb
38
41
  @tab_ratio = nil
39
42
  end
40
43
 
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
+
41
57
  # Sets or configures the workbook-wide default font.
42
58
  #
43
59
  # @param name [String, Hash, nil] Font name or font options Hash.
@@ -56,84 +72,211 @@ module Xlsxrb
56
72
  existing[:size] = size if size
57
73
  @default_font = existing.merge(opts)
58
74
  end
75
+ # @deprecated Use {#default_font} instead. Will be removed in 0.2.0.
59
76
  alias set_default_font default_font
60
77
 
78
+ # @return [Hash{String => StyleBuilder}]
79
+ #: Hash[String, StyleBuilder]
80
+ attr_reader :styles
81
+
82
+ # Defines or configures a named cell style at the workbook level.
83
+ #
84
+ # @example
85
+ # workbook.style(:header, bold: true, fill_color: "4F81BD", font_color: "FFFFFF")
86
+ #
87
+ # @param name [String, Symbol] The name of the style.
88
+ # @param opts [Hash] Style options (e.g. bold: true, fill_color: "FF0000").
89
+ # @yield [style_builder]
90
+ # @yieldparam style_builder [Xlsxrb::StyleBuilder]
91
+ # @return [StyleBuilder]
92
+ # @api public
93
+ #: (String | Symbol name, **untyped opts) ?{ (StyleBuilder) -> void } -> StyleBuilder
94
+ def style(name, **opts)
95
+ style_name = name.to_s
96
+ style_builder = StyleBuilder.new(style_name)
97
+ style_builder.apply_options!(**opts) unless opts.empty?
98
+ yield style_builder if block_given?
99
+ @styles[style_name] = style_builder
100
+ end
101
+
102
+ # Creates an {Elements::Formula} object.
103
+ #
104
+ # @param expression [String] The formula expression without leading '='.
105
+ # @param cached_value [Object, nil] Optional precomputed value.
106
+ # @param type [Symbol, String, nil] Formula type (:shared, :array, etc.).
107
+ # @param ref [String, nil] Target cell or range reference.
108
+ # @param shared_index [Integer, nil] Shared formula group index (0-based).
109
+ # @return [Elements::Formula]
110
+ # @api public
111
+ #: (String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?) -> Elements::Formula
112
+ def formula(...)
113
+ Xlsxrb.formula(...)
114
+ end
115
+
116
+ # Creates an array or dynamic array {Elements::Formula} object.
117
+ #
118
+ # @param expression [String] The formula expression without leading '='.
119
+ # @param ref [String, nil] Target cell or range reference (keyword).
120
+ # @param cached_value [Object, nil] Optional precomputed value.
121
+ # @return [Elements::Formula]
122
+ # @api public
123
+ #: (String expression, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) -> Elements::Formula
124
+ def array_formula(...)
125
+ Xlsxrb.array_formula(...)
126
+ end
127
+
128
+ # Creates a shared formula {Elements::Formula} object.
129
+ #
130
+ # @param expression [String, nil] The formula expression.
131
+ # @param shared_index [Integer, nil] Shared formula group index (0-based).
132
+ # @param ref [String, nil] Target range for master cell.
133
+ # @param cached_value [Object, nil] Optional precomputed value.
134
+ # @return [Elements::Formula]
135
+ # @api public
136
+ #: (?String? expression, ?shared_index: Integer?, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil)) -> Elements::Formula
137
+ def shared_formula(...)
138
+ Xlsxrb.shared_formula(...)
139
+ end
140
+
141
+ # Creates an {Elements::RichText} object.
142
+ #
143
+ # @param runs [Array] Text runs or styling options.
144
+ # @return [Elements::RichText]
145
+ # @api public
146
+ #: (*(Hash[Symbol, untyped] | Elements::RichTextRun | String | Array[untyped] | untyped) runs, ?text: String?, ?base_font: untyped, **untyped font_props) -> Elements::RichText
147
+ def rich_text(...)
148
+ Xlsxrb.rich_text(...)
149
+ end
150
+
151
+ # Creates or returns an {Elements::CellError} object.
152
+ #
153
+ # @param code [String, Symbol, Elements::CellError] Error code or symbol.
154
+ # @return [Elements::CellError]
155
+ # @api public
156
+ #: (String | Symbol | Elements::CellError code) -> Elements::CellError
157
+ def cell_error(...)
158
+ Xlsxrb.cell_error(...)
159
+ end
160
+
161
+ OMITTED = Object.new.freeze
162
+ private_constant :OMITTED
163
+
61
164
  # Gets or sets the active worksheet tab in the workbook window.
62
165
  #
166
+ # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#active_sheet=} instead.
63
167
  # @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
64
168
  # @return [String, Symbol, Integer, nil]
65
169
  # @api public
66
170
  #: (?String | Symbol | Integer | nil name_or_index) -> (String | Symbol | Integer | nil)
67
- def active_sheet(name_or_index = nil)
68
- if name_or_index
69
- @active_sheet = name_or_index
70
- else
171
+ def active_sheet(name_or_index = OMITTED)
172
+ if name_or_index.equal?(OMITTED)
71
173
  @active_sheet
174
+ else
175
+ @active_sheet = name_or_index
72
176
  end
73
177
  end
74
- alias active_sheet= active_sheet
178
+ # @deprecated Use {#active_sheet} instead. Will be removed in 0.2.0.
75
179
  alias active_tab active_sheet
76
- 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
+ # @return [String, Symbol, Integer, nil]
185
+ # @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=
77
189
 
78
190
  # Gets or sets the first sheet tab visible in the sheet tab scroll area.
79
191
  #
192
+ # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#first_sheet=} instead.
80
193
  # @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
81
194
  # @return [String, Symbol, Integer, nil]
82
195
  # @api public
83
196
  #: (?String | Symbol | Integer | nil name_or_index) -> (String | Symbol | Integer | nil)
84
- def first_sheet(name_or_index = nil)
85
- if name_or_index
86
- @first_sheet = name_or_index
87
- else
197
+ def first_sheet(name_or_index = OMITTED)
198
+ if name_or_index.equal?(OMITTED)
88
199
  @first_sheet
200
+ else
201
+ @first_sheet = name_or_index
89
202
  end
90
203
  end
91
- alias first_sheet= first_sheet
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)
92
211
 
93
212
  # Gets or sets whether sheet tabs are visible in the workbook window.
94
213
  #
214
+ # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#show_sheet_tabs=} instead.
95
215
  # @param visible [Boolean, nil]
96
216
  # @return [Boolean, nil]
97
217
  # @api public
98
218
  #: (?bool? visible) -> bool?
99
- def show_sheet_tabs(visible = nil)
100
- if visible.nil?
219
+ def show_sheet_tabs(visible = OMITTED)
220
+ if visible.equal?(OMITTED)
101
221
  @show_sheet_tabs
102
222
  else
103
223
  @show_sheet_tabs = visible
104
224
  end
105
225
  end
106
- alias show_sheet_tabs= show_sheet_tabs
226
+
227
+ # Sets whether sheet tabs are visible in the workbook window.
228
+ #
229
+ # @param visible [Boolean, nil]
230
+ # @return [Boolean, nil]
231
+ # @api public
232
+ attr_writer :show_sheet_tabs #: bool?
107
233
 
108
234
  # Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
109
235
  #
236
+ # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#tab_ratio=} instead.
110
237
  # @param ratio [Integer, nil]
111
238
  # @return [Integer, nil]
112
239
  # @api public
113
240
  #: (?Integer? ratio) -> Integer?
114
- def tab_ratio(ratio = nil)
115
- if ratio
116
- @tab_ratio = ratio
117
- else
241
+ def tab_ratio(ratio = OMITTED)
242
+ if ratio.equal?(OMITTED)
118
243
  @tab_ratio
244
+ else
245
+ @tab_ratio = ratio
119
246
  end
120
247
  end
121
- alias tab_ratio= tab_ratio
248
+
249
+ # Sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
250
+ #
251
+ # @param ratio [Integer, nil]
252
+ # @return [Integer, nil]
253
+ # @api public
254
+ attr_writer :tab_ratio #: Integer?
122
255
 
123
256
  # Gets or sets the internal code name of the workbook.
124
257
  #
258
+ # @deprecated Calling with arguments is deprecated and will be removed in 0.2.0. Use {#code_name=} instead.
125
259
  # @param name [String, Symbol, nil] The code name.
126
260
  # @return [String, nil]
127
261
  # @api public
128
262
  #: (?String | Symbol | nil name) -> String?
129
- def code_name(name = nil)
130
- if name
131
- @workbook_properties[:code_name] = name.to_s
132
- else
263
+ def code_name(name = OMITTED)
264
+ if name.equal?(OMITTED)
133
265
  @workbook_properties[:code_name]
266
+ else
267
+ @workbook_properties[:code_name] = name&.to_s
134
268
  end
135
269
  end
136
- alias code_name= code_name
270
+
271
+ # Sets the internal code name of the workbook.
272
+ #
273
+ # @param name [String, Symbol, nil] The code name.
274
+ # @return [String, nil]
275
+ # @api public
276
+ #: (String | Symbol | nil name) -> String?
277
+ def code_name=(name)
278
+ @workbook_properties[:code_name] = name&.to_s
279
+ end
137
280
 
138
281
  # Sets a workbook-level property.
139
282
  #
@@ -145,9 +288,29 @@ module Xlsxrb
145
288
  # @param value [String, Integer, Boolean] Property value.
146
289
  # @return [void]
147
290
  # @api public
148
- #: (Symbol name, String | Integer | bool value) -> (String | Integer | bool)
291
+ #: (Symbol name, String | Integer | bool value) -> void
149
292
  def workbook_property(name, value)
150
293
  @workbook_properties[name] = value
294
+ nil
295
+ end
296
+
297
+ # Returns workbook-level properties.
298
+ #
299
+ # @return [Elements::WorkbookProperties]
300
+ # @api public
301
+ #: () -> Elements::WorkbookProperties
302
+ def workbook_properties
303
+ Elements::WorkbookProperties.new(**@workbook_properties)
304
+ end
305
+
306
+ # Sets workbook-level properties.
307
+ #
308
+ # @param props [Hash, Elements::MetadataRecord]
309
+ # @return [void]
310
+ # @api public
311
+ #: (untyped props) -> void
312
+ def workbook_properties=(props)
313
+ @workbook_properties = props.is_a?(Elements::MetadataRecord) ? props.to_h : props
151
314
  end
152
315
 
153
316
  # Returns the list of worksheet names added to this workbook so far.
@@ -162,18 +325,22 @@ module Xlsxrb
162
325
  # Adds a new worksheet to the workbook.
163
326
  #
164
327
  # @param name [String, nil] Sheet name (max 31 chars, no forbidden chars `[ ] * ? / \ :`).
165
- # @param opts [Hash] Sheet properties.
328
+ # @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.
332
+ # @param opts [Hash] Additional sheet properties.
166
333
  # @yield [sheet_builder]
167
334
  # @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder]
168
335
  # @return [Elements::Worksheet]
169
336
  # @raise [ArgumentError] If sheet name violates Excel limits in strict mode.
170
337
  # @api public
171
- #: (?String? name, **untyped opts) ?{ (WorksheetBuilder) -> void } -> untyped
172
- def sheet(name = nil, **opts)
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)
173
340
  sheet_count = [@sheets.size, @sheet_builders.size].max
174
341
  name ||= "Sheet#{sheet_count + 1}"
175
342
  raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name.length > 31
176
- raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet::FORBIDDEN_NAME_CHARS)
343
+ raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars)
177
344
  raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'"))
178
345
  raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History")
179
346
 
@@ -181,9 +348,12 @@ module Xlsxrb
181
348
  raise ArgumentError, "Sheet name '#{name}' is already used. Excel requires unique sheet names." if @strict_excel_mode && existing_names.include?(name.downcase)
182
349
 
183
350
  active = opts.delete(:active)
184
- state = opts.delete(:state)
185
- hidden = opts.delete(:hidden)
186
- code_name = opts.delete(:code_name)
351
+ state = opts.delete(:state) if opts.key?(:state)
352
+ hidden = opts.delete(:hidden) if opts.key?(:hidden)
353
+ code_name = opts.delete(:code_name) if opts.key?(:code_name)
354
+ 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?
187
357
  state ||= :hidden if hidden
188
358
  state ||= :visible
189
359
  state = state.to_sym
@@ -191,80 +361,151 @@ module Xlsxrb
191
361
 
192
362
  @active_sheet = name if active
193
363
 
194
- sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state)
195
- sheet_builder.code_name(code_name) if code_name
364
+ sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state, escape_formulas: @escape_formulas)
365
+ sheet_builder.code_name = code_name if code_name
366
+ sheet_builder.tab_color = tab_color if tab_color
196
367
  opts.each { |k, v| sheet_builder.sheet_properties(k, v) }
197
368
  @sheet_builders << sheet_builder
198
369
  yield sheet_builder if block_given?
199
370
  @defined_names.concat(sheet_builder.defined_names)
200
371
  sheet_builder.defined_names.clear
201
- @sheets << sheet_builder.build
372
+ built = sheet_builder.build
373
+ @sheets << built
374
+ built
375
+ end
376
+
377
+ # Accesses or defines a worksheet by name.
378
+ #
379
+ # @param name_or_index [String, Symbol, Integer, nil]
380
+ # @yield [sheet_builder]
381
+ # @return [Elements::Worksheet]
382
+ # @api public
383
+ #: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } -> Elements::Worksheet
384
+ def [](name_or_index = nil, &)
385
+ sheet(name_or_index&.to_s, &)
202
386
  end
203
- alias [] sheet
204
387
 
205
388
  # --- Workbook-Level Methods ---
206
389
 
207
- # Adds a defined name (named range or formula constant) to the workbook.
208
- #
209
- # @param name [String] Defined name (e.g. "TaxRate").
210
- # @param value [String] Formula expression or range reference (e.g. "Sheet1!$B$2").
211
390
  # Adds a defined name (named range or formula) to the workbook.
212
391
  # Bare cell references and coordinate keywords are automatically formatted with sheet quotes and absolute $ references.
213
392
  #
214
393
  # @param name [String] Defined name identifier.
215
394
  # @param value [Object, nil] Value or formula expression.
216
- # @param sheet [String, nil] Optional sheet name for sheet-scoped defined names.
395
+ # @param sheet [String, nil] Reference sheet name for coordinates/formula.
396
+ # @param scope [String, Symbol, nil] Optional sheet name or :workbook for scope (defaults to workbook).
217
397
  # @param hidden [Boolean] Whether the defined name is hidden from Excel's UI.
218
- # @param col [Integer, String, nil] Column index or letter for coordinate-based reference.
398
+ # @param column [Integer, String, nil] Deprecated alias for columns.
399
+ # @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.
219
402
  # @param rows [Integer, Range, nil] Row or row range (1-based) for coordinate-based reference.
220
403
  # @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.
221
406
  # @return [void]
222
407
  # @api public
223
- #: (String name, ?untyped value, ?sheet: String?, ?hidden: bool, ?col: untyped, ?rows: untyped, ?range: untyped, ?ref_sheet: String?, ?local: bool) -> void
224
- def defined_name(name, value = nil, sheet: nil, hidden: false, col: nil, rows: nil, range: nil, ref_sheet: nil, local: false)
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?
225
415
  target_ref_sheet = ref_sheet || sheet
226
- normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: target_ref_sheet, col: col, rows: rows, range: range)
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)
227
419
  entry = { name: name, value: normalized_val, hidden: hidden }
228
- local_sheet = sheet if sheet || local
420
+ local_sheet = if scope
421
+ scope == :workbook ? nil : scope.to_s
422
+ elsif sheet || local || ref_sheet
423
+ sheet
424
+ end
229
425
  entry[:local_sheet_name] = local_sheet if local_sheet
230
426
  @defined_names << entry
231
427
  end
232
428
 
233
- # Sets the print area for a sheet.
429
+ # Returns workbook-level defined names.
430
+ #
431
+ # @return [Array<Elements::DefinedName>]
432
+ # @api public
433
+ #: () -> Array[Elements::DefinedName]
434
+ def defined_names
435
+ @defined_names.map { |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) }
436
+ end
437
+
438
+ # Sets or gets the print area for a sheet.
234
439
  #
235
- # @param range [String] Cell range (e.g. "A1:G50").
440
+ # @param range [String, Object, nil] Cell range (e.g. "A1:G50").
236
441
  # @param sheet [String, nil] Target sheet name (defaults to latest or "Sheet1").
237
- # @return [void]
442
+ # @return [String, nil, void]
238
443
  # @api public
239
- #: (String range, ?sheet: String?) -> void
240
- def print_area(range, sheet: nil)
444
+ #: (?untyped range, ?sheet: String?) -> (String | nil | void)
445
+ def print_area(range = OMITTED, sheet: nil)
241
446
  sheet_name = sheet || @sheets.last&.name || "Sheet1"
242
- value = "'#{sheet_name}'!#{absolute_range(range)}"
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
+
243
452
  @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
244
- defined_name("_xlnm.Print_Area", value, sheet: 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
458
+ end
459
+
460
+ # Sets the print area for the worksheet.
461
+ #
462
+ # @param range [String, nil] Cell range (e.g. "A1:G50").
463
+ # @return [String, nil]
464
+ # @api public
465
+ #: (String? range) -> String?
466
+ def print_area=(range)
467
+ print_area(range)
468
+ range
245
469
  end
246
470
 
247
471
  # Sets repeating print titles (rows and/or columns) for pagination.
248
472
  #
249
473
  # @param rows [String, nil] Repeating row range (e.g. "1:2").
250
- # @param cols [String, nil] Repeating column range (e.g. "A:B").
474
+ # @param columns [String, nil] Repeating column range (e.g. "A:B").
475
+ # @param cols [String, nil] Deprecated alias for columns.
251
476
  # @param sheet [String, nil] Target sheet name.
252
477
  # @return [void]
253
478
  # @api public
254
- #: (?rows: String?, ?cols: String?, ?sheet: String?) -> void
255
- def print_titles(rows: nil, cols: nil, sheet: nil)
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)
256
484
  sheet_name = sheet || @sheets.last&.name || "Sheet1"
257
485
  existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
258
- if existing && (rows.nil? || cols.nil?)
259
- cols = Regexp.last_match(1).delete("$") if cols.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
260
- rows = Regexp.last_match(1).delete("$") if rows.nil? && existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
486
+ if existing && (r.nil? || c.nil?)
487
+ c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
488
+ r = Regexp.last_match(1).delete("$") if r.nil? && existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
261
489
  end
262
490
  parts = []
263
- parts << "'#{sheet_name}'!$#{cols.sub(":", ":$")}" if cols
264
- parts << "'#{sheet_name}'!$#{rows.sub(":", ":$")}" if rows
491
+ parts << "'#{sheet_name}'!$#{c.sub(":", ":$")}" if c
492
+ parts << "'#{sheet_name}'!$#{r.sub(":", ":$")}" if r
265
493
  value = parts.join(",")
266
494
  @defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
267
495
  defined_name("_xlnm.Print_Titles", value, sheet: sheet_name)
496
+ nil
497
+ end
498
+
499
+ # Sets repeating print titles.
500
+ #
501
+ # @param opts [Elements::PrintTitles, Hash] Options with :rows and/or :columns.
502
+ # @return [Elements::PrintTitles, Hash]
503
+ # @api public
504
+ #: (Elements::PrintTitles | Hash[Symbol, untyped] opts) -> (Elements::PrintTitles | Hash[Symbol, untyped])
505
+ def print_titles=(opts)
506
+ h = opts.respond_to?(:to_h) ? opts.to_h : opts
507
+ print_titles(**h)
508
+ opts
268
509
  end
269
510
 
270
511
  # Sets repeating title rows for pagination on the target or active sheet.
@@ -303,9 +544,9 @@ module Xlsxrb
303
544
  cols_str = if first.is_a?(String)
304
545
  first.include?(":") ? first : "#{first}:#{last || first}"
305
546
  else
306
- first_name = Utils.col_index_to_name(first)
547
+ first_name = Elements::Cell._column_letter(first)
307
548
  last_name = if last.is_a?(Integer)
308
- Utils.col_index_to_name(last)
549
+ Elements::Cell._column_letter(last)
309
550
  elsif last
310
551
  last.to_s
311
552
  else
@@ -313,17 +554,39 @@ module Xlsxrb
313
554
  end
314
555
  "#{first_name}:#{last_name}"
315
556
  end
316
- print_titles(cols: cols_str, sheet: sheet)
557
+ print_titles(columns: cols_str, sheet: sheet)
317
558
  end
318
559
 
319
- # Sets workbook structure and window protection.
560
+ # Sets or returns workbook structure and window protection.
320
561
  #
321
562
  # @param opts [Hash] Protection options (e.g. lock_structure: true, password: "secret").
563
+ # @return [Elements::WorkbookProtection, nil, void]
564
+ # @api public
565
+ #: (**String | Integer | bool | nil opts) -> (Elements::WorkbookProtection | nil)
566
+ def workbook_protection(**opts)
567
+ if opts.empty?
568
+ @workbook_protection ? Elements::WorkbookProtection.new(**@workbook_protection) : nil
569
+ else
570
+ @workbook_protection = opts
571
+ nil
572
+ end
573
+ end
574
+
575
+ # Sets workbook protection settings.
576
+ #
577
+ # @param opts [Hash, Elements::MetadataRecord, nil]
322
578
  # @return [void]
323
579
  # @api public
580
+ #: (untyped opts) -> void
581
+ def workbook_protection=(opts)
582
+ @workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : opts
583
+ end
584
+
585
+ # @deprecated Use {#workbook_protection} instead.
324
586
  #: (**String | Integer | bool | nil opts) -> void
325
- def protect_workbook(**opts)
326
- @workbook_protection = opts
587
+ def protect_workbook(...)
588
+ warn "[DEPRECATION] `protect_workbook` is deprecated; use `workbook_protection` instead.", uplevel: 1
589
+ workbook_protection(...)
327
590
  end
328
591
 
329
592
  # Sets a Dublin Core document metadata property.
@@ -337,6 +600,15 @@ module Xlsxrb
337
600
  @core_properties[name] = value
338
601
  end
339
602
 
603
+ # Returns core document properties.
604
+ #
605
+ # @return [Elements::CoreProperties]
606
+ # @api public
607
+ #: () -> Elements::CoreProperties
608
+ def core_properties
609
+ Elements::CoreProperties.new(**@core_properties)
610
+ end
611
+
340
612
  # Sets an extended application metadata property.
341
613
  #
342
614
  # @param name [Symbol] Application property name (:company, :manager, :app_version, etc.).
@@ -348,6 +620,15 @@ module Xlsxrb
348
620
  @app_properties[name] = value
349
621
  end
350
622
 
623
+ # Returns application document properties.
624
+ #
625
+ # @return [Elements::AppProperties]
626
+ # @api public
627
+ #: () -> Elements::AppProperties
628
+ def app_properties
629
+ Elements::AppProperties.new(**@app_properties)
630
+ end
631
+
351
632
  # Sets multiple core, app, and/or custom properties simultaneously.
352
633
  #
353
634
  # @param core [Hash{Symbol => Object}, nil] Core properties map.
@@ -374,6 +655,18 @@ module Xlsxrb
374
655
  @custom_properties << { name: name, value: value, type: type }
375
656
  end
376
657
 
658
+ # Returns custom document properties.
659
+ #
660
+ # @return [Hash<String, Elements::CustomProperty>]
661
+ # @api public
662
+ #: () -> Hash[String, Elements::CustomProperty]
663
+ def custom_properties
664
+ @custom_properties.to_h do |cp|
665
+ prop = cp.is_a?(Elements::CustomProperty) ? cp : Elements::CustomProperty.new(name: (cp[:name] || cp["name"]).to_s, value: cp[:value] || cp["value"], type: cp[:type] || cp["type"] || :string)
666
+ [prop.name.to_s, prop]
667
+ end.freeze
668
+ end
669
+
377
670
  # Builds and returns the compiled {Elements::Workbook}.
378
671
  #
379
672
  # @return [Elements::Workbook]
@@ -392,7 +685,7 @@ module Xlsxrb
392
685
  # Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined
393
686
  @sheet_builders.each do |sb|
394
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 }
395
- print_titles(rows: sb.print_titles[:rows], cols: 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 }
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 }
396
689
  sb.defined_names&.each do |dn|
397
690
  @defined_names << dn
398
691
  end
@@ -427,6 +720,7 @@ module Xlsxrb
427
720
  idx || @first_sheet
428
721
  end
429
722
 
723
+ @workbook_properties[:active_sheet] = active_tab_idx unless active_tab_idx.nil?
430
724
  @workbook_properties[:active_tab] = active_tab_idx unless active_tab_idx.nil?
431
725
  @workbook_properties[:first_sheet] = first_sheet_idx unless first_sheet_idx.nil?
432
726
  @workbook_properties[:show_sheet_tabs] = @show_sheet_tabs unless @show_sheet_tabs.nil?
@@ -473,8 +767,8 @@ module Xlsxrb
473
767
 
474
768
  #: (untyped sheets) -> (::Array[untyped | ::Hash[untyped, untyped]] | ::Array[untyped])
475
769
  def process_styles(sheets)
476
- # Collect all unique StyleBuilders from all sheets
477
- all_style_builders = {}
770
+ # Collect all unique StyleBuilders from workbook and sheets
771
+ all_style_builders = @styles.dup
478
772
  @sheet_builders.each do |sheet_builder|
479
773
  sheet_builder.styles.each do |style_name, style_builder|
480
774
  all_style_builders[style_name.to_s] = style_builder
@@ -537,14 +831,14 @@ module Xlsxrb
537
831
  resolved_col_style = if raw_col_style.is_a?(Hash)
538
832
  inline_name = "__inline_col_#{raw_col_style.hash}"
539
833
  sb = all_style_builders[inline_name]
540
- if sb&.num_fmt_id
541
- col_fmt_map[col.index] = sb.num_fmt_id.is_a?(String) ? sb.num_fmt_id : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.num_fmt_id]
834
+ if sb&.number_format
835
+ col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
542
836
  end
543
837
  style_name_to_id[inline_name]
544
838
  elsif (raw_col_style.is_a?(String) || raw_col_style.is_a?(Symbol)) && style_name_to_id.key?(raw_col_style.to_s)
545
839
  sb = all_style_builders[raw_col_style.to_s]
546
- if sb&.num_fmt_id
547
- col_fmt_map[col.index] = sb.num_fmt_id.is_a?(String) ? sb.num_fmt_id : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.num_fmt_id]
840
+ if sb&.number_format
841
+ col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
548
842
  end
549
843
  style_name_to_id[raw_col_style.to_s]
550
844
  else
@@ -588,7 +882,7 @@ module Xlsxrb
588
882
  end
589
883
 
590
884
  new_cells = row.cells.map do |cell|
591
- raw_cell_style = cell.style_index
885
+ raw_cell_style = cell.style || cell.style_index
592
886
  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)
593
887
  style_name_to_id[raw_cell_style.to_s]
594
888
  elsif raw_cell_style.nil?
@@ -599,7 +893,7 @@ module Xlsxrb
599
893
 
600
894
  target_fmt = cell.format_code || col_fmt_map[cell.column_index]
601
895
 
602
- if resolved_cell_style == cell.style_index && target_fmt == cell.format_code
896
+ if resolved_cell_style == (cell.style || cell.style_index) && target_fmt == cell.format_code
603
897
  cell
604
898
  else
605
899
  Elements::Cell.new(
@@ -607,7 +901,8 @@ module Xlsxrb
607
901
  column_index: cell.column_index,
608
902
  value: cell.value,
609
903
  formula: cell.formula,
610
- style_index: resolved_cell_style,
904
+ style: (resolved_cell_style unless resolved_cell_style.is_a?(Integer)),
905
+ style_index: (resolved_cell_style if resolved_cell_style.is_a?(Integer)),
611
906
  hyperlink: cell.hyperlink,
612
907
  comment: cell.comment,
613
908
  raw_value: cell.raw_value,
@@ -622,10 +917,11 @@ module Xlsxrb
622
917
  index: row.index,
623
918
  cells: new_cells,
624
919
  height: row.height,
625
- hidden: row.hidden,
626
- custom_height: row.custom_height,
627
- collapsed: row.collapsed,
920
+ hidden: row.hidden?,
921
+ custom_height: row.custom_height?,
922
+ collapsed: row.collapsed?,
628
923
  outline_level: row.outline_level,
924
+ style_index: resolved_row_style,
629
925
  unmapped_data: row_unmapped,
630
926
  errors: row.errors
631
927
  )
@@ -637,7 +933,7 @@ module Xlsxrb
637
933
  [updated_sheets, styles_definition]
638
934
  end
639
935
 
640
- #: (untyped writer) -> { fonts: untyped, fills: untyped, borders: untyped, xf_entries: untyped, num_fmts: untyped }
936
+ #: (untyped writer) -> Hash[Symbol, untyped]
641
937
  def extract_styles_from_writer(writer)
642
938
  # Extract style definitions from the writer that can be reused
643
939
  # This captures the fonts, fills, borders, and xf entries that were created
@@ -645,6 +941,7 @@ module Xlsxrb
645
941
  fonts: writer.fonts.dup,
646
942
  fills: writer.fills.dup,
647
943
  borders: writer.borders.dup,
944
+ cell_xfs: writer.xf_entries.dup,
648
945
  xf_entries: writer.xf_entries.dup,
649
946
  num_fmts: writer.num_fmts.dup
650
947
  }