xlsxrb 0.1.16 → 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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +157 -1
  3. data/README.md +81 -21
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +31 -61
  5. data/lib/xlsxrb/chart_builder.rb +619 -98
  6. data/lib/xlsxrb/dsl_helpers.rb +583 -15
  7. data/lib/xlsxrb/elements/cell.rb +265 -53
  8. data/lib/xlsxrb/elements/column.rb +95 -6
  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 +243 -7
  15. data/lib/xlsxrb/elements/workbook.rb +202 -65
  16. data/lib/xlsxrb/elements/worksheet.rb +728 -85
  17. data/lib/xlsxrb/elements.rb +1 -0
  18. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +32 -3
  19. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +194 -26
  20. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +372 -18
  21. data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +33 -14
  22. data/lib/xlsxrb/ooxml/reader.rb +92 -0
  23. data/lib/xlsxrb/ooxml/styles_parser.rb +29 -14
  24. data/lib/xlsxrb/ooxml/workbook_parser.rb +18 -3
  25. data/lib/xlsxrb/ooxml/workbook_writer.rb +351 -77
  26. data/lib/xlsxrb/ooxml/worksheet_parser.rb +18 -4
  27. data/lib/xlsxrb/ooxml/worksheet_writer.rb +349 -103
  28. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +584 -443
  29. data/lib/xlsxrb/ooxml/writer/features_xml.rb +52 -14
  30. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +42 -18
  31. data/lib/xlsxrb/ooxml/writer.rb +288 -35
  32. data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
  33. data/lib/xlsxrb/ooxml/zip_reader.rb +47 -10
  34. data/lib/xlsxrb/ooxml/zip_writer.rb +15 -5
  35. data/lib/xlsxrb/stream_row.rb +97 -18
  36. data/lib/xlsxrb/stream_sheet.rb +827 -157
  37. data/lib/xlsxrb/stream_writer.rb +2766 -375
  38. data/lib/xlsxrb/style_builder.rb +821 -105
  39. data/lib/xlsxrb/utils.rb +82 -57
  40. data/lib/xlsxrb/version.rb +1 -1
  41. data/lib/xlsxrb/workbook_builder.rb +542 -62
  42. data/lib/xlsxrb/worksheet_builder.rb +2279 -209
  43. data/lib/xlsxrb.rb +1539 -344
  44. data/sig/generated/xlsxrb/chart_builder.rbs +336 -79
  45. data/sig/generated/xlsxrb/dsl_helpers.rbs +137 -4
  46. data/sig/generated/xlsxrb/elements/cell.rbs +104 -15
  47. data/sig/generated/xlsxrb/elements/column.rbs +62 -3
  48. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +49 -7
  49. data/sig/generated/xlsxrb/elements/image.rbs +30 -8
  50. data/sig/generated/xlsxrb/elements/metadata.rbs +1587 -0
  51. data/sig/generated/xlsxrb/elements/row.rbs +61 -21
  52. data/sig/generated/xlsxrb/elements/styles.rbs +96 -58
  53. data/sig/generated/xlsxrb/elements/types.rbs +29 -0
  54. data/sig/generated/xlsxrb/elements/workbook.rbs +92 -44
  55. data/sig/generated/xlsxrb/elements/worksheet.rbs +259 -38
  56. data/sig/generated/xlsxrb/ooxml/reader/listeners/core_listeners.rbs +2 -0
  57. data/sig/generated/xlsxrb/ooxml/reader/listeners/drawing_listeners.rbs +74 -2
  58. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +97 -0
  59. data/sig/generated/xlsxrb/ooxml/reader/listeners/style_listeners.rbs +3 -0
  60. data/sig/generated/xlsxrb/ooxml/reader.rbs +20 -0
  61. data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +3 -0
  62. data/sig/generated/xlsxrb/ooxml/workbook_parser.rbs +8 -0
  63. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -4
  64. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +1 -1
  65. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +12 -1
  66. data/sig/generated/xlsxrb/ooxml/writer/drawing_xml.rbs +16 -0
  67. data/sig/generated/xlsxrb/ooxml/writer.rbs +45 -8
  68. data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
  69. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +2 -2
  70. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -2
  71. data/sig/generated/xlsxrb/stream_row.rbs +48 -14
  72. data/sig/generated/xlsxrb/stream_sheet.rbs +398 -89
  73. data/sig/generated/xlsxrb/stream_writer.rbs +1601 -219
  74. data/sig/generated/xlsxrb/style_builder.rbs +403 -75
  75. data/sig/generated/xlsxrb/utils.rbs +49 -13
  76. data/sig/generated/xlsxrb/workbook_builder.rbs +308 -26
  77. data/sig/generated/xlsxrb/worksheet_builder.rbs +1142 -96
  78. data/sig/generated/xlsxrb.rbs +321 -52
  79. metadata +3 -1
