xlsxrb 0.1.17 → 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 (80) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +256 -1
  3. data/README.md +111 -28
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +439 -139
  5. data/lib/xlsxrb/chart_builder.rb +540 -193
  6. data/lib/xlsxrb/dsl_helpers.rb +178 -73
  7. data/lib/xlsxrb/elements/cell.rb +197 -130
  8. data/lib/xlsxrb/elements/column.rb +88 -7
  9. data/lib/xlsxrb/elements/coordinate_access.rb +86 -59
  10. data/lib/xlsxrb/elements/image.rb +18 -19
  11. data/lib/xlsxrb/elements/metadata.rb +2379 -0
  12. data/lib/xlsxrb/elements/row.rb +84 -118
  13. data/lib/xlsxrb/elements/styles.rb +140 -90
  14. data/lib/xlsxrb/elements/types.rb +164 -22
  15. data/lib/xlsxrb/elements/workbook.rb +122 -140
  16. data/lib/xlsxrb/elements/worksheet.rb +451 -226
  17. data/lib/xlsxrb/elements.rb +1 -0
  18. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +16 -5
  19. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +14 -9
  20. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +296 -34
  21. data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
  22. data/lib/xlsxrb/ooxml/reader.rb +52 -50
  23. data/lib/xlsxrb/ooxml/styles_parser.rb +36 -5
  24. data/lib/xlsxrb/ooxml/utils.rb +2 -2
  25. data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
  26. data/lib/xlsxrb/ooxml/workbook_writer.rb +242 -109
  27. data/lib/xlsxrb/ooxml/worksheet_parser.rb +63 -31
  28. data/lib/xlsxrb/ooxml/worksheet_writer.rb +119 -56
  29. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +33 -13
  30. data/lib/xlsxrb/ooxml/writer/features_xml.rb +75 -15
  31. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +18 -8
  32. data/lib/xlsxrb/ooxml/writer.rb +124 -44
  33. data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
  34. data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
  35. data/lib/xlsxrb/ooxml/zip_reader.rb +112 -25
  36. data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
  37. data/lib/xlsxrb/stream_row.rb +56 -81
  38. data/lib/xlsxrb/stream_sheet.rb +542 -273
  39. data/lib/xlsxrb/stream_writer.rb +1536 -870
  40. data/lib/xlsxrb/style_builder.rb +667 -221
  41. data/lib/xlsxrb/version.rb +1 -1
  42. data/lib/xlsxrb/workbook_builder.rb +406 -143
  43. data/lib/xlsxrb/worksheet_builder.rb +1009 -900
  44. data/lib/xlsxrb.rb +1179 -1056
  45. data/sig/generated/xlsxrb/chart_builder.rbs +385 -159
  46. data/sig/generated/xlsxrb/dsl_helpers.rbs +81 -15
  47. data/sig/generated/xlsxrb/elements/cell.rbs +78 -70
  48. data/sig/generated/xlsxrb/elements/column.rbs +55 -3
  49. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +35 -29
  50. data/sig/generated/xlsxrb/elements/image.rbs +7 -11
  51. data/sig/generated/xlsxrb/elements/metadata.rbs +1574 -0
  52. data/sig/generated/xlsxrb/elements/row.rbs +39 -42
  53. data/sig/generated/xlsxrb/elements/styles.rbs +66 -66
  54. data/sig/generated/xlsxrb/elements/types.rbs +27 -0
  55. data/sig/generated/xlsxrb/elements/workbook.rbs +46 -99
  56. data/sig/generated/xlsxrb/elements/worksheet.rbs +121 -96
  57. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -4
  58. data/sig/generated/xlsxrb/ooxml/reader.rbs +2 -2
  59. data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
  60. data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
  61. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -1
  62. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
  63. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +6 -4
  64. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
  65. data/sig/generated/xlsxrb/ooxml/writer.rbs +22 -13
  66. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
  67. data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
  68. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +24 -9
  69. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
  70. data/sig/generated/xlsxrb/stream_row.rbs +32 -33
  71. data/sig/generated/xlsxrb/stream_sheet.rbs +233 -181
  72. data/sig/generated/xlsxrb/stream_writer.rbs +961 -667
  73. data/sig/generated/xlsxrb/style_builder.rbs +402 -128
  74. data/sig/generated/xlsxrb/workbook_builder.rbs +223 -71
  75. data/sig/generated/xlsxrb/worksheet_builder.rbs +579 -453
  76. data/sig/generated/xlsxrb.rbs +205 -144
  77. data/sig/rexml.rbs +8 -0
  78. metadata +5 -5
  79. data/lib/xlsxrb/utils.rb +0 -291
  80. data/sig/generated/xlsxrb/utils.rbs +0 -184
@@ -16,65 +16,160 @@ module Xlsxrb
16
16
  #
17
17
  # @api public
18
18
  class Cell
19
- attr_reader :row_index, :column_index, :value, :formula, :style_index, :unmapped_data, :errors, :hyperlink, :comment, :raw_value, :format_code, :date1904, :threaded_comments
19
+ #: Integer
20
+ attr_reader :row_index
21
+ #: Integer
22
+ attr_reader :column_index
23
+ #: untyped
24
+ attr_reader :value
25
+ #: Elements::Formula?
26
+ attr_reader :formula
27
+ #: (String | Symbol | Hash[Symbol, untyped])?
28
+ attr_reader :style
29
+ #: Integer?
30
+ attr_reader :style_index
31
+ #: Hash[untyped, untyped]
32
+ attr_reader :unmapped_data
33
+ #: Array[String]
34
+ attr_reader :errors
35
+ #: Elements::Hyperlink?
36
+ attr_reader :hyperlink
37
+ #: Elements::Comment?
38
+ attr_reader :comment
39
+ #: String?
40
+ attr_reader :raw_value
41
+ #: String?
42
+ attr_reader :format_code
43
+ #: Array[Elements::ThreadedComment]
44
+ attr_reader :threaded_comments
20
45
 
21
46
  # @param row_index [Integer] 0-based row index.
22
47
  # @param column_index [Integer] 0-based column index.
23
48
  # @param value [Object, nil] The cell's value.
24
49
  # @param formula [Elements::Formula, nil] Optional formula.
25
- # @param style_index [Integer, String, Symbol, Hash, nil] Style identifier or inline style.
50
+ # @param style [String, Symbol, Hash, nil] Style name or inline style properties.
51
+ # @param style_index [Integer, nil] 0-based style index.
26
52
  # @param unmapped_data [Hash] Additional metadata.
27
53
  # @param errors [Array<String>, nil] Validation errors.
28
- # @param hyperlink [Hash, String, Elements::Hyperlink, nil] Optional hyperlink metadata or URL.
29
- # @param comment [Hash, String, nil] Optional cell comment or note.
54
+ # @param hyperlink [Elements::Hyperlink, Hash, String, nil] Optional hyperlink metadata or URL.
55
+ # @param comment [Elements::Comment, Hash, String, nil] Optional cell comment or note.
30
56
  # @param raw_value [String, nil] Optional unparsed raw string value.
31
57
  # @param format_code [String, nil] Optional OpenXML number/date format code.
32
58
  # @param date1904 [Boolean] Whether the cell belongs to a 1904 date system workbook.
33
- # @param threaded_comments [Array<Hash>, nil] Optional threaded comments.
34
- #: (row_index: untyped, column_index: untyped, ?value: untyped, ?formula: (Elements::Formula | String)?, ?style_index: (Integer | String | Symbol | Hash[Symbol, untyped])?, ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?hyperlink: (Hash[Symbol, untyped] | String | Elements::Hyperlink)?, ?comment: (Hash[Symbol, untyped] | String)?, ?raw_value: String?, ?format_code: String?, ?date1904: bool, ?threaded_comments: Array[Hash[Symbol, untyped]]?) -> void
35
- def initialize(row_index:, column_index:, value: nil, formula: nil, style_index: nil, unmapped_data: EMPTY_HASH, errors: nil, hyperlink: nil, comment: nil, raw_value: nil, format_code: nil, date1904: false, threaded_comments: nil)
59
+ # @param threaded_comments [Array<Elements::ThreadedComment, Hash>, nil] Optional threaded comments.
60
+ #: (row_index: untyped, column_index: untyped, ?value: untyped, ?formula: (Elements::Formula | String | Hash[Symbol, untyped])?, ?style: (String | Symbol | Hash[Symbol, untyped])?, ?style_index: Integer?, ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?hyperlink: (Elements::Hyperlink | Hash[Symbol, untyped] | String)?, ?comment: (Elements::Comment | Hash[Symbol, untyped] | String)?, ?raw_value: String?, ?format_code: String?, ?date1904: bool, ?threaded_comments: Array[Elements::ThreadedComment | Hash[Symbol, untyped]]?) -> void
61
+ def initialize(row_index:, column_index:, value: nil, formula: nil, style: nil, style_index: nil, unmapped_data: EMPTY_HASH, errors: nil, hyperlink: nil, comment: nil, raw_value: nil, format_code: nil, date1904: false, threaded_comments: nil)
36
62
  @row_index = row_index
37
63
  @column_index = column_index
38
64
  @value = value
39
- @formula = formula
40
- @style_index = style_index
65
+ @formula = self.class.normalize_formula(formula)
66
+ @style = style
67
+ @style_index = style_index || (style if style.is_a?(Integer))
41
68
  @unmapped_data = unmapped_data || EMPTY_HASH
42
69
  computed_errors = errors || self.class.validate(row_index, column_index, value)
43
70
  @errors = computed_errors.frozen? ? computed_errors : computed_errors.freeze
44
- @hyperlink = hyperlink
45
- @comment = comment
71
+ @hyperlink = case hyperlink
72
+ when Hash
73
+ Elements::Hyperlink.new(**hyperlink)
74
+ when String
75
+ hyperlink.empty? ? nil : Elements::Hyperlink.new(url: hyperlink)
76
+ else
77
+ hyperlink
78
+ end
79
+ @comment = case comment
80
+ when nil, Elements::Comment
81
+ comment
82
+ when String
83
+ comment.empty? ? nil : Elements::Comment.new(text: comment)
84
+ when Hash
85
+ Elements::Comment.new(**comment)
86
+ else
87
+ Elements::Comment.new(text: comment.to_s)
88
+ end
46
89
  @raw_value = raw_value
47
90
  @format_code = format_code
48
91
  @date1904 = date1904 ? true : false
49
- @threaded_comments = (threaded_comments || EMPTY_ARRAY).freeze
92
+ @threaded_comments = if threaded_comments
93
+ threaded_comments.map do |tc|
94
+ tc.is_a?(Elements::ThreadedComment) ? tc : Elements::ThreadedComment.new(**tc)
95
+ end.freeze
96
+ else
97
+ EMPTY_ARRAY
98
+ end
99
+ freeze
50
100
  end
51
101
 
102
+ # @api private
52
103
  # rubocop:disable Style/OptionalBooleanParameter
53
104
  def self.fast_create(row_index, column_index, value, style_index = nil, formula = nil, raw_value = nil, format_code = nil, date1904 = false)
54
- inst = allocate
55
- inst.instance_variable_set(:@row_index, row_index)
56
- inst.instance_variable_set(:@column_index, column_index)
57
- inst.instance_variable_set(:@value, value)
58
- inst.instance_variable_set(:@formula, formula)
59
- inst.instance_variable_set(:@style_index, style_index)
60
- inst.instance_variable_set(:@unmapped_data, EMPTY_HASH)
61
- inst.instance_variable_set(:@errors, EMPTY_ERRORS)
62
- inst.instance_variable_set(:@hyperlink, nil)
63
- inst.instance_variable_set(:@comment, nil)
64
- inst.instance_variable_set(:@raw_value, raw_value)
65
- inst.instance_variable_set(:@format_code, format_code)
66
- inst.instance_variable_set(:@date1904, date1904 ? true : false)
67
- inst.instance_variable_set(:@threaded_comments, EMPTY_ARRAY)
68
- inst
105
+ allocate.__send__(:init_fast, row_index, column_index, value, style_index, formula, raw_value, format_code, date1904)
69
106
  end
70
107
  # rubocop:enable Style/OptionalBooleanParameter
71
108
 
109
+ # Assigns instance variables directly, in the same order as #initialize, for
110
+ # fast_create. One method call instead of one instance_variable_set per variable.
111
+ #: (untyped row_index, untyped column_index, untyped value, untyped style_index, untyped formula, String? raw_value, String? format_code, bool date1904) -> Cell
112
+ def init_fast(row_index, column_index, value, style_index, formula, raw_value, format_code, date1904)
113
+ integer_style = style_index.is_a?(Integer)
114
+ @row_index = row_index
115
+ @column_index = column_index
116
+ @value = value
117
+ @formula = formula.nil? ? nil : self.class.normalize_formula(formula)
118
+ @style = integer_style ? nil : style_index
119
+ @style_index = integer_style ? style_index : nil
120
+ @unmapped_data = EMPTY_HASH
121
+ @errors = EMPTY_ERRORS
122
+ @hyperlink = nil
123
+ @comment = nil
124
+ @raw_value = raw_value
125
+ @format_code = format_code
126
+ @date1904 = date1904 ? true : false
127
+ @threaded_comments = EMPTY_ARRAY
128
+ freeze
129
+ end
130
+ private :init_fast
131
+
132
+ # @api private
133
+ #: (untyped formula) -> Elements::Formula?
134
+ def self.normalize_formula(formula)
135
+ case formula
136
+ when nil, Elements::Formula
137
+ formula
138
+ when String
139
+ expr = formula.delete_prefix("=")
140
+ expr.empty? ? nil : Elements::Formula.new(expression: expr)
141
+ when Hash
142
+ expr = (formula[:expression] || formula[:formula] || "").to_s.delete_prefix("=")
143
+ is_shared = formula[:shared] || formula[:shared_index]
144
+ return nil if expr.empty? && !is_shared
145
+
146
+ Elements::Formula.new(
147
+ expression: expr,
148
+ cached_value: formula[:cached_value] || formula[:value],
149
+ type: formula[:type],
150
+ ref: formula[:ref],
151
+ shared_index: formula[:shared_index]&.to_i,
152
+ calculate_always: formula[:calculate_always],
153
+ aca: formula[:aca],
154
+ bx: formula[:bx],
155
+ dt2d: formula[:dt2d],
156
+ dtr: formula[:dtr],
157
+ r1: formula[:r1],
158
+ r2: formula[:r2]
159
+ )
160
+ else
161
+ expr = formula.to_s.delete_prefix("=")
162
+ expr.empty? ? nil : Elements::Formula.new(expression: expr)
163
+ end
164
+ end
165
+
72
166
  def ==(other)
73
167
  other.is_a?(Cell) &&
74
168
  row_index == other.row_index &&
75
169
  column_index == other.column_index &&
76
170
  value == other.value &&
77
171
  formula == other.formula &&
172
+ style == other.style &&
78
173
  style_index == other.style_index &&
79
174
  unmapped_data == other.unmapped_data &&
80
175
  errors == other.errors &&
@@ -87,18 +182,32 @@ module Xlsxrb
87
182
  alias eql? ==
88
183
 
89
184
  def hash
90
- [row_index, column_index, value, formula, style_index, unmapped_data, errors, hyperlink, comment, raw_value, format_code, threaded_comments].hash
185
+ [row_index, column_index, value, formula, style, style_index, unmapped_data, errors, hyperlink, comment, raw_value, format_code, threaded_comments].hash
91
186
  end
92
187
 
93
188
  def deconstruct
94
- [row_index, column_index, value, formula, style_index, unmapped_data, errors, hyperlink, comment, raw_value, format_code, threaded_comments]
189
+ [row_index, column_index, value, formula, style_index || style, unmapped_data, errors, hyperlink, comment, raw_value, format_code, threaded_comments]
95
190
  end
96
191
 
97
- def deconstruct_keys(keys)
98
- h = { row_index: row_index, column_index: column_index, value: value, formula: formula,
99
- style_index: style_index, unmapped_data: unmapped_data, errors: errors, hyperlink: hyperlink,
100
- comment: comment, raw_value: raw_value, format_code: format_code, threaded_comments: threaded_comments }
101
- keys ? h.slice(*keys) : h
192
+ # Deconstructs cell attributes for pattern matching.
193
+ #
194
+ # @param _keys [Array<Symbol>, nil]
195
+ # @return [Hash{Symbol => Object}]
196
+ # @api public
197
+ #: (Array[Symbol]? _keys) -> Hash[Symbol, untyped]
198
+ def deconstruct_keys(_keys)
199
+ {
200
+ ref: ref,
201
+ row_index: @row_index,
202
+ column_index: @column_index,
203
+ value: @value,
204
+ formula: @formula,
205
+ style_index: @style_index,
206
+ format_code: @format_code,
207
+ hyperlink: @hyperlink,
208
+ comment: @comment,
209
+ threaded_comments: @threaded_comments
210
+ }
102
211
  end
103
212
 
104
213
  def with(**changes)
@@ -107,6 +216,7 @@ module Xlsxrb
107
216
  column_index: changes.fetch(:column_index, column_index),
108
217
  value: changes.fetch(:value, value),
109
218
  formula: changes.fetch(:formula, formula),
219
+ style: changes.fetch(:style, style),
110
220
  style_index: changes.fetch(:style_index, style_index),
111
221
  unmapped_data: changes.fetch(:unmapped_data, unmapped_data),
112
222
  errors: changes.fetch(:errors, errors),
@@ -133,7 +243,7 @@ module Xlsxrb
133
243
  # @api public
134
244
  #: () -> String
135
245
  def ref
136
- "#{self.class.column_letter(column_index)}#{row_index + 1}"
246
+ "#{self.class._column_letter(column_index)}#{row_index + 1}"
137
247
  end
138
248
 
139
249
  # Returns whether the cell contains a formula.
@@ -167,14 +277,13 @@ module Xlsxrb
167
277
  # @return [Boolean]
168
278
  # @api public
169
279
  #: () -> bool
170
- def link?
280
+ def hyperlink?
171
281
  case hyperlink
172
282
  when nil then false
173
- when Hash, String then !hyperlink.empty?
283
+ when Hash, String, Elements::Hyperlink then !hyperlink.empty?
174
284
  else true
175
285
  end
176
286
  end
177
- alias hyperlink? link?
178
287
 
179
288
  # Returns the hyperlink target URL or location string, or nil.
180
289
  #
@@ -215,7 +324,7 @@ module Xlsxrb
215
324
  def comment?
216
325
  case comment
217
326
  when nil then false
218
- when Hash, String then !comment.empty?
327
+ when Hash, String, Elements::Comment then !comment.empty?
219
328
  else true
220
329
  end
221
330
  end
@@ -232,6 +341,10 @@ module Xlsxrb
232
341
  txt&.to_s
233
342
  when String
234
343
  comment.empty? ? nil : comment
344
+ when Elements::Comment
345
+ comment.text&.to_s
346
+ else
347
+ comment.respond_to?(:text) ? comment.text&.to_s : nil
235
348
  end
236
349
  end
237
350
 
@@ -240,15 +353,15 @@ module Xlsxrb
240
353
  # @return [Boolean]
241
354
  # @api public
242
355
  #: () -> bool
243
- def threaded_comments?
356
+ def threaded_comment?
244
357
  !@threaded_comments.empty?
245
358
  end
246
359
 
247
360
  # Returns the root threaded comment for this cell, or nil.
248
361
  #
249
- # @return [Hash[Symbol, untyped], nil]
362
+ # @return [Elements::ThreadedComment, nil]
250
363
  # @api public
251
- #: () -> Hash[Symbol, untyped]?
364
+ #: () -> Elements::ThreadedComment?
252
365
  def threaded_comment
253
366
  @threaded_comments.first
254
367
  end
@@ -265,42 +378,6 @@ module Xlsxrb
265
378
  NumberFormatter.format(value, custom_format_code || format_code)
266
379
  end
267
380
 
268
- # Access cell attributes by Symbol key.
269
- #
270
- # @param key [Symbol] Attribute key (:value, :formula, :style_index, :ref, :column_index, :row_index, :type, :raw_value, :format_code, :formatted_value).
271
- # @return [Object, nil]
272
- # @api public
273
- #: (Symbol key) -> untyped
274
- def [](key)
275
- case key
276
- when :value then value
277
- when :formula
278
- formula.is_a?(Formula) ? formula.expression : formula
279
- when :style_index then style_index
280
- when :ref then ref
281
- when :column_index then column_index
282
- when :row_index then row_index
283
- when :hyperlink then hyperlink
284
- when :url then url
285
- when :tooltip then tooltip
286
- when :comment then comment
287
- when :threaded_comments then threaded_comments
288
- when :threaded_comment then threaded_comment
289
- when :raw_value then raw_value
290
- when :format_code then format_code
291
- when :formatted_value then formatted_value
292
- when :format_type then format_type
293
- when :date_format then date_format?
294
- when :time_format then time_format?
295
- when :date1904 then date1904?
296
- when :type
297
- case value
298
- when String then "s"
299
- when true, false then "b"
300
- end
301
- end
302
- end
303
-
304
381
  # Returns the classified format category for this cell (:date, :datetime, :time, :number, :text, :general, or nil).
305
382
  #
306
383
  # @return [Symbol, nil]
@@ -310,6 +387,15 @@ module Xlsxrb
310
387
  format_code ? NumberFormatter.format_type(format_code) : nil
311
388
  end
312
389
 
390
+ # Returns the number format code for this cell.
391
+ #
392
+ # @return [String, nil]
393
+ # @api public
394
+ #: () -> String?
395
+ def number_format
396
+ format_code
397
+ end
398
+
313
399
  # Returns whether the cell has a date, datetime, or time format.
314
400
  #
315
401
  # @return [Boolean]
@@ -319,6 +405,24 @@ module Xlsxrb
319
405
  format_code ? NumberFormatter.date_format?(format_code) : false
320
406
  end
321
407
 
408
+ # Returns whether the cell has a datetime format (date with time).
409
+ #
410
+ # @return [Boolean]
411
+ # @api public
412
+ #: () -> bool
413
+ def datetime_format?
414
+ format_code ? NumberFormatter.datetime_format?(format_code) : false
415
+ end
416
+
417
+ # Returns whether the cell has a date-only format.
418
+ #
419
+ # @return [Boolean]
420
+ # @api public
421
+ #: () -> bool
422
+ def date_only_format?
423
+ format_code ? NumberFormatter.date_only_format?(format_code) : false
424
+ end
425
+
322
426
  # Returns whether the cell has a time-only format.
323
427
  #
324
428
  # @return [Boolean]
@@ -337,15 +441,6 @@ module Xlsxrb
337
441
  @date1904 ? true : false
338
442
  end
339
443
 
340
- # Returns the cell value.
341
- #
342
- # @return [Object, nil]
343
- # @api public
344
- #: () -> untyped
345
- def content
346
- value
347
- end
348
-
349
444
  # Returns the string representation of the cell value.
350
445
  #
351
446
  # @return [String]
@@ -375,16 +470,14 @@ module Xlsxrb
375
470
 
376
471
  # Converts the cell value (numeric serial date or date string) to Date.
377
472
  #
378
- # @param date1904 [Boolean, nil] Whether to use the 1904 date system (defaults to cell's date1904 setting).
379
473
  # @return [Date, nil]
380
474
  # @api public
381
- #: (?date1904: bool?) -> Date?
382
- def to_date(date1904: nil)
475
+ #: () -> Date?
476
+ def to_date
383
477
  return value if value.is_a?(Date)
384
478
 
385
- d1904 = date1904.nil? ? date1904? : date1904
386
479
  if value.is_a?(Numeric)
387
- Ooxml::Utils.serial_to_date(value, date1904: d1904)
480
+ Ooxml::Utils.serial_to_date(value, date1904: date1904?)
388
481
  else
389
482
  begin
390
483
  Date.parse(value.to_s)
@@ -396,16 +489,14 @@ module Xlsxrb
396
489
 
397
490
  # Converts the cell value (numeric serial datetime or datetime string) to Time.
398
491
  #
399
- # @param date1904 [Boolean, nil] Whether to use the 1904 date system (defaults to cell's date1904 setting).
400
492
  # @return [Time, nil]
401
493
  # @api public
402
- #: (?date1904: bool?) -> Time?
403
- def to_time(date1904: nil)
494
+ #: () -> Time?
495
+ def to_time
404
496
  return value if value.is_a?(Time)
405
497
 
406
- d1904 = date1904.nil? ? date1904? : date1904
407
498
  if value.is_a?(Numeric)
408
- Ooxml::Utils.serial_to_datetime(value, date1904: d1904)
499
+ Ooxml::Utils.serial_to_datetime(value, date1904: date1904?)
409
500
  else
410
501
  begin
411
502
  Time.parse(value.to_s)
@@ -419,6 +510,7 @@ module Xlsxrb
419
510
  #
420
511
  # @param index [Integer] 0-based column index.
421
512
  # @return [String] Excel column letter.
513
+ # @api private
422
514
  #: (Integer index) -> String
423
515
  def self.calculate_column_letter(index)
424
516
  result = +""
@@ -434,17 +526,8 @@ module Xlsxrb
434
526
  # Cache column letters up to Excel's limit (16,384)
435
527
  @column_letters = (0...16_384).map { |index| calculate_column_letter(index).freeze }.freeze
436
528
 
437
- # Converts a 0-based column index to an Excel letter (0 -> "A", 25 -> "Z", 26 -> "AA").
438
- #
439
- # @example
440
- # Cell.column_letter(0) #=> "A"
441
- # Cell.column_letter(26) #=> "AA"
442
- #
443
- # @param index [Integer] 0-based column index.
444
- # @return [String] Excel column letter.
445
- # @api public
446
- #: (untyped index) -> String
447
- def self.column_letter(index)
529
+ # @api private
530
+ def self._column_letter(index)
448
531
  raise ArgumentError, "Column index must be a non-negative Integer, got #{index.inspect}" unless index.is_a?(Integer) && index >= 0
449
532
 
450
533
  @column_letters[index] || calculate_column_letter(index)
@@ -454,22 +537,14 @@ module Xlsxrb
454
537
  #
455
538
  # @param letter [String] Uppercase column letter string (e.g. "A", "AA").
456
539
  # @return [Integer] 0-based column index.
540
+ # @api private
457
541
  #: (String letter) -> Integer
458
542
  def self.calculate_column_index(letter)
459
543
  letter.bytes.reduce(0) { |acc, b| (acc * 26) + (b - 64) } - 1
460
544
  end
461
545
 
462
- # Converts a column letter (e.g. "A", :AA) to a 0-based column index.
463
- #
464
- # @example
465
- # Cell.column_index("A") #=> 0
466
- # Cell.column_index(:AA) #=> 26
467
- #
468
- # @param letter [String, Symbol, Integer] Column letter or integer index.
469
- # @return [Integer] 0-based column index.
470
- # @api public
471
- #: (String | Symbol | Integer letter) -> Integer
472
- def self.column_index(letter)
546
+ # @api private
547
+ def self._column_index(letter)
473
548
  if letter.is_a?(Integer)
474
549
  raise ArgumentError, "Column index must be >= 0, got #{letter}" if letter.negative?
475
550
 
@@ -489,17 +564,8 @@ module Xlsxrb
489
564
  calculate_column_index(str.upcase)
490
565
  end
491
566
 
492
- # Parses an Excel-style reference to [row_index, col_index] (both 0-based).
493
- #
494
- # @example
495
- # Cell.parse_ref("A1") #=> [0, 0]
496
- # Cell.parse_ref("C10") #=> [9, 2]
497
- #
498
- # @param ref [String, nil] Excel cell reference.
499
- # @return [Array(Integer, Integer), nil] 0-based [row_index, col_index].
500
- # @api public
501
- #: (String? ref) -> [Integer, Integer]?
502
- def self.parse_ref(ref)
567
+ # @api private
568
+ def self._parse_ref(ref)
503
569
  return nil unless ref
504
570
 
505
571
  bytes = ref.b
@@ -560,6 +626,7 @@ module Xlsxrb
560
626
  # @param column_index [Integer]
561
627
  # @param value [Object]
562
628
  # @return [Array<String>] List of errors.
629
+ # @api private
563
630
  #: (untyped row_index, untyped column_index, untyped value) -> Array[String]
564
631
  def self.validate(row_index, column_index, value)
565
632
  valid_val = valid_value?(value)
@@ -12,8 +12,10 @@ module Xlsxrb
12
12
  #
13
13
  # @api public
14
14
  # rubocop:disable Style/DataInheritance -- Required as class syntax for mutant subject matcher
15
- class Column < Data.define(:index, :width, :hidden, :custom_width, :outline_level, :style_index, :unmapped_data, :errors)
15
+ class Column < Data.define(:index, :width, :hidden, :custom_width, :outline_level, :style_index, :unmapped_data, :errors, :best_fit, :collapsed)
16
16
  # rubocop:enable Style/DataInheritance
17
+
18
+ undef_method :hidden, :custom_width, :best_fit, :collapsed
17
19
  # @param index [Integer] 0-based column index.
18
20
  # @param width [Float, Integer, nil] Column width in characters.
19
21
  # @param hidden [Boolean] Whether the column is hidden.
@@ -22,14 +24,19 @@ module Xlsxrb
22
24
  # @param style_index [Integer, String, Symbol, Hash, nil] Optional style identifier or inline style.
23
25
  # @param unmapped_data [Hash] Additional metadata.
24
26
  # @param errors [Array<String>, nil] Validation errors.
25
- #: (index: Integer, ?width: Float | Integer | nil, ?hidden: bool, ?custom_width: bool, ?outline_level: Integer | nil, ?style_index: (Integer | String | Symbol | Hash[Symbol, untyped])?, ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?) -> void
27
+ # @param best_fit [Boolean] Whether the column automatically fits content.
28
+ # @param collapsed [Boolean] Whether the column is collapsed.
29
+ #: (index: Integer, ?width: Float | Integer | nil, ?hidden: bool, ?custom_width: bool, ?outline_level: Integer | nil, ?style_index: (Integer | String | Symbol | Hash[Symbol, untyped])?, ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?best_fit: bool, ?collapsed: bool) -> void
26
30
  def initialize(index:, width: nil, hidden: false, custom_width: false, outline_level: nil,
27
- style_index: nil, unmapped_data: {}, errors: nil)
28
- style_idx = style_index || unmapped_data[:style_index] || unmapped_data[:style]
31
+ style_index: nil, unmapped_data: {}, errors: nil, best_fit: false, collapsed: false)
32
+ @hidden = hidden ? true : false
33
+ @custom_width = custom_width ? true : false
34
+ @best_fit = best_fit ? true : false
35
+ @collapsed = collapsed ? true : false
29
36
  computed_errors = errors || self.class.validate(index)
30
- super(index: index, width: width, hidden: hidden, custom_width: custom_width,
31
- outline_level: outline_level, style_index: style_idx, unmapped_data: unmapped_data,
32
- errors: computed_errors.freeze)
37
+ super(**{ index: index, width: width, hidden: @hidden, custom_width: @custom_width,
38
+ outline_level: outline_level, style_index: style_index, unmapped_data: unmapped_data,
39
+ errors: computed_errors.freeze, best_fit: @best_fit, collapsed: @collapsed })
33
40
  end
34
41
 
35
42
  # Returns whether the column definition is valid according to OOXML specifications.
@@ -40,6 +47,79 @@ module Xlsxrb
40
47
  errors.empty?
41
48
  end
42
49
 
50
+ # Returns whether the column is hidden.
51
+ #
52
+ # @return [Boolean]
53
+ # @api public
54
+ #: () -> bool
55
+ def hidden?
56
+ @hidden
57
+ end
58
+
59
+ # Returns whether custom width is set.
60
+ #
61
+ # @return [Boolean]
62
+ # @api public
63
+ #: () -> bool
64
+ def custom_width?
65
+ @custom_width
66
+ end
67
+
68
+ # Returns whether best fit is enabled.
69
+ #
70
+ # @return [Boolean]
71
+ # @api public
72
+ #: () -> bool
73
+ def best_fit?
74
+ @best_fit
75
+ end
76
+
77
+ # Returns whether the column is collapsed.
78
+ #
79
+ # @return [Boolean]
80
+ # @api public
81
+ #: () -> bool
82
+ def collapsed?
83
+ @collapsed
84
+ end
85
+
86
+ # Deconstructs column attributes for pattern matching.
87
+ #
88
+ # @param _keys [Array<Symbol>, nil]
89
+ # @return [Hash{Symbol => Object}]
90
+ # @api public
91
+ #: (Array[Symbol]? _keys) -> Hash[Symbol, untyped]
92
+ def deconstruct_keys(_keys)
93
+ {
94
+ index: index,
95
+ width: width,
96
+ hidden: hidden?,
97
+ custom_width: custom_width?,
98
+ best_fit: best_fit?,
99
+ collapsed: collapsed?,
100
+ outline_level: outline_level,
101
+ style_index: style_index
102
+ }
103
+ end
104
+
105
+ # Returns the column letter (e.g. "A", "B", "AA").
106
+ #
107
+ # @return [String]
108
+ # @api public
109
+ #: () -> String
110
+ def column_letter
111
+ Cell._column_letter(index)
112
+ end
113
+
114
+ # Returns the style identifier or inline style.
115
+ #
116
+ # @return [Integer, String, Symbol, Hash, nil]
117
+ # @api public
118
+ #: () -> (Integer | String | Symbol | Hash[Symbol, untyped])?
119
+ def style
120
+ style_index
121
+ end
122
+
43
123
  # Returns whether the column index is within valid OOXML range (0..16383).
44
124
  #
45
125
  # @param index [Object]
@@ -58,6 +138,7 @@ module Xlsxrb
58
138
  #
59
139
  # @param index [Integer]
60
140
  # @return [Array<String>] List of errors.
141
+ # @api private
61
142
  #: (untyped index) -> Array[String]
62
143
  def self.validate(index)
63
144
  errs = []