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
@@ -14,7 +14,7 @@ module Xlsxrb
14
14
  #
15
15
  # @api public
16
16
  # rubocop:disable Style/DataInheritance -- Required as class syntax for mutant subject matcher
17
- class Row < Data.define(:index, :cells, :height, :hidden, :custom_height, :collapsed, :outline_level, :unmapped_data, :errors)
17
+ class Row < Data.define(:index, :cells, :height, :hidden, :custom_height, :collapsed, :outline_level, :style_index, :unmapped_data, :errors)
18
18
  # rubocop:enable Style/DataInheritance
19
19
  [Enumerable].each { |m| include m }
20
20
 
@@ -25,30 +25,37 @@ module Xlsxrb
25
25
  # @param custom_height [Boolean] Whether custom height is set.
26
26
  # @param collapsed [Boolean] Whether the row is collapsed.
27
27
  # @param outline_level [Integer, nil] Grouping/outline level.
28
+ # @param style_index [Integer, nil] Default row style index.
28
29
  # @param unmapped_data [Hash] Additional metadata.
29
30
  # @param errors [Array<String>, nil] Validation errors.
30
- #: (index: Integer, ?cells: Array[Elements::Cell], ?height: Float | Integer | nil, ?hidden: bool, ?custom_height: bool, ?collapsed: bool, ?outline_level: Integer | nil, ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?) -> void
31
+ #: (index: Integer, ?cells: Array[Elements::Cell], ?height: Float | Integer | nil, ?hidden: bool, ?custom_height: bool, ?collapsed: bool, ?outline_level: Integer | nil, ?style_index: (Integer | Symbol | String | Hash[Symbol, untyped] | nil), ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?) -> void
31
32
  def initialize(index:, cells: EMPTY_CELLS, height: nil, hidden: false, custom_height: false, collapsed: false,
32
- outline_level: nil, unmapped_data: EMPTY_HASH, errors: nil)
33
+ outline_level: nil, style_index: nil, unmapped_data: EMPTY_HASH, errors: nil)
33
34
  computed_errors = errors || self.class.validate(index, cells)
34
35
  computed_errors = computed_errors.freeze unless computed_errors.frozen?
35
36
  cells = cells.freeze unless cells.frozen?
36
- super(index: index, cells: cells, height: height, hidden: hidden,
37
- custom_height: custom_height, collapsed: collapsed, outline_level: outline_level,
38
- unmapped_data: unmapped_data, errors: computed_errors)
37
+ @hidden = hidden ? true : false
38
+ @custom_height = custom_height ? true : false
39
+ @collapsed = collapsed ? true : false
40
+ resolved_style_index = style_index || unmapped_data[:style_index]
41
+ super(index: index, cells: cells, height: height, hidden: @hidden,
42
+ custom_height: @custom_height, collapsed: @collapsed, outline_level: outline_level,
43
+ style_index: resolved_style_index, unmapped_data: unmapped_data, errors: computed_errors)
39
44
  end
40
45
 
41
- # Access a cell by 0-based column index, or access row attributes via Symbol.
46
+ # Access a cell by 0-based column index, column letter, or access row attributes via Symbol.
42
47
  #
43
48
  # @example
44
- # row[0] #=> Cell at column 0
49
+ # row[0] #=> Cell at column 0 (or nil if sparse)
50
+ # row["A"] #=> Cell at column "A"
51
+ # row[:A] #=> Cell at column "A"
45
52
  # row[:height] #=> 25.0
46
53
  # row[:cells] #=> [Cell, Cell, ...]
47
54
  #
48
- # @param col_index [Integer, Symbol] Column index or attribute symbol.
55
+ # @param col_index [Integer, String, Symbol] Column index, column letter, or attribute symbol.
49
56
  # @return [Elements::Cell, Object, nil]
50
57
  # @api public
51
- #: (Integer | Symbol col_index) -> untyped
58
+ #: (Integer | String | Symbol col_index) -> untyped
52
59
  def [](col_index)