@@ -13,7 +13,7 @@ module Xlsxrb
13
13
  #
14
14
  # @api public
15
15
  # rubocop:disable Style/DataInheritance -- Required as class syntax for mutant subject matcher
16
- class Workbook < Data.define(:sheets, :shared_strings, :styles, :unmapped_data, :errors, :defined_names)
16
+ class Workbook < Data.define(:sheets, :shared_strings, :styles, :unmapped_data, :errors, :defined_names, :workbook_properties, :core_properties, :app_properties, :workbook_protection, :custom_properties)
17
17
  # rubocop:enable Style/DataInheritance
18
18
  [Enumerable].each { |m| include m }
19
19
 
@@ -23,18 +23,76 @@ module Xlsxrb
23
23
  # @param unmapped_data [Hash] Additional metadata for round-tripping.
24
24
  # @param errors [Array<String>, nil] Validation errors.
25
25
  # @param defined_names [Array<Hash>, nil] Defined names list.
26
+ # @param workbook_properties [Elements::WorkbookProperties, Hash, nil] Workbook properties.
27
+ # @param core_properties [Elements::CoreProperties, Hash, nil] Core properties.
28
+ # @param app_properties [Elements::AppProperties, Hash, nil] App properties.
29
+ # @param workbook_protection [Elements::WorkbookProtection, Hash, nil] Workbook protection.
30
+ # @param custom_properties [Hash, Array, nil] Custom properties.
26
31
  # @param date1904 [Boolean] Whether the workbook uses the 1904 date system.
27
- #: (?sheets: Array[Elements::Worksheet | StreamSheet], ?shared_strings: Array[String], ?styles: Elements::Styles | Hash[untyped, untyped], ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?defined_names: Array[Hash[Symbol, untyped]]?, ?date1904: bool) -> void
28
- def initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil, defined_names: nil, date1904: false)
29
- dns = defined_names || unmapped_data[:defined_names] || unmapped_data.dig(:facade, :defined_names) || []
32
+ #: (?sheets: Array[Elements::Worksheet | StreamSheet], ?shared_strings: Array[String], ?styles: Elements::Styles | Hash[untyped, untyped], ?unmapped_data: Hash[untyped, untyped], ?errors: Array[String]?, ?defined_names: Array[untyped]?, ?workbook_properties: (Elements::WorkbookProperties | Hash[Symbol, untyped])?, ?core_properties: (Elements::CoreProperties | Hash[Symbol, untyped])?, ?app_properties: (Elements::AppProperties | Hash[Symbol, untyped])?, ?workbook_protection: (Elements::WorkbookProtection | Hash[Symbol, untyped])?, ?custom_properties: (Hash[String, Elements::CustomProperty] | Hash[untyped, untyped] | Array[untyped])?, ?date1904: bool) -> void
33
+ def initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil, defined_names: nil,
34
+ workbook_properties: nil, core_properties: nil, app_properties: nil, workbook_protection: nil,
35
+ custom_properties: nil, date1904: false)
36
+ dns = (defined_names || unmapped_data[:defined_names] || unmapped_data.dig(:facade, :defined_names) || []).map do |dn|
37
+ dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn)
38
+ end
30
39
  computed_errors = errors || self.class.validate(sheets)
