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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +93 -1
- data/README.md +79 -19
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +31 -61
- data/lib/xlsxrb/chart_builder.rb +595 -113
- data/lib/xlsxrb/dsl_helpers.rb +72 -20
- data/lib/xlsxrb/elements/cell.rb +243 -56
- data/lib/xlsxrb/elements/column.rb +94 -5
- data/lib/xlsxrb/elements/coordinate_access.rb +108 -10
- data/lib/xlsxrb/elements/image.rb +81 -15
- data/lib/xlsxrb/elements/metadata.rb +2733 -0
- data/lib/xlsxrb/elements/row.rb +130 -46
- data/lib/xlsxrb/elements/styles.rb +184 -76
- data/lib/xlsxrb/elements/types.rb +242 -7
- data/lib/xlsxrb/elements/workbook.rb +171 -82
- data/lib/xlsxrb/elements/worksheet.rb +598 -102
- data/lib/xlsxrb/elements.rb +1 -0
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +12 -4
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -2
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +264 -17
- data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
- data/lib/xlsxrb/ooxml/reader.rb +4 -0
- data/lib/xlsxrb/ooxml/styles_parser.rb +10 -3
- data/lib/xlsxrb/ooxml/workbook_writer.rb +90 -36
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +2 -2
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +64 -19
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +5 -2
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +26 -5
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +19 -9
- data/lib/xlsxrb/ooxml/writer.rb +22 -6
- data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
- data/lib/xlsxrb/ooxml/zip_reader.rb +47 -10
- data/lib/xlsxrb/stream_row.rb +97 -18
- data/lib/xlsxrb/stream_sheet.rb +706 -190
- data/lib/xlsxrb/stream_writer.rb +1893 -374
- data/lib/xlsxrb/style_builder.rb +774 -111
- data/lib/xlsxrb/utils.rb +66 -106
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +380 -83
- data/lib/xlsxrb/worksheet_builder.rb +1210 -233
- data/lib/xlsxrb.rb +1266 -326
- data/sig/generated/xlsxrb/chart_builder.rbs +319 -90
- data/sig/generated/xlsxrb/dsl_helpers.rbs +20 -2
- data/sig/generated/xlsxrb/elements/cell.rbs +92 -20
- data/sig/generated/xlsxrb/elements/column.rbs +61 -2
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +49 -7
- data/sig/generated/xlsxrb/elements/image.rbs +30 -8
- data/sig/generated/xlsxrb/elements/metadata.rbs +1587 -0
- data/sig/generated/xlsxrb/elements/row.rbs +61 -21
- data/sig/generated/xlsxrb/elements/styles.rbs +96 -58
- data/sig/generated/xlsxrb/elements/types.rbs +27 -0
- data/sig/generated/xlsxrb/elements/workbook.rbs +63 -52
- data/sig/generated/xlsxrb/elements/worksheet.rbs +192 -51
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -0
- data/sig/generated/xlsxrb/ooxml/reader.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/writer.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +2 -2
- data/sig/generated/xlsxrb/stream_row.rbs +48 -14
- data/sig/generated/xlsxrb/stream_sheet.rbs +324 -115
- data/sig/generated/xlsxrb/stream_writer.rbs +1068 -255
- data/sig/generated/xlsxrb/style_builder.rbs +389 -79
- data/sig/generated/xlsxrb/utils.rbs +34 -18
- data/sig/generated/xlsxrb/workbook_builder.rbs +234 -34
- data/sig/generated/xlsxrb/worksheet_builder.rbs +628 -126
- data/sig/generated/xlsxrb.rbs +257 -49
- 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,
|
|
29
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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,22 +127,13 @@ 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
|
|
136
|
+
workbook_properties.date1904?
|
|
81
137
|
end
|
|
82
138
|
|
|
83
139
|
# Returns the internal VBA or application code name of the workbook, or nil if not specified.
|
|
@@ -86,16 +142,25 @@ module Xlsxrb
|
|
|
86
142
|
# @api public
|
|
87
143
|
#: () -> String?
|
|
88
144
|
def code_name
|
|
89
|
-
workbook_properties
|
|
145
|
+
workbook_properties.code_name
|
|
90
146
|
end
|
|
91
147
|
|
|
92
|
-
# Returns the 0-based active worksheet
|
|
148
|
+
# Returns the 0-based active worksheet index from workbookView, or nil if not set.
|
|
93
149
|
#
|
|
94
150
|
# @return [Integer, nil]
|
|
95
151
|
# @api public
|
|
96
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.
|
|
158
|
+
# @return [Integer, nil]
|
|
159
|
+
# @api public
|
|
160
|
+
#: () -> Integer?
|
|
97
161
|
def active_tab
|
|
98
|
-
|
|
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
|
|
99
164
|
end
|
|
100
165
|
|
|
101
166
|
# Returns the active worksheet in the workbook, or the first worksheet if not set.
|
|
@@ -104,7 +169,7 @@ module Xlsxrb
|
|
|
104
169
|
# @api public
|
|
105
170
|
#: () -> (Elements::Worksheet | StreamSheet)?
|
|
106
171
|
def active_sheet
|
|
107
|
-
idx =
|
|
172
|
+
idx = active_sheet_index
|
|
108
173
|
idx ? sheets[idx] : sheets.first
|
|
109
174
|
end
|
|
110
175
|
|
|
@@ -114,7 +179,7 @@ module Xlsxrb
|
|
|
114
179
|
# @api public
|
|
115
180
|
#: () -> Integer?
|
|
116
181
|
def first_sheet
|
|
117
|
-
workbook_properties
|
|
182
|
+
workbook_properties.first_sheet
|
|
118
183
|
end
|
|
119
184
|
|
|
120
185
|
# Returns whether sheet tabs are displayed in the workbook window.
|
|
@@ -123,10 +188,15 @@ module Xlsxrb
|
|
|
123
188
|
# @api public
|
|
124
189
|
#: () -> bool
|
|
125
190
|
def show_sheet_tabs?
|
|
126
|
-
|
|
127
|
-
|
|
191
|
+
workbook_properties.show_sheet_tabs != false
|
|
192
|
+
end
|
|
193
|
+
|
|
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?
|
|
128
199
|
end
|
|
129
|
-
alias show_sheet_tabs show_sheet_tabs?
|
|
130
200
|
|
|
131
201
|
# Returns the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
|
|
132
202
|
#
|
|
@@ -134,7 +204,7 @@ module Xlsxrb
|
|
|
134
204
|
# @api public
|
|
135
205
|
#: () -> Integer?
|
|
136
206
|
def tab_ratio
|
|
137
|
-
workbook_properties
|
|
207
|
+
workbook_properties.tab_ratio
|
|
138
208
|
end
|
|
139
209
|
|
|
140
210
|
# Returns whether Excel is instructed to perform a full recalculation when loading the workbook.
|
|
@@ -143,44 +213,14 @@ module Xlsxrb
|
|
|
143
213
|
# @api public
|
|
144
214
|
#: () -> bool
|
|
145
215
|
def full_calc_on_load?
|
|
146
|
-
workbook_properties
|
|
147
|
-
end
|
|
148
|
-
alias full_calc_on_load full_calc_on_load?
|
|
149
|
-
|
|
150
|
-
# Returns core document properties (e.g. title, creator, created), or an empty hash.
|
|
151
|
-
#
|
|
152
|
-
# @return [Hash<Symbol, untyped>]
|
|
153
|
-
# @api public
|
|
154
|
-
#: () -> Hash[Symbol, untyped]
|
|
155
|
-
def core_properties
|
|
156
|
-
unmapped_data[:core_properties] || unmapped_data.dig(:facade, :core_properties) || {}
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
# Returns application properties (e.g. app_name, doc_security), or an empty hash.
|
|
160
|
-
#
|
|
161
|
-
# @return [Hash<Symbol, untyped>]
|
|
162
|
-
# @api public
|
|
163
|
-
#: () -> Hash[Symbol, untyped]
|
|
164
|
-
def app_properties
|
|
165
|
-
unmapped_data[:app_properties] || unmapped_data.dig(:facade, :app_properties) || {}
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
# Returns custom document properties as an array of property hashes, or an empty array.
|
|
169
|
-
#
|
|
170
|
-
# @return [Array<Hash[Symbol, untyped]>]
|
|
171
|
-
# @api public
|
|
172
|
-
#: () -> Array[Hash[Symbol, untyped]]
|
|
173
|
-
def custom_properties
|
|
174
|
-
unmapped_data[:custom_properties] || unmapped_data.dig(:facade, :custom_properties) || []
|
|
216
|
+
workbook_properties.full_calc_on_load ? true : false
|
|
175
217
|
end
|
|
176
218
|
|
|
177
|
-
#
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
def workbook_protection
|
|
183
|
-
unmapped_data[:workbook_protection] || unmapped_data.dig(:facade, :workbook_protection)
|
|
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?
|
|
184
224
|
end
|
|
185
225
|
|
|
186
226
|
# Returns the defined name matching name (and optionally sheet), or nil.
|
|
@@ -190,21 +230,26 @@ module Xlsxrb
|
|
|
190
230
|
# wb.defined_name("Print_Area", sheet: "Sheet1")
|
|
191
231
|
#
|
|
192
232
|
# @param name [String] The defined name.
|
|
193
|
-
# @param sheet [String, Integer, nil] Optional sheet name or 0-based index.
|
|
194
|
-
# @return [
|
|
233
|
+
# @param sheet [String, Symbol, Integer, nil] Optional sheet name or 0-based index.
|
|
234
|
+
# @return [Elements::DefinedName, nil]
|
|
195
235
|
# @api public
|
|
196
|
-
#: (String name, ?sheet: (String | Integer)?) ->
|
|
236
|
+
#: (String name, ?sheet: (String | Symbol | Integer)?) -> Elements::DefinedName?
|
|
197
237
|
def defined_name(name, sheet: nil)
|
|
198
238
|
sheet_idx = case sheet
|
|
199
239
|
when Integer then sheet
|
|
200
|
-
when String
|
|
240
|
+
when String, Symbol
|
|
241
|
+
target = sheet.to_s
|
|
242
|
+
sheets.find_index { |s| s.name == target }
|
|
201
243
|
end
|
|
202
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
|
+
|
|
203
248
|
if sheet_idx
|
|
204
|
-
defined_names.find { |dn| dn
|
|
249
|
+
defined_names.find { |dn| dn_name.call(dn) == name && dn_local.call(dn) == sheet_idx }
|
|
205
250
|
else
|
|
206
|
-
defined_names.find { |dn| dn
|
|
207
|
-
defined_names.find { |dn| dn
|
|
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 }
|
|
208
253
|
end
|
|
209
254
|
end
|
|
210
255
|
|
|
@@ -213,17 +258,19 @@ module Xlsxrb
|
|
|
213
258
|
# @example
|
|
214
259
|
# wb.sheet(0)
|
|
215
260
|
# wb.sheet("Sales")
|
|
261
|
+
# wb.sheet(:Sales)
|
|
216
262
|
#
|
|
217
|
-
# @param identifier [Integer, String, untyped] 0-based index or sheet name.
|
|
263
|
+
# @param identifier [Integer, String, Symbol, untyped] 0-based index or sheet name.
|
|
218
264
|
# @return [Elements::Worksheet, StreamSheet, nil]
|
|
219
265
|
# @api public
|
|
220
|
-
#: (?Integer | String | untyped identifier) -> (Elements::Worksheet | StreamSheet)?
|
|
266
|
+
#: (?Integer | String | Symbol | untyped identifier) -> (Elements::Worksheet | StreamSheet)?
|
|
221
267
|
def sheet(identifier = 0)
|
|
222
268
|
case identifier
|
|
223
269
|
when Integer
|
|
224
270
|
sheets[identifier]
|
|
225
|
-
when String
|
|
226
|
-
|
|
271
|
+
when String, Symbol
|
|
272
|
+
target = identifier.to_s
|
|
273
|
+
sheets.find { |s| s.name == target }
|
|
227
274
|
end
|
|
228
275
|
end
|
|
229
276
|
alias [] sheet
|
|
@@ -266,22 +313,39 @@ module Xlsxrb
|
|
|
266
313
|
target_sheet.images_at(ref_or_row, col)
|
|
267
314
|
end
|
|
268
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
|
+
|
|
269
327
|
# Loads all sheets into memory, returning an Elements::Workbook where every
|
|
270
328
|
# worksheet is a fully-parsed Elements::Worksheet supporting coordinate random access.
|
|
271
329
|
#
|
|
272
330
|
# @example
|
|
273
|
-
# wb = Xlsxrb.
|
|
331
|
+
# wb = Xlsxrb.load("file.xlsx")
|
|
274
332
|
# puts wb["Sheet1"]["A1"].value
|
|
275
333
|
#
|
|
334
|
+
# @deprecated Use {Xlsxrb.load} instead. Will be removed in 0.2.0.
|
|
276
335
|
# @return [Elements::Workbook]
|
|
277
336
|
# @api public
|
|
278
337
|
#: () -> Elements::Workbook
|
|
279
338
|
def load
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
|
283
348
|
end
|
|
284
|
-
alias to_workbook load
|
|
285
349
|
|
|
286
350
|
# Closes any streaming resources associated with worksheets.
|
|
287
351
|
#
|
|
@@ -289,6 +353,10 @@ module Xlsxrb
|
|
|
289
353
|
# @api public
|
|
290
354
|
#: () -> void
|
|
291
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
|
|
292
360
|
sheets.each { |s| s.close if s.respond_to?(:close) }
|
|
293
361
|
nil
|
|
294
362
|
end
|
|
@@ -296,6 +364,7 @@ module Xlsxrb
|
|
|
296
364
|
# Returns a new Workbook with the specified sheet updated.
|
|
297
365
|
# Yields the matched worksheet to the block, which must return a new Worksheet.
|
|
298
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.
|
|
299
368
|
# @example
|
|
300
369
|
# new_wb = wb.update_sheet("Sheet1") do |sheet|
|
|
301
370
|
# sheet.update_cell("A1", value: "New Title")
|
|
@@ -309,6 +378,7 @@ module Xlsxrb
|
|
|
309
378
|
# @api public
|
|
310
379
|
#: (Integer | String identifier) ?{ (Elements::Worksheet) -> Elements::Worksheet } -> Elements::Workbook
|
|
311
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
|
|
312
382
|
raise ArgumentError, "block is required" unless block_given?
|
|
313
383
|
|
|
314
384
|
sheet_to_update = sheet(identifier)
|
|
@@ -337,17 +407,36 @@ module Xlsxrb
|
|
|
337
407
|
# wb.save("output.xlsx")
|
|
338
408
|
#
|
|
339
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:`).
|
|
340
413
|
# @return [void]
|
|
341
414
|
# @api public
|
|
342
|
-
#: (String | IO | StringIO filepath) -> void
|
|
343
|
-
def save(filepath)
|
|
344
|
-
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)
|
|
345
433
|
end
|
|
346
434
|
|
|
347
435
|
# Validates workbook structure according to OOXML specifications.
|
|
348
436
|
#
|
|
349
437
|
# @param sheets [Array<Elements::Worksheet>]
|
|
350
438
|
# @return [Array<String>] List of error messages.
|
|
439
|
+
# @api private
|
|
351
440
|
#: (untyped sheets) -> Array[String]
|
|
352
441
|
def self.validate(sheets)
|
|
353
442
|
errs = []
|