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
@@ -14,10 +14,12 @@ 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
 
21
+ undef_method :hidden, :custom_height, :collapsed
22
+
21
23
  # @param index [Integer] 0-based row index.
22
24
  # @param cells [Array<Elements::Cell>] Cells in this row.
23
25
  # @param height [Float, Integer, nil] Row height in points.
@@ -25,127 +27,85 @@ module Xlsxrb
25
27
  # @param custom_height [Boolean] Whether custom height is set.
26
28
  # @param collapsed [Boolean] Whether the row is collapsed.
27
29
  # @param outline_level [Integer, nil] Grouping/outline level.
30
+ # @param style_index [Integer, nil] Default row style index.
28
31
  # @param unmapped_data [Hash] Additional metadata.
29
32
  # @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
33
+ #: (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
34
  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)
35
+ outline_level: nil, style_index: nil, unmapped_data: EMPTY_HASH, errors: nil)
33
36
  computed_errors = errors || self.class.validate(index, cells)
34
37
  computed_errors = computed_errors.freeze unless computed_errors.frozen?
35
38
  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)
39
+ @hidden = hidden ? true : false
40
+ @custom_height = custom_height ? true : false
41
+ @collapsed = collapsed ? true : false
42
+ super(**{ index: index, cells: cells, height: height, hidden: @hidden,
43
+ custom_height: @custom_height, collapsed: @collapsed, outline_level: outline_level,
44
+ style_index: style_index, unmapped_data: unmapped_data, errors: computed_errors })
39
45
  end
40
46
 
41
- # Access a cell by 0-based column index, or access row attributes via Symbol.
47
+ # Access a cell by 0-based column index, column letter/Symbol, or column range.
42
48
  #
43
49
  # @example
44
- # row[0] #=> Cell at column 0
45
- # row[:height] #=> 25.0
46
- # row[:cells] #=> [Cell, Cell, ...]
50
+ # row[0] #=> Cell at column 0 (or nil if sparse)
51
+ # row["A"] #=> Cell at column "A"
52
+ # row[:A] #=> Cell at column "A"
53
+ # row[0..2] #=> [Cell, Cell, ...]
47
54
  #
48
- # @param col_index [Integer, Symbol] Column index or attribute symbol.
49
- # @return [Elements::Cell, Object, nil]
55
+ # @param col_index_or_range [Integer, String, Symbol, Range<Integer>] Column index, column letter/Symbol, or column index range.
56
+ # @return [Elements::Cell, Array<Elements::Cell>, nil]
50
57
  # @api public
51
- #: (Integer | Symbol col_index) -> untyped
52
- def [](col_index)
53
- case col_index
54
- when Symbol
55
- case col_index
56
- when :cells then cells
57
- when :values then values
58
- when :raw_values then raw_values
59
- when :formatted_values then formatted_values
60
- when :formulas then formulas
61
- when :index then index
62
- when :height then height
63
- when :hidden then hidden
64
- when :custom_height then custom_height
65
- when :collapsed then collapsed
66
- when :outline_level then outline_level
67
- when :style_index then style_index
68
- when :hidden? then hidden?
69
- when :collapsed? then collapsed?
70
- when :custom_height? then custom_height?
71
- when :attributes then attributes
72
- when :cells_hash then cells_hash
73
- when :attrs
74
- h = { height: height, hidden: hidden, custom_height: custom_height, collapsed: collapsed, outline_level: outline_level }
75
- h[:style_index] = style_index if style_index
76
- h
77
- end
58
+ #: (Integer | String | Symbol col_index_or_range) -> Elements::Cell?
59
+ #: (Range[Integer] col_index_or_range) -> Array[Elements::Cell]
60
+ def [](col_index_or_range)
61
+ case col_index_or_range
62
+ when Range
63
+ cells.select { |c| col_index_or_range.cover?(c.column_index) }
78
64
  else
79
- cells[col_index]
65
+ cell_at(col_index_or_range)
80
66
  end
81
67
  end
82
68
 