31
40
  unmapped = unmapped_data
32
- if date1904 && !unmapped.dig(:workbook_properties, :date1904) && !unmapped.dig(:facade, :workbook_properties, :date1904)
41
+
42
+ raw_wb_props = workbook_properties || unmapped[:workbook_properties] || unmapped.dig(:facade, :workbook_properties) || {}
43
+ norm_wb_props = raw_wb_props.is_a?(Elements::WorkbookProperties) ? raw_wb_props : Elements::WorkbookProperties.new(**raw_wb_props)
44
+ if date1904 && !norm_wb_props.date1904?
45
+ norm_wb_props = norm_wb_props.with(date1904: true)
33
46
  unmapped = unmapped.dup
34
47
  unmapped[:workbook_properties] = (unmapped[:workbook_properties] || {}).merge(date1904: true)
35
48
  end
36
- super(sheets: sheets.freeze, shared_strings: shared_strings.freeze, styles: styles,
37
- unmapped_data: unmapped, errors: computed_errors.freeze, defined_names: dns.freeze)
49
+
50
+ raw_core = core_properties || unmapped[:core_properties] || unmapped.dig(:facade, :core_properties) || {}
51
+ norm_core = raw_core.is_a?(Elements::CoreProperties) ? raw_core : Elements::CoreProperties.new(**raw_core)
52
+
53
+ raw_app = app_properties || unmapped[:app_properties] || unmapped.dig(:facade, :app_properties) || {}
54
+ norm_app = raw_app.is_a?(Elements::AppProperties) ? raw_app : Elements::AppProperties.new(**raw_app)
55
+
56
+ raw_prot = workbook_protection || unmapped[:workbook_protection] || unmapped.dig(:facade, :workbook_protection)
57
+ norm_prot = if raw_prot.nil?
58
+ nil
59
+ elsif raw_prot.is_a?(Elements::WorkbookProtection)
60
+ raw_prot
61
+ else
62
+ Elements::WorkbookProtection.new(**raw_prot)
63
+ end
64
+
65
+ raw_custom = custom_properties || unmapped[:custom_properties] || unmapped.dig(:facade, :custom_properties)
66
+ norm_custom = if raw_custom.nil?
67
+ nil
68
+ elsif raw_custom.is_a?(Hash)
69
+ raw_custom.map do |k, v|
70
+ if v.is_a?(Elements::CustomProperty)
71
+ v
72
+ elsif v.is_a?(Hash)
73
+ Elements::CustomProperty.new(name: (v[:name] || v["name"] || k).to_s, value: v[:value] || v["value"], type: v[:type] || v["type"])
74
+ else
75
+ Elements::CustomProperty.new(name: k.to_s, value: v)
76
+ end
77
+ end
78
+ elsif raw_custom.is_a?(Array)
79
+ raw_custom.map do |v|
80
+ if v.is_a?(Hash)
81
+ Elements::CustomProperty.new(name: (v[:name] || v["name"]).to_s, value: v[:value] || v["value"], type: v[:type] || v["type"])
82
+ else
83
+ v
84
+ end
85
+ end
86
+ else
87
+ []
88
+ end.to_h { |cp| [cp.name.to_s, cp] }.freeze
89
+
90
+ norm_styles = styles.is_a?(Elements::Styles) ? styles : Elements::Styles.new(styles || {})
91
+
92
+ super(sheets: sheets.freeze, shared_strings: shared_strings.freeze, styles: norm_styles,
93
+ unmapped_data: unmapped, errors: computed_errors.freeze, defined_names: dns.freeze,
94
+ workbook_properties: norm_wb_props, core_properties: norm_core, app_properties: norm_app,
95
+ workbook_protection: norm_prot, custom_properties: norm_custom)
38
96
  end
39
97
 
40
98
  # Iterate over worksheets.
@@ -52,7 +110,14 @@ module Xlsxrb
52
110
  def each(&)
53
111
  sheets.each(&)
54
112
  end
55
- alias each_sheet each
113
+
114
+ # @deprecated Use {#each} instead. Will be removed in 0.2.0.
115
+ #: () { (Elements::Worksheet | StreamSheet) -> void } -> void
116
+ #: () -> Enumerator[Elements::Worksheet | StreamSheet, void]
117
+ def each_sheet(&)
118
+ warn "[DEPRECATION] #each_sheet is deprecated; use #each instead.", uplevel: 1
119
+ each(&)
120
+ end
56
121
 
57
122
  # Returns whether the workbook is valid according to ECMA-376 rules.
58
123
  #
@@ -62,77 +127,100 @@ module Xlsxrb
62
127
  errors.empty?
63
128
  end
64
129
 
65
- # Returns workbook-level properties (e.g. date1904, active_tab, full_calc_on_load), or an empty hash.
66
- #
67
- # @return [Hash<Symbol, untyped>]
68
- # @api public
69
- #: () -> Hash[Symbol, untyped]
70
- def workbook_properties
71
- unmapped_data[:workbook_properties] || unmapped_data.dig(:facade, :workbook_properties) || {}
72
- end
73
-
74
130
  # Returns whether the workbook uses the 1904 date system.
75
131
  #
76
132
  # @return [Boolean]
77
133
  # @api public
78
134
  #: () -> bool
79
135
  def date1904?
80
- workbook_properties[:date1904] ? true : false
136
+ workbook_properties.date1904?
81
137
  end
82
138
 
83
- # Returns the 0-based active worksheet tab index from workbookView, or nil if not set.
139
+ # Returns the internal VBA or application code name of the workbook, or nil if not specified.
84
140
  #
141
+ # @return [String, nil]
142
+ # @api public
143
+ #: () -> String?
144
+ def code_name
145
+ workbook_properties.code_name
146
+ end
147
+
148
+ # Returns the 0-based active worksheet index from workbookView, or nil if not set.
149
+ #
150
+ # @return [Integer, nil]
151
+ # @api public
152
+ #: () -> Integer?
153
+ def active_sheet_index
154
+ workbook_properties.active_sheet
155
+ end
156
+
157
+ # @deprecated Use {#active_sheet_index} or {#active_sheet} instead.
85
158
  # @return [Integer, nil]
86
159
  # @api public
87
160
  #: () -> Integer?
88
161
  def active_tab
89
- workbook_properties[:active_tab]
162
+ warn "[Xlsxrb DEPRECATION] Workbook#active_tab is deprecated and will be removed in v0.2.0. Use #active_sheet_index or #active_sheet instead.", uplevel: 1
163
+ active_sheet_index
90
164
  end
91
165
 
92
- # Returns whether Excel is instructed to perform a full recalculation when loading the workbook.
166
+ # Returns the active worksheet in the workbook, or the first worksheet if not set.
93
167
  #
94
- # @return [Boolean]
168
+ # @return [Elements::Worksheet, StreamSheet, nil]
95
169
  # @api public
96
- #: () -> bool
97
- def full_calc_on_load?
98
- workbook_properties[:full_calc_on_load] ? true : false
170
+ #: () -> (Elements::Worksheet | StreamSheet)?
171
+ def active_sheet
172
+ idx = active_sheet_index
173
+ idx ? sheets[idx] : sheets.first
99
174
  end
100
- alias full_calc_on_load full_calc_on_load?
101
175
 
102
- # Returns core document properties (e.g. title, creator, created), or an empty hash.
176
+ # Returns the index of the first sheet tab visible in the sheet tab scroll area.
103
177
  #
104
- # @return [Hash<Symbol, untyped>]
178
+ # @return [Integer, nil]
105
179
  # @api public
106
- #: () -> Hash[Symbol, untyped]
107
- def core_properties
108
- unmapped_data[:core_properties] || unmapped_data.dig(:facade, :core_properties) || {}
180
+ #: () -> Integer?
181
+ def first_sheet
182
+ workbook_properties.first_sheet
109
183
  end
110
184
 
111
- # Returns application properties (e.g. app_name, doc_security), or an empty hash.
185
+ # Returns whether sheet tabs are displayed in the workbook window.
112
186
  #
113
- # @return [Hash<Symbol, untyped>]
187
+ # @return [Boolean]
114
188
  # @api public
115
- #: () -> Hash[Symbol, untyped]
116
- def app_properties
117
- unmapped_data[:app_properties] || unmapped_data.dig(:facade, :app_properties) || {}
189
+ #: () -> bool
190
+ def show_sheet_tabs?
191
+ workbook_properties.show_sheet_tabs != false
118
192
  end
119
193
 
120
- # Returns custom document properties as an array of property hashes, or an empty array.
194
+ # @deprecated Use {#show_sheet_tabs?} instead. Will be removed in 0.2.0.
195
+ #: () -> bool
196
+ def show_sheet_tabs
197
+ warn "[DEPRECATION] #show_sheet_tabs is deprecated; use #show_sheet_tabs? instead.", uplevel: 1
198
+ show_sheet_tabs?
199
+ end
200
+
201
+ # Returns the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
121
202
  #
122
- # @return [Array<Hash[Symbol, untyped]>]
203
+ # @return [Integer, nil]
123
204
  # @api public
124
- #: () -> Array[Hash[Symbol, untyped]]
125
- def custom_properties
126
- unmapped_data[:custom_properties] || unmapped_data.dig(:facade, :custom_properties) || []
205
+ #: () -> Integer?
206
+ def tab_ratio
207
+ workbook_properties.tab_ratio
127
208
  end
128
209
 
129
- # Returns workbook protection settings as a hash, or nil if unprotected.
210
+ # Returns whether Excel is instructed to perform a full recalculation when loading the workbook.
130
211
  #
131
- # @return [Hash[Symbol, untyped], nil]
212
+ # @return [Boolean]
132
213
  # @api public
133
- #: () -> Hash[Symbol, untyped]?
134
- def workbook_protection
135
- unmapped_data[:workbook_protection] || unmapped_data.dig(:facade, :workbook_protection)
214
+ #: () -> bool
215
+ def full_calc_on_load?
216
+ workbook_properties.full_calc_on_load ? true : false
217
+ end
218
+
219
+ # @deprecated Use {#full_calc_on_load?} instead. Will be removed in 0.2.0.
220
+ #: () -> bool
221
+ def full_calc_on_load
222
+ warn "[DEPRECATION] #full_calc_on_load is deprecated; use #full_calc_on_load? instead.", uplevel: 1
223
+ full_calc_on_load?
136
224
  end
137
225
 
138
226
  # Returns the defined name matching name (and optionally sheet), or nil.
@@ -142,21 +230,26 @@ module Xlsxrb
142
230
  # wb.defined_name("Print_Area", sheet: "Sheet1")
143
231
  #
144
232
  # @param name [String] The defined name.
145
- # @param sheet [String, Integer, nil] Optional sheet name or 0-based index.
146
- # @return [Hash[Symbol, untyped], nil]
233
+ # @param sheet [String, Symbol, Integer, nil] Optional sheet name or 0-based index.
234
+ # @return [Elements::DefinedName, nil]
147
235
  # @api public
148
- #: (String name, ?sheet: (String | Integer)?) -> Hash[Symbol, untyped]?
236
+ #: (String name, ?sheet: (String | Symbol | Integer)?) -> Elements::DefinedName?
149
237
  def defined_name(name, sheet: nil)
150
238
  sheet_idx = case sheet
151
239
  when Integer then sheet
152
- when String then sheets.find_index { |s| s.name == sheet }
240
+ when String, Symbol
241
+ target = sheet.to_s
242
+ sheets.find_index { |s| s.name == target }
153
243
  end
154
244
 
245
+ dn_name = ->(dn) { dn.respond_to?(:name) ? dn.name : dn[:name] }
246
+ dn_local = ->(dn) { dn.respond_to?(:local_sheet_id) ? dn.local_sheet_id : dn[:local_sheet_id] }
247
+
155
248
  if sheet_idx
156
- defined_names.find { |dn| dn[:name] == name && dn[:local_sheet_id] == sheet_idx }
249
+ defined_names.find { |dn| dn_name.call(dn) == name && dn_local.call(dn) == sheet_idx }
157
250
  else
158
- defined_names.find { |dn| dn[:name] == name && dn[:local_sheet_id].nil? } ||
159
- defined_names.find { |dn| dn[:name] == name }
251
+ defined_names.find { |dn| dn_name.call(dn) == name && dn_local.call(dn).nil? } ||
252
+ defined_names.find { |dn| dn_name.call(dn) == name }
160
253
  end
161
254
  end
162
255
 
@@ -165,17 +258,19 @@ module Xlsxrb
165
258
  # @example
166
259
  # wb.sheet(0)
167
260
  # wb.sheet("Sales")
261
+ # wb.sheet(:Sales)
168
262
  #
169
- # @param identifier [Integer, String, untyped] 0-based index or sheet name.
263
+ # @param identifier [Integer, String, Symbol, untyped] 0-based index or sheet name.
170
264
  # @return [Elements::Worksheet, StreamSheet, nil]
171
265
  # @api public
172
- #: (?Integer | String | untyped identifier) -> (Elements::Worksheet | StreamSheet)?
266
+ #: (?Integer | String | Symbol | untyped identifier) -> (Elements::Worksheet | StreamSheet)?
173
267
  def sheet(identifier = 0)
174
268
  case identifier
175
269
  when Integer
176
270
  sheets[identifier]
177
- when String
178
- sheets.find { |s| s.name == identifier }
271
+ when String, Symbol
272
+ target = identifier.to_s
273
+ sheets.find { |s| s.name == target }
179
274
  end
180
275
  end
181
276
  alias [] sheet
@@ -218,22 +313,39 @@ module Xlsxrb
218
313
  target_sheet.images_at(ref_or_row, col)
219
314
  end
220
315
 
316
+ # Internal load helper without deprecation warning.
317
+ # @api private
318
+ #: () -> Elements::Workbook
319
+ def _load
320
+ loaded_sheets = sheets.map do |s|
321
+ s.respond_to?(:load) ? s.load : s
322
+ end
323
+ close
324
+ with(sheets: loaded_sheets)
325
+ end
326
+
221
327
  # Loads all sheets into memory, returning an Elements::Workbook where every
222
328
  # worksheet is a fully-parsed Elements::Worksheet supporting coordinate random access.
223
329
  #
224
330
  # @example
225
- # wb = Xlsxrb.read("file.xlsx").load
331
+ # wb = Xlsxrb.load("file.xlsx")
226
332
  # puts wb["Sheet1"]["A1"].value
227
333
  #
334
+ # @deprecated Use {Xlsxrb.load} instead. Will be removed in 0.2.0.
228
335
  # @return [Elements::Workbook]
229
336
  # @api public
230
337
  #: () -> Elements::Workbook
231
338
  def load
232
- loaded_sheets = sheets.map { |s| s.respond_to?(:load) ? s.load : s }
233
- close
234
- with(sheets: loaded_sheets)
339
+ warn "[DEPRECATION] Elements::Workbook#load is deprecated; use Xlsxrb.load(source) instead.", uplevel: 1
340
+ _load
341
+ end
342
+
343
+ # @deprecated Use {Xlsxrb.load} instead. Will be removed in 0.2.0.
344
+ #: () -> Elements::Workbook
345
+ def to_workbook
346
+ warn "[DEPRECATION] #to_workbook is deprecated; use Xlsxrb.load(source) instead.", uplevel: 1
347
+ _load
235
348
  end
236
- alias to_workbook load
237
349
 
238
350
  # Closes any streaming resources associated with worksheets.
239
351
  #
@@ -241,6 +353,10 @@ module Xlsxrb
241
353
  # @api public
242
354
  #: () -> void
243
355
  def close
356
+ if sheets.all?(Elements::Worksheet)
357
+ Xlsxrb::Deprecation.warn("Elements::Workbook#close is deprecated; in-memory workbooks do not hold file handles.", uplevel: 1)
358
+ return nil
359
+ end
244
360
  sheets.each { |s| s.close if s.respond_to?(:close) }
245
361
  nil
246
362
  end
@@ -248,6 +364,7 @@ module Xlsxrb
248
364
  # Returns a new Workbook with the specified sheet updated.
249
365
  # Yields the matched worksheet to the block, which must return a new Worksheet.
250
366
  #
367
+ # @deprecated Use {Xlsxrb.read} + {Xlsxrb.write} for streaming transformations or {Xlsxrb.write} DSL for report generation. Will be removed in 0.2.0.
251
368
  # @example
252
369
  # new_wb = wb.update_sheet("Sheet1") do |sheet|
253
370
  # sheet.update_cell("A1", value: "New Title")
@@ -261,6 +378,7 @@ module Xlsxrb
261
378
  # @api public
262
379
  #: (Integer | String identifier) ?{ (Elements::Worksheet) -> Elements::Worksheet } -> Elements::Workbook
263
380
  def update_sheet(identifier)
381
+ warn "[DEPRECATION] Elements::Workbook#update_sheet is deprecated and will be removed in 0.2.0; use Xlsxrb.read + Xlsxrb.write for streaming transformations or Xlsxrb.write DSL for report generation.", uplevel: 1
264
382
  raise ArgumentError, "block is required" unless block_given?
265
383
 
266
384
  sheet_to_update = sheet(identifier)
@@ -289,17 +407,36 @@ module Xlsxrb
289
407
  # wb.save("output.xlsx")
290
408
  #
291
409
  # @param filepath [String, IO, StringIO] Destination file path or IO stream.
410
+ # @param shared_strings [Boolean] Whether to intern strings into a shared strings table.
411
+ # @param compression_level [Integer, nil] ZIP deflate compression level (0..9).
412
+ # @param options [Hash] Optional write options forwarded to {Xlsxrb.write} (e.g. `password:`, `encryption_mode:`).
292
413
  # @return [void]
293
414
  # @api public
294
- #: (String | IO | StringIO filepath) -> void
295
- def save(filepath)
296
- Xlsxrb.write(filepath, self)
415
+ #: (String | IO | StringIO filepath, ?shared_strings: bool, ?compression_level: Integer?, **untyped options) -> void
416
+ def save(filepath, shared_strings: true, compression_level: nil, **options)
417
+ Xlsxrb.write(filepath, self, shared_strings: shared_strings, compression_level: compression_level, **options)
418
+ end
419
+
420
+ # Serializes the workbook to a binary XLSX data string in memory.
421
+ #
422
+ # @example Export workbook bytes
423
+ # data = wb.to_data
424
+ #
425
+ # @param shared_strings [Boolean] Whether to intern strings into a shared strings table.
426
+ # @param compression_level [Integer, nil] ZIP deflate compression level (0..9).
427
+ # @param options [Hash] Additional write options forwarded to {Xlsxrb.write} (e.g. `password:`, `encryption_mode:`).
428
+ # @return [String] Binary XLSX data.
429
+ # @api public
430
+ #: (?shared_strings: bool, ?compression_level: Integer?, **untyped options) -> String
431
+ def to_data(shared_strings: true, compression_level: nil, **options)
432
+ Xlsxrb.write(self, shared_strings: shared_strings, compression_level: compression_level, **options)
297
433
  end
298
434
 
299
435
  # Validates workbook structure according to OOXML specifications.
300
436
  #
301
437
  # @param sheets [Array<Elements::Worksheet>]
302
438
  # @return [Array<String>] List of error messages.
439
+ # @api private
303
440
  #: (untyped sheets) -> Array[String]
304
441
  def self.validate(sheets)
305
442
  errs = []