53
60
  case col_index
54
61
  when Symbol
@@ -57,67 +64,90 @@ module Xlsxrb
57
64
  when :values then values
58
65
  when :raw_values then raw_values
59
66
  when :formatted_values then formatted_values
60
- when :formulas then formulas
67
+ when :formula_expressions, :formulas then formula_expressions
61
68
  when :index then index
62
69
  when :height then height
63
- when :hidden then hidden
64
- when :custom_height then custom_height
65
- when :collapsed then collapsed
70
+ when :hidden, :hidden? then hidden?
71
+ when :custom_height, :custom_height? then custom_height?
72
+ when :collapsed, :collapsed? then collapsed?
66
73
  when :outline_level then outline_level
67
74
  when :style_index then style_index
68
- when :hidden? then hidden?
69
- when :collapsed? then collapsed?
70
- when :custom_height? then custom_height?
71
75
  when :attributes then attributes
72
- when :cells_hash then cells_hash
76
+ when :cells_hash
77
+ warn "[DEPRECATION] `:cells_hash` key is deprecated; use `cells` instead.", uplevel: 1
78
+ cells_hash
73
79
  when :attrs
74
- h = { height: height, hidden: hidden, custom_height: custom_height, collapsed: collapsed, outline_level: outline_level }
80
+ warn "[DEPRECATION] `:attrs` key is deprecated; use `:attributes` instead.", uplevel: 1
81
+ h = { height: height, hidden: hidden?, custom_height: custom_height?, collapsed: collapsed?, outline_level: outline_level }
75
82
  h[:style_index] = style_index if style_index
76
83
  h
84
+ else
85
+ idx = Cell._column_index(col_index.to_s)
86
+ idx ? cell_at(idx) : nil
77
87
  end
78
88
  else
79
- cells[col_index]
89
+ idx = col_index.is_a?(Integer) ? col_index : Cell._column_index(col_index)
90
+ idx ? cell_at(idx) : nil
80
91
  end
81
92
  end
82
93
 
83
- # Returns a new Row with missing cells padded up to the maximum column index.
94
+ # Returns a new Row with missing cells padded up to max_column (or maximum cell column index if nil).
84
95
  #
96
+ # @param max_column [Integer, nil] Maximum 0-based column index to pad up to.
97
+ # @param max_col [Integer, nil] Deprecated alias for max_column.
85
98
  # @return [Elements::Row]
86
99
  # @api public
87
- #: () -> Elements::Row
88
- def pad_empty_cells
100
+ #: (?max_column: Integer?, ?max_col: Integer?) -> Elements::Row
101
+ def padded(max_column: nil, max_col: nil)
102
+ if max_col
103
+ warn "[DEPRECATION] `max_col:` is deprecated; use `max_column:` instead.", uplevel: 1 unless max_column
104
+ max_column ||= max_col
105
+ end
89
106
  return self if cells.empty?
90
107
 
91
- max_col = cells.map(&:column_index).max || 0
92
- return self if cells.size == max_col + 1
108
+ max_c = max_column || (cells.map(&:column_index).max || 0)
109
+ return self if max_column.nil? && cells.size == max_c + 1
93
110
 
94
111
  is_d1904 = cells.first ? cells.first.date1904? : false
95
112
  cell_map = {}
96
113
  cells.each { |c| cell_map[c.column_index] = c }
97
- padded = Array.new(max_col + 1)
98
- (0..max_col).each do |c_idx|
99
- padded[c_idx] = cell_map[c_idx] || Cell.fast_create(index, c_idx, nil, nil, nil, nil, nil, is_d1904)
114
+ padded_cells = Array.new(max_c + 1)
115
+ (0..max_c).each do |c_idx|
116
+ padded_cells[c_idx] = cell_map[c_idx] || Cell.fast_create(index, c_idx, nil, nil, nil, nil, nil, is_d1904)
100
117
  end
101
118
  Row.new(
102
119
  index: index,
103
- cells: padded,
120
+ cells: padded_cells,
104
121
  height: height,
105
122
  hidden: hidden,
106
123
  custom_height: custom_height,
107
124
  collapsed: collapsed,
108
125
  outline_level: outline_level,
126
+ style_index: style_index,
109
127
  unmapped_data: unmapped_data,
110
128
  errors: errors
111
129
  )
112
130
  end
113
131
 
132
+ # @deprecated Use {#padded} instead. Will be removed in 0.2.0.
133
+ #
134
+ # @param max_col [Integer, nil] Deprecated parameter.
135
+ # @return [Elements::Row]
136
+ #: (?max_col: Integer?) -> Elements::Row
137
+ def pad_empty_cells(max_col: nil)
138
+ warn "[DEPRECATION] `Row#pad_empty_cells` is deprecated; use `Row#padded` instead.", uplevel: 1
139
+ padded(max_column: max_col)
140
+ end
141
+
114
142
  # Returns a coordinate-keyed Hash of cell values (e.g. {"A1" => "value", "B1" => 123}).
115
143
  #
144
+ # @deprecated Use {#cells} or sheet.cells_hash instead. Will be removed in 0.2.0.
116
145
  # @param type_cast [Boolean] Whether to coerce date/time serial numbers into Date/Time instances.
117
146
  # @return [Hash{String => Object}]
118
147
  # @api public
119
148
  #: (?type_cast: bool) -> Hash[String, untyped]
120
149
  def cells_hash(type_cast: false)
150
+ warn "[DEPRECATION] `Row#cells_hash` is deprecated; use `cells` instead.", uplevel: 1
121
151
  result = {}
122
152
  cells.each do |c|
123
153
  val = c.value
@@ -139,13 +169,34 @@ module Xlsxrb
139
169
  result
140
170
  end
141
171
 
172
+ # @deprecated Use {#hidden?} instead. Will be removed in 0.2.0.
173
+ #: () -> bool
174
+ def hidden
175
+ warn "[DEPRECATION] `Row#hidden` is deprecated; use `hidden?` instead.", uplevel: 1
176
+ super
177
+ end
178
+
179
+ # @deprecated Use {#collapsed?} instead. Will be removed in 0.2.0.
180
+ #: () -> bool
181
+ def collapsed
182
+ warn "[DEPRECATION] `Row#collapsed` is deprecated; use `collapsed?` instead.", uplevel: 1
183
+ super
184
+ end
185
+
186
+ # @deprecated Use {#custom_height?} instead. Will be removed in 0.2.0.
187
+ #: () -> bool
188
+ def custom_height
189
+ warn "[DEPRECATION] `Row#custom_height` is deprecated; use `custom_height?` instead.", uplevel: 1
190
+ super
191
+ end
192
+
142
193
  # Returns whether the row is hidden.
143
194
  #
144
195
  # @return [Boolean]
145
196
  # @api public
146
197
  #: () -> bool
147
198
  def hidden?
148
- hidden == true
199
+ @hidden
149
200
  end
150
201
 
151
202
  # Returns whether the row is collapsed.
@@ -154,7 +205,7 @@ module Xlsxrb
154
205
  # @api public
155
206
  #: () -> bool
156
207
  def collapsed?
157
- collapsed == true
208
+ @collapsed
158
209
  end
159
210
 
160
211
  # Returns whether custom height is set.
@@ -163,7 +214,36 @@ module Xlsxrb
163
214
  # @api public
164
215
  #: () -> bool
165
216
  def custom_height?
166
- custom_height == true
217
+ @custom_height
218
+ end
219
+
220
+ # Deconstructs row values for positional pattern matching.
221
+ #
222
+ # @return [Array<Object>]
223
+ # @api public
224
+ #: () -> Array[untyped]
225
+ def deconstruct
226
+ values
227
+ end
228
+
229
+ # Deconstructs row attributes for pattern matching.
230
+ #
231
+ # @param _keys [Array<Symbol>, nil]
232
+ # @return [Hash{Symbol => Object}]
233
+ # @api public
234
+ #: (Array[Symbol]? _keys) -> Hash[Symbol, untyped]
235
+ def deconstruct_keys(_keys)
236
+ {
237
+ index: index,
238
+ cells: cells,
239
+ values: values,
240
+ height: height,
241
+ hidden: hidden,
242
+ custom_height: custom_height,
243
+ collapsed: collapsed,
244
+ outline_level: outline_level,
245
+ style_index: style_index
246
+ }
167
247
  end
168
248
 
169
249
  # Returns a frozen Hash of row attributes.
@@ -184,14 +264,6 @@ module Xlsxrb
184
264
  h.freeze
185
265
  end
186
266
 
187
- # Returns style_index if present in unmapped_data.
188
- #
189
- # @return [Integer, nil]
190
- #: () -> Integer?
191
- def style_index
192
- unmapped_data[:style_index]
193
- end
194
-
195
267
  # Iterate over cells in this row.
196
268
  #
197
269
  # @example
@@ -232,8 +304,9 @@ module Xlsxrb
232
304
 
233
305
  # Convert row cells to an Array of raw values.
234
306
  #
307
+ # @deprecated Use {#values} to get an Array of cell values, or {#cells} for Cell objects. In 0.2.0, {#to_a} will conform to Enumerable#to_a and return Array<Cell>.
235
308
  # @example
236
- # row.to_a #=> ["ID", "Name", "Total"]
309
+ # row.values #=> ["ID", "Name", "Total"]
237
310
  #
238
311
  # @return [Array<Object>]
239
312
  # @api public
@@ -268,14 +341,17 @@ module Xlsxrb
268
341
  cells.all? { |c| c.value.nil? || (c.value.is_a?(String) && c.value.empty?) }
269
342
  end
270
343
 
271
- # Returns the cell at the given 0-based column index, or nil.
344
+ # Returns the cell at the given 0-based column index, column letter, or Symbol, or nil.
272
345
  #
273
- # @param column_index [Integer] 0-based column index.
346
+ # @param column_index [Integer, String, Symbol] 0-based column index or column letter/Symbol.
274
347
  # @return [Elements::Cell, nil]
275
348
  # @api public
276
- #: (Integer column_index) -> Elements::Cell?
349
+ #: (Integer | String | Symbol column_index) -> Elements::Cell?
277
350
  def cell_at(column_index)
278
- cells.find { |c| c.column_index == column_index }
351
+ col = column_index.is_a?(Integer) ? column_index : Cell._column_index(column_index)
352
+ return nil unless col
353
+
354
+ cells.find { |c| c.column_index == col }
279
355
  end
280
356
 
281
357
  # Returns cell values as an Array (sparse columns get nil).
@@ -342,7 +418,7 @@ module Xlsxrb
342
418
  # @return [Array<String, nil>]
343
419
  # @api public
344
420
  #: () -> Array[String?]
345
- def formulas
421
+ def formula_expressions
346
422
  return [] if cells.empty?
347
423
 
348
424
  max_col = cells.max_by(&:column_index).column_index
@@ -351,6 +427,13 @@ module Xlsxrb
351
427
  result
352
428
  end
353
429
 
430
+ # @deprecated Use {#formula_expressions} instead. Will be removed in 0.2.0.
431
+ #: () -> Array[String?]
432
+ def formulas
433
+ warn "[DEPRECATION] `Row#formulas` is deprecated; use `Row#formula_expressions` instead.", uplevel: 1
434
+ formula_expressions
435
+ end
436
+
354
437
  # Returns whether the row index is within valid OOXML range (0..1048575).
355
438
  #
356
439
  # @param index [Object]
@@ -370,6 +453,7 @@ module Xlsxrb
370
453
  # @param index [Integer]
371
454
  # @param cells [Array<Elements::Cell>]
372
455
  # @return [Array<String>] List of errors.
456
+ # @api private
373
457
  #: (untyped index, untyped cells) -> Array[String]
374
458
  def self.validate(index, cells)
375
459
  errs = []