83
- # Returns a new Row with missing cells padded up to the maximum column index.
69
+ # Returns a new Row with missing cells padded up to max_column (or maximum cell column index if nil).
84
70
  #
71
+ # @param max_column [Integer, nil] Maximum 0-based column index to pad up to.
85
72
  # @return [Elements::Row]
86
73
  # @api public
87
- #: () -> Elements::Row
88
- def pad_empty_cells
74
+ #: (?max_column: Integer?) -> Elements::Row
75
+ def padded(max_column: nil)
89
76
  return self if cells.empty?
90
77
 
91
- max_col = cells.map(&:column_index).max || 0
92
- return self if cells.size == max_col + 1
78
+ max_c = max_column || (cells.map(&:column_index).max || 0)
79
+ return self if max_column.nil? && cells.size == max_c + 1
93
80
 
94
81
  is_d1904 = cells.first ? cells.first.date1904? : false
95
82
  cell_map = {}
96
83
  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)
84
+ padded_cells = Array.new(max_c + 1)
85
+ (0..max_c).each do |c_idx|
86
+ padded_cells[c_idx] = cell_map[c_idx] || Cell.fast_create(index, c_idx, nil, nil, nil, nil, nil, is_d1904)
100
87
  end
101
88
  Row.new(
102
89
  index: index,
103
- cells: padded,
90
+ cells: padded_cells,
104
91
  height: height,
105
- hidden: hidden,
106
- custom_height: custom_height,
107
- collapsed: collapsed,
92
+ hidden: @hidden,
93
+ custom_height: @custom_height,
94
+ collapsed: @collapsed,
108
95
  outline_level: outline_level,
96
+ style_index: style_index,
109
97
  unmapped_data: unmapped_data,
110
98
  errors: errors
111
99
  )
112
100
  end
113
101
 
114
- # Returns a coordinate-keyed Hash of cell values (e.g. {"A1" => "value", "B1" => 123}).
115
- #
116
- # @param type_cast [Boolean] Whether to coerce date/time serial numbers into Date/Time instances.
117
- # @return [Hash{String => Object}]
118
- # @api public
119
- #: (?type_cast: bool) -> Hash[String, untyped]
120
- def cells_hash(type_cast: false)
121
- result = {}
122
- cells.each do |c|
123
- val = c.value
124
- if type_cast && val.is_a?(Numeric) && c.format_code
125
- fmt_type = NumberFormatter.format_type(c.format_code)
126
- begin
127
- case fmt_type
128
- when :date
129
- val = Ooxml::Utils.serial_to_date(val, date1904: c.date1904?)
130
- when :datetime, :time
131
- val = Ooxml::Utils.serial_to_datetime(val, date1904: c.date1904?)
132
- end
133
- rescue StandardError
134
- # Keep val numeric on failure
135
- end
136
- end
137
- result[c.ref] = val
138
- end
139
- result
140
- end
141
-
142
102
  # Returns whether the row is hidden.
143
103
  #
144
104
  # @return [Boolean]
145
105
  # @api public
146
106
  #: () -> bool
147
107
  def hidden?
148
- hidden == true
108
+ @hidden
149
109
  end
150
110
 
151
111
  # Returns whether the row is collapsed.
@@ -154,7 +114,7 @@ module Xlsxrb
154
114
  # @api public
155
115
  #: () -> bool
156
116
  def collapsed?
157
- collapsed == true
117
+ @collapsed
158
118
  end
159
119
 
160
120
  # Returns whether custom height is set.
@@ -163,7 +123,36 @@ module Xlsxrb
163
123
  # @api public
164
124
  #: () -> bool
165
125
  def custom_height?
166
- custom_height == true
126
+ @custom_height
127
+ end
128
+
129
+ # Deconstructs row values for positional pattern matching.
130
+ #
131
+ # @return [Array<Object>]
132
+ # @api public
133
+ #: () -> Array[untyped]
134
+ def deconstruct
135
+ values
136
+ end
137
+
138
+ # Deconstructs row attributes for pattern matching.
139
+ #
140
+ # @param _keys [Array<Symbol>, nil]
141
+ # @return [Hash{Symbol => Object}]
142
+ # @api public
143
+ #: (Array[Symbol]? _keys) -> Hash[Symbol, untyped]
144
+ def deconstruct_keys(_keys)
145
+ {
146
+ index: index,
147
+ cells: cells,
148
+ values: values,
149
+ height: height,
150
+ hidden: hidden?,
151
+ custom_height: custom_height?,
152
+ collapsed: collapsed?,
153
+ outline_level: outline_level,
154
+ style_index: style_index
155
+ }
167
156
  end
168
157
 
169
158
  # Returns a frozen Hash of row attributes.
@@ -175,23 +164,15 @@ module Xlsxrb
175
164
  h = {
176
165
  index: index,
177
166
  height: height,
178
- hidden: hidden,
179
- custom_height: custom_height,
180
- collapsed: collapsed,
167
+ hidden: hidden?,
168
+ custom_height: custom_height?,
169
+ collapsed: collapsed?,
181
170
  outline_level: outline_level
182
171
  }
183
172
  h[:style_index] = style_index if style_index
184
173
  h.freeze
185
174
  end
186
175
 
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
176
  # Iterate over cells in this row.
196
177
  #
197
178
  # @example
@@ -230,25 +211,6 @@ module Xlsxrb
230
211
  cells.each(&)
231
212
  end
232
213
 
233
- # Convert row cells to an Array of raw values.
234
- #
235
- # @example
236
- # row.to_a #=> ["ID", "Name", "Total"]
237
- #
238
- # @return [Array<Object>]
239
- # @api public
240
- #: () -> Array[untyped]
241
- def to_a
242
- return [] if cells.empty?
243
-
244
- max_col = cells.map(&:column_index).max
245
- arr = Array.new(max_col + 1)
246
- cells.each do |cell|
247
- arr[cell.column_index] = cell.value
248
- end
249
- arr
250
- end
251
-
252
214
  # Returns whether the row is valid according to OOXML specifications.
253
215
  #
254
216
  # @return [Boolean]
@@ -268,14 +230,17 @@ module Xlsxrb
268
230
  cells.all? { |c| c.value.nil? || (c.value.is_a?(String) && c.value.empty?) }
269
231
  end
270
232
 
271
- # Returns the cell at the given 0-based column index, or nil.
233
+ # Returns the cell at the given 0-based column index, column letter, or Symbol, or nil.
272
234
  #
273
- # @param column_index [Integer] 0-based column index.
235
+ # @param column_index [Integer, String, Symbol] 0-based column index or column letter/Symbol.
274
236
  # @return [Elements::Cell, nil]
275
237
  # @api public
276
- #: (Integer column_index) -> Elements::Cell?
238
+ #: (Integer | String | Symbol column_index) -> Elements::Cell?
277
239
  def cell_at(column_index)
278
- cells.find { |c| c.column_index == column_index }
240
+ col = column_index.is_a?(Integer) ? column_index : Cell._column_index(column_index)
241
+ return nil unless col
242
+
243
+ cells.find { |c| c.column_index == col }
279
244
  end
280
245
 
281
246
  # Returns cell values as an Array (sparse columns get nil).
@@ -342,7 +307,7 @@ module Xlsxrb
342
307
  # @return [Array<String, nil>]
343
308
  # @api public
344
309
  #: () -> Array[String?]
345
- def formulas
310
+ def formula_expressions
346
311
  return [] if cells.empty?
347
312
 
348
313
  max_col = cells.max_by(&:column_index).column_index
@@ -370,6 +335,7 @@ module Xlsxrb
370
335
  # @param index [Integer]
371
336
  # @param cells [Array<Elements::Cell>]
372
337
  # @return [Array<String>] List of errors.
338
+ # @api private
373
339
  #: (untyped index, untyped cells) -> Array[String]
374
340
  def self.validate(index, cells)
375
341
  errs = []