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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +256 -1
- data/README.md +111 -28
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +439 -139
- data/lib/xlsxrb/chart_builder.rb +540 -193
- data/lib/xlsxrb/dsl_helpers.rb +178 -73
- data/lib/xlsxrb/elements/cell.rb +197 -130
- data/lib/xlsxrb/elements/column.rb +88 -7
- data/lib/xlsxrb/elements/coordinate_access.rb +86 -59
- data/lib/xlsxrb/elements/image.rb +18 -19
- data/lib/xlsxrb/elements/metadata.rb +2379 -0
- data/lib/xlsxrb/elements/row.rb +84 -118
- data/lib/xlsxrb/elements/styles.rb +140 -90
- data/lib/xlsxrb/elements/types.rb +164 -22
- data/lib/xlsxrb/elements/workbook.rb +122 -140
- data/lib/xlsxrb/elements/worksheet.rb +451 -226
- data/lib/xlsxrb/elements.rb +1 -0
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +16 -5
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +14 -9
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +296 -34
- data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
- data/lib/xlsxrb/ooxml/reader.rb +52 -50
- data/lib/xlsxrb/ooxml/styles_parser.rb +36 -5
- data/lib/xlsxrb/ooxml/utils.rb +2 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +242 -109
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +63 -31
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +119 -56
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +33 -13
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +75 -15
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +18 -8
- data/lib/xlsxrb/ooxml/writer.rb +124 -44
- data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
- data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
- data/lib/xlsxrb/ooxml/zip_reader.rb +112 -25
- data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
- data/lib/xlsxrb/stream_row.rb +56 -81
- data/lib/xlsxrb/stream_sheet.rb +542 -273
- data/lib/xlsxrb/stream_writer.rb +1536 -870
- data/lib/xlsxrb/style_builder.rb +667 -221
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +406 -143
- data/lib/xlsxrb/worksheet_builder.rb +1009 -900
- data/lib/xlsxrb.rb +1179 -1056
- data/sig/generated/xlsxrb/chart_builder.rbs +385 -159
- data/sig/generated/xlsxrb/dsl_helpers.rbs +81 -15
- data/sig/generated/xlsxrb/elements/cell.rbs +78 -70
- data/sig/generated/xlsxrb/elements/column.rbs +55 -3
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +35 -29
- data/sig/generated/xlsxrb/elements/image.rbs +7 -11
- data/sig/generated/xlsxrb/elements/metadata.rbs +1574 -0
- data/sig/generated/xlsxrb/elements/row.rbs +39 -42
- data/sig/generated/xlsxrb/elements/styles.rbs +66 -66
- data/sig/generated/xlsxrb/elements/types.rbs +27 -0
- data/sig/generated/xlsxrb/elements/workbook.rbs +46 -99
- data/sig/generated/xlsxrb/elements/worksheet.rbs +121 -96
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -4
- data/sig/generated/xlsxrb/ooxml/reader.rbs +2 -2
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +6 -4
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +22 -13
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
- data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +24 -9
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
- data/sig/generated/xlsxrb/stream_row.rbs +32 -33
- data/sig/generated/xlsxrb/stream_sheet.rbs +233 -181
- data/sig/generated/xlsxrb/stream_writer.rbs +961 -667
- data/sig/generated/xlsxrb/style_builder.rbs +402 -128
- data/sig/generated/xlsxrb/workbook_builder.rbs +223 -71
- data/sig/generated/xlsxrb/worksheet_builder.rbs +579 -453
- data/sig/generated/xlsxrb.rbs +205 -144
- data/sig/rexml.rbs +8 -0
- metadata +5 -5
- data/lib/xlsxrb/utils.rb +0 -291
- data/sig/generated/xlsxrb/utils.rbs +0 -184
|
@@ -13,11 +13,14 @@ module Xlsxrb
|
|
|
13
13
|
# sheet.row(["Product", "Revenue"])
|
|
14
14
|
# sheet.row(["Widget", 1000])
|
|
15
15
|
# end
|
|
16
|
-
# builder.
|
|
16
|
+
# builder.core_properties(creator: "Reporting System")
|
|
17
17
|
# end
|
|
18
18
|
#
|
|
19
19
|
# @api public
|
|
20
20
|
class WorkbookBuilder
|
|
21
|
+
OMITTED = Object.new.freeze
|
|
22
|
+
private_constant :OMITTED
|
|
23
|
+
|
|
21
24
|
# @param strict_excel_mode [Boolean] Whether to enforce Microsoft Excel specifications.
|
|
22
25
|
# @param default_font [Hash, nil] Optional default font options (e.g. `{ name: "Arial", size: 12 }`).
|
|
23
26
|
#: (?strict_excel_mode: bool, ?default_font: untyped?) -> void
|
|
@@ -27,6 +30,7 @@ module Xlsxrb
|
|
|
27
30
|
@sheets = []
|
|
28
31
|
@sheet_builders = [] # Keep track of sheet builders for style processing
|
|
29
32
|
@defined_names = []
|
|
33
|
+
@styles = {}
|
|
30
34
|
@core_properties = {}
|
|
31
35
|
@app_properties = {}
|
|
32
36
|
@custom_properties = []
|
|
@@ -56,98 +60,178 @@ module Xlsxrb
|
|
|
56
60
|
existing[:size] = size if size
|
|
57
61
|
@default_font = existing.merge(opts)
|
|
58
62
|
end
|
|
59
|
-
|
|
63
|
+
|
|
64
|
+
# @return [Hash{String => StyleBuilder}]
|
|
65
|
+
#: Hash[String, StyleBuilder]
|
|
66
|
+
attr_reader :styles
|
|
67
|
+
|
|
68
|
+
# Defines or configures a named cell style at the workbook level.
|
|
69
|
+
#
|
|
70
|
+
# @example
|
|
71
|
+
# workbook.style(:header, bold: true, fill_color: "4F81BD", font_color: "FFFFFF")
|
|
72
|
+
#
|
|
73
|
+
# @param name [String, Symbol] The name of the style.
|
|
74
|
+
# @param opts [Hash] Style options (e.g. bold: true, fill_color: "FF0000").
|
|
75
|
+
# @yield [style_builder]
|
|
76
|
+
# @yieldparam style_builder [Xlsxrb::StyleBuilder]
|
|
77
|
+
# @return [StyleBuilder]
|
|
78
|
+
# @api public
|
|
79
|
+
#: (String | Symbol name, **untyped opts) ?{ (StyleBuilder) -> void } -> StyleBuilder
|
|
80
|
+
def style(name, **opts)
|
|
81
|
+
style_name = name.to_s
|
|
82
|
+
style_builder = StyleBuilder.new(style_name)
|
|
83
|
+
style_builder.apply_options!(**opts) unless opts.empty?
|
|
84
|
+
yield style_builder if block_given?
|
|
85
|
+
@styles[style_name] = style_builder
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Creates an {Elements::Formula} object.
|
|
89
|
+
#
|
|
90
|
+
# @param expression [String] The formula expression without leading '='.
|
|
91
|
+
# @param cached_value [Object, nil] Optional precomputed value.
|
|
92
|
+
# @param type [Symbol, String, nil] Formula type (:shared, :array, etc.).
|
|
93
|
+
# @param ref [String, nil] Target cell or range reference.
|
|
94
|
+
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
95
|
+
# @return [Elements::Formula]
|
|
96
|
+
# @api public
|
|
97
|
+
#: (String expression, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), ?type: (Symbol | String)?, ?ref: String?, ?shared_index: Integer?) -> Elements::Formula
|
|
98
|
+
def formula(...)
|
|
99
|
+
Xlsxrb.formula(...)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Creates an array or dynamic array {Elements::Formula} object.
|
|
103
|
+
#
|
|
104
|
+
# @param expression [String] The formula expression without leading '='.
|
|
105
|
+
# @param ref [String, nil] Target cell or range reference (keyword).
|
|
106
|
+
# @param cached_value [Object, nil] Optional precomputed value.
|
|
107
|
+
# @return [Elements::Formula]
|
|
108
|
+
# @api public
|
|
109
|
+
#: (String expression, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil), **untyped attrs) -> Elements::Formula
|
|
110
|
+
def array_formula(...)
|
|
111
|
+
Xlsxrb.array_formula(...)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Creates a shared formula {Elements::Formula} object.
|
|
115
|
+
#
|
|
116
|
+
# @param expression [String, nil] The formula expression.
|
|
117
|
+
# @param shared_index [Integer, nil] Shared formula group index (0-based).
|
|
118
|
+
# @param ref [String, nil] Target range for master cell.
|
|
119
|
+
# @param cached_value [Object, nil] Optional precomputed value.
|
|
120
|
+
# @return [Elements::Formula]
|
|
121
|
+
# @api public
|
|
122
|
+
#: (?String? expression, ?shared_index: Integer?, ?ref: String?, ?cached_value: (String | Numeric | bool | Elements::CellError | nil)) -> Elements::Formula
|
|
123
|
+
def shared_formula(...)
|
|
124
|
+
Xlsxrb.shared_formula(...)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Creates an {Elements::RichText} object.
|
|
128
|
+
#
|
|
129
|
+
# @param runs [Array] Text runs or styling options.
|
|
130
|
+
# @return [Elements::RichText]
|
|
131
|
+
# @api public
|
|
132
|
+
#: (*(Hash[Symbol, untyped] | Elements::RichTextRun | String | Array[untyped] | untyped) runs, ?text: String?, ?base_font: untyped, **untyped font_props) -> Elements::RichText
|
|
133
|
+
def rich_text(...)
|
|
134
|
+
Xlsxrb.rich_text(...)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Creates or returns an {Elements::CellError} object.
|
|
138
|
+
#
|
|
139
|
+
# @param code [String, Symbol, Elements::CellError] Error code or symbol.
|
|
140
|
+
# @return [Elements::CellError]
|
|
141
|
+
# @api public
|
|
142
|
+
#: (String | Symbol | Elements::CellError code) -> Elements::CellError
|
|
143
|
+
def cell_error(...)
|
|
144
|
+
Xlsxrb.cell_error(...)
|
|
145
|
+
end
|
|
60
146
|
|
|
61
147
|
# Gets or sets the active worksheet tab in the workbook window.
|
|
62
148
|
#
|
|
63
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
64
149
|
# @return [String, Symbol, Integer, nil]
|
|
65
150
|
# @api public
|
|
66
|
-
#: (
|
|
67
|
-
|
|
68
|
-
if name_or_index
|
|
69
|
-
@active_sheet = name_or_index
|
|
70
|
-
else
|
|
71
|
-
@active_sheet
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
alias active_sheet= active_sheet
|
|
75
|
-
alias active_tab active_sheet
|
|
76
|
-
alias active_tab= active_sheet
|
|
151
|
+
#: (String | Symbol | Integer | nil)
|
|
152
|
+
attr_accessor :active_sheet
|
|
77
153
|
|
|
78
154
|
# Gets or sets the first sheet tab visible in the sheet tab scroll area.
|
|
79
155
|
#
|
|
80
|
-
# @param name_or_index [String, Symbol, Integer, nil] Sheet name or 0-based sheet index.
|
|
81
156
|
# @return [String, Symbol, Integer, nil]
|
|
82
157
|
# @api public
|
|
83
|
-
#: (
|
|
84
|
-
|
|
85
|
-
if name_or_index
|
|
86
|
-
@first_sheet = name_or_index
|
|
87
|
-
else
|
|
88
|
-
@first_sheet
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
alias first_sheet= first_sheet
|
|
158
|
+
#: (String | Symbol | Integer | nil)
|
|
159
|
+
attr_accessor :first_sheet
|
|
92
160
|
|
|
93
|
-
#
|
|
161
|
+
# Sets whether sheet tabs are visible in the workbook window.
|
|
94
162
|
#
|
|
95
163
|
# @param visible [Boolean, nil]
|
|
96
164
|
# @return [Boolean, nil]
|
|
97
165
|
# @api public
|
|
98
|
-
#: (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
166
|
+
#: (bool? visible) -> bool?
|
|
167
|
+
attr_writer :show_sheet_tabs
|
|
168
|
+
|
|
169
|
+
# Returns whether sheet tabs are visible in the workbook window.
|
|
170
|
+
#
|
|
171
|
+
# @return [Boolean]
|
|
172
|
+
# @api public
|
|
173
|
+
#: () -> bool
|
|
174
|
+
def show_sheet_tabs?
|
|
175
|
+
@show_sheet_tabs != false
|
|
105
176
|
end
|
|
106
|
-
alias show_sheet_tabs= show_sheet_tabs
|
|
107
177
|
|
|
108
178
|
# Gets or sets the ratio between the sheet tabs bar and the horizontal scroll bar (0-1000).
|
|
109
179
|
#
|
|
110
|
-
# @param ratio [Integer, nil]
|
|
111
180
|
# @return [Integer, nil]
|
|
112
181
|
# @api public
|
|
113
|
-
#:
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
182
|
+
#: Integer?
|
|
183
|
+
attr_accessor :tab_ratio
|
|
184
|
+
|
|
185
|
+
# Gets the internal code name of the workbook.
|
|
186
|
+
#
|
|
187
|
+
# @return [String, nil]
|
|
188
|
+
# @api public
|
|
189
|
+
#: () -> String?
|
|
190
|
+
def code_name
|
|
191
|
+
@workbook_properties[:code_name]
|
|
120
192
|
end
|
|
121
|
-
alias tab_ratio= tab_ratio
|
|
122
193
|
|
|
123
|
-
#
|
|
194
|
+
# Sets the internal code name of the workbook.
|
|
124
195
|
#
|
|
125
196
|
# @param name [String, Symbol, nil] The code name.
|
|
126
197
|
# @return [String, nil]
|
|
127
198
|
# @api public
|
|
128
|
-
#: (
|
|
129
|
-
def code_name(name
|
|
130
|
-
|
|
131
|
-
@workbook_properties[:code_name] = name.to_s
|
|
132
|
-
else
|
|
133
|
-
@workbook_properties[:code_name]
|
|
134
|
-
end
|
|
199
|
+
#: (String | Symbol | nil name) -> String?
|
|
200
|
+
def code_name=(name)
|
|
201
|
+
@workbook_properties[:code_name] = name&.to_s
|
|
135
202
|
end
|
|
136
|
-
alias code_name= code_name
|
|
137
203
|
|
|
138
|
-
#
|
|
204
|
+
# Configures or queries workbook-level properties.
|
|
205
|
+
#
|
|
206
|
+
# When called with no arguments, returns the {Elements::WorkbookProperties} value object.
|
|
207
|
+
# When called with keyword options, configures properties and returns nil.
|
|
139
208
|
#
|
|
140
209
|
# @note **SECURITY WARNING:** If you set `:update_links` to anything other than `"never"`,
|
|
141
210
|
# you may expose end-users to malicious external reference vulnerabilities (e.g., CSV/DDE Injection)
|
|
142
211
|
# when they open the generated Excel file. Ensure you fully trust the exported data.
|
|
143
212
|
#
|
|
144
|
-
# @param
|
|
145
|
-
# @
|
|
213
|
+
# @param opts [Hash] Workbook properties map.
|
|
214
|
+
# @return [Elements::WorkbookProperties, void]
|
|
215
|
+
# @api public
|
|
216
|
+
#: () -> Elements::WorkbookProperties
|
|
217
|
+
#: (**untyped opts) -> void
|
|
218
|
+
def workbook_properties(**opts)
|
|
219
|
+
return Elements::WorkbookProperties.new(**@workbook_properties) if opts.empty?
|
|
220
|
+
|
|
221
|
+
@workbook_properties.merge!(opts)
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Sets workbook-level properties.
|
|
226
|
+
#
|
|
227
|
+
# @param props [Hash, Elements::MetadataRecord]
|
|
146
228
|
# @return [void]
|
|
147
229
|
# @api public
|
|
148
|
-
#: (
|
|
149
|
-
def
|
|
150
|
-
|
|
230
|
+
#: (untyped props) -> void
|
|
231
|
+
def workbook_properties=(props)
|
|
232
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
233
|
+
@workbook_properties = h.transform_keys(&:to_sym)
|
|
234
|
+
nil
|
|
151
235
|
end
|
|
152
236
|
|
|
153
237
|
# Returns the list of worksheet names added to this workbook so far.
|
|
@@ -162,18 +246,21 @@ module Xlsxrb
|
|
|
162
246
|
# Adds a new worksheet to the workbook.
|
|
163
247
|
#
|
|
164
248
|
# @param name [String, nil] Sheet name (max 31 chars, no forbidden chars `[ ] * ? / \ :`).
|
|
165
|
-
# @param
|
|
249
|
+
# @param state [Symbol, String, nil] Sheet visibility state (:visible, :hidden, :very_hidden).
|
|
250
|
+
# @param tab_color [String, Hash, Symbol, nil] Tab color hex string (e.g. "FF0000") or hash options.
|
|
251
|
+
# @param code_name [String, Symbol, nil] Internal VBA code name of the sheet.
|
|
252
|
+
# @param opts [Hash] Additional sheet properties.
|
|
166
253
|
# @yield [sheet_builder]
|
|
167
254
|
# @yieldparam sheet_builder [Xlsxrb::WorksheetBuilder]
|
|
168
255
|
# @return [Elements::Worksheet]
|
|
169
256
|
# @raise [ArgumentError] If sheet name violates Excel limits in strict mode.
|
|
170
257
|
# @api public
|
|
171
|
-
#: (?String? name, **untyped opts) ?{ (WorksheetBuilder) -> void } ->
|
|
172
|
-
def sheet(name = nil, **opts)
|
|
258
|
+
#: (?String? name, ?state: Symbol | String | nil, ?tab_color: String | Hash[Symbol, untyped] | Symbol | nil, ?code_name: String | Symbol | nil, **untyped opts) ?{ (WorksheetBuilder) -> void } -> Elements::Worksheet
|
|
259
|
+
def sheet(name = nil, state: nil, tab_color: nil, code_name: nil, **opts)
|
|
173
260
|
sheet_count = [@sheets.size, @sheet_builders.size].max
|
|
174
261
|
name ||= "Sheet#{sheet_count + 1}"
|
|
175
262
|
raise ArgumentError, "Sheet name '#{name}' must be <= 31 characters (Excel limitation)" if @strict_excel_mode && name.length > 31
|
|
176
|
-
raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet
|
|
263
|
+
raise ArgumentError, "Sheet name '#{name}' contains invalid characters (Excel limitation)" if name.match?(Elements::Worksheet.forbidden_name_chars)
|
|
177
264
|
raise ArgumentError, "Sheet name '#{name}' cannot start or end with an apostrophe (Excel limitation)" if @strict_excel_mode && (name.start_with?("'") || name.end_with?("'"))
|
|
178
265
|
raise ArgumentError, "Sheet name '#{name}' is reserved by Excel (Excel limitation)" if @strict_excel_mode && name.casecmp?("History")
|
|
179
266
|
|
|
@@ -181,9 +268,10 @@ module Xlsxrb
|
|
|
181
268
|
raise ArgumentError, "Sheet name '#{name}' is already used. Excel requires unique sheet names." if @strict_excel_mode && existing_names.include?(name.downcase)
|
|
182
269
|
|
|
183
270
|
active = opts.delete(:active)
|
|
184
|
-
state = opts.delete(:state)
|
|
271
|
+
state = opts.delete(:state) if opts.key?(:state)
|
|
185
272
|
hidden = opts.delete(:hidden)
|
|
186
|
-
code_name = opts.delete(:code_name)
|
|
273
|
+
code_name = opts.delete(:code_name) if opts.key?(:code_name)
|
|
274
|
+
tab_color = opts.delete(:tab_color) if opts.key?(:tab_color)
|
|
187
275
|
state ||= :hidden if hidden
|
|
188
276
|
state ||= :visible
|
|
189
277
|
state = state.to_sym
|
|
@@ -192,79 +280,141 @@ module Xlsxrb
|
|
|
192
280
|
@active_sheet = name if active
|
|
193
281
|
|
|
194
282
|
sheet_builder = WorksheetBuilder.new(name, strict_excel_mode: @strict_excel_mode, state: state)
|
|
195
|
-
sheet_builder.code_name
|
|
196
|
-
|
|
283
|
+
sheet_builder.code_name = code_name if code_name
|
|
284
|
+
sheet_builder.tab_color = tab_color if tab_color
|
|
285
|
+
sheet_builder.sheet_properties(**opts) unless opts.empty?
|
|
197
286
|
@sheet_builders << sheet_builder
|
|
198
287
|
yield sheet_builder if block_given?
|
|
199
288
|
@defined_names.concat(sheet_builder.defined_names)
|
|
200
289
|
sheet_builder.defined_names.clear
|
|
201
|
-
|
|
290
|
+
built = sheet_builder.build
|
|
291
|
+
@sheets << built
|
|
292
|
+
built
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Accesses or defines a worksheet by name.
|
|
296
|
+
#
|
|
297
|
+
# @param name_or_index [String, Symbol, Integer, nil]
|
|
298
|
+
# @yield [sheet_builder]
|
|
299
|
+
# @return [Elements::Worksheet]
|
|
300
|
+
# @api public
|
|
301
|
+
#: (?String | Symbol | Integer | nil name_or_index) ?{ (WorksheetBuilder) -> void } -> Elements::Worksheet
|
|
302
|
+
def [](name_or_index = nil, &)
|
|
303
|
+
sheet(name_or_index&.to_s, &)
|
|
202
304
|
end
|
|
203
|
-
alias [] sheet
|
|
204
305
|
|
|
205
306
|
# --- Workbook-Level Methods ---
|
|
206
307
|
|
|
207
|
-
# Adds a defined name (named range or formula constant) to the workbook.
|
|
208
|
-
#
|
|
209
|
-
# @param name [String] Defined name (e.g. "TaxRate").
|
|
210
|
-
# @param value [String] Formula expression or range reference (e.g. "Sheet1!$B$2").
|
|
211
308
|
# Adds a defined name (named range or formula) to the workbook.
|
|
212
309
|
# Bare cell references and coordinate keywords are automatically formatted with sheet quotes and absolute $ references.
|
|
213
310
|
#
|
|
214
311
|
# @param name [String] Defined name identifier.
|
|
215
312
|
# @param value [Object, nil] Value or formula expression.
|
|
216
|
-
# @param sheet [String, nil]
|
|
313
|
+
# @param sheet [String, nil] Reference sheet name for coordinates/formula.
|
|
314
|
+
# @param scope [String, Symbol, nil] Optional sheet name or :workbook for scope (defaults to workbook).
|
|
217
315
|
# @param hidden [Boolean] Whether the defined name is hidden from Excel's UI.
|
|
218
|
-
# @param
|
|
219
|
-
# @param rows [Integer, Range, nil] Row or row range (
|
|
316
|
+
# @param columns [Integer, String, nil] Column index or letter for coordinate-based reference.
|
|
317
|
+
# @param rows [Integer, Range, nil] Row or row range (0-based) for coordinate-based reference.
|
|
220
318
|
# @param range [String, nil] Cell range reference.
|
|
221
319
|
# @return [void]
|
|
222
320
|
# @api public
|
|
223
|
-
#: (String name, ?untyped value, ?sheet: String?, ?hidden: bool, ?
|
|
224
|
-
def defined_name(name, value = nil, sheet: nil, hidden: false,
|
|
225
|
-
|
|
226
|
-
normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: target_ref_sheet, col: col, rows: rows, range: range)
|
|
321
|
+
#: (String name, ?untyped value, ?sheet: String?, ?scope: (String | Symbol)?, ?hidden: bool, ?columns: untyped, ?rows: untyped, ?range: untyped) -> void
|
|
322
|
+
def defined_name(name, value = nil, sheet: nil, scope: nil, hidden: false, columns: nil, rows: nil, range: nil)
|
|
323
|
+
normalized_val = DslHelpers.normalize_defined_name_value(value, sheet: sheet, columns: columns, rows: rows, range: range)
|
|
227
324
|
entry = { name: name, value: normalized_val, hidden: hidden }
|
|
228
|
-
local_sheet =
|
|
325
|
+
local_sheet = if scope
|
|
326
|
+
scope == :workbook ? nil : scope.to_s
|
|
327
|
+
end
|
|
229
328
|
entry[:local_sheet_name] = local_sheet if local_sheet
|
|
230
329
|
@defined_names << entry
|
|
231
330
|
end
|
|
232
331
|
|
|
233
|
-
#
|
|
332
|
+
# Returns workbook-level defined names.
|
|
333
|
+
#
|
|
334
|
+
# @return [Array<Elements::DefinedName>]
|
|
335
|
+
# @api public
|
|
336
|
+
#: () -> Array[Elements::DefinedName]
|
|
337
|
+
def defined_names
|
|
338
|
+
@defined_names.map { |dn| dn.is_a?(Elements::DefinedName) ? dn : Elements::DefinedName.new(**dn) }
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# Returns the print area range for the sheet, or nil if none.
|
|
234
342
|
#
|
|
235
|
-
# @param range [String] Cell range (e.g. "A1:G50").
|
|
236
343
|
# @param sheet [String, nil] Target sheet name (defaults to latest or "Sheet1").
|
|
237
|
-
# @return [
|
|
344
|
+
# @return [String, nil]
|
|
238
345
|
# @api public
|
|
239
|
-
#: (
|
|
240
|
-
def print_area(
|
|
346
|
+
#: (?sheet: String?) -> String?
|
|
347
|
+
def print_area(sheet: nil)
|
|
241
348
|
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
defined_name("_xlnm.Print_Area", value, sheet: sheet_name)
|
|
349
|
+
dn = @defined_names.find { |d| d[:name] == "_xlnm.Print_Area" && d[:local_sheet_name] == sheet_name }
|
|
350
|
+
dn ? dn[:value].split("!", 2).last.tr("$", "") : nil
|
|
245
351
|
end
|
|
246
352
|
|
|
247
|
-
# Sets
|
|
353
|
+
# Sets the print area for the current worksheet.
|
|
354
|
+
#
|
|
355
|
+
# @param range [String, nil] Cell range (e.g. "A1:G50").
|
|
356
|
+
# @return [String, nil]
|
|
357
|
+
# @api public
|
|
358
|
+
#: (String? range) -> String?
|
|
359
|
+
def print_area=(range)
|
|
360
|
+
sheet_name = @sheets.last&.name || "Sheet1"
|
|
361
|
+
set_print_area(range, sheet: sheet_name)
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Sets or gets repeating print titles (rows and/or columns) for pagination.
|
|
365
|
+
#
|
|
366
|
+
# When called with no row/column arguments, returns the {Elements::PrintTitles} value object or nil.
|
|
367
|
+
# When called with row/column arguments, sets repeating print titles and returns nil.
|
|
248
368
|
#
|
|
249
369
|
# @param rows [String, nil] Repeating row range (e.g. "1:2").
|
|
250
|
-
# @param
|
|
370
|
+
# @param columns [String, nil] Repeating column range (e.g. "A:B").
|
|
251
371
|
# @param sheet [String, nil] Target sheet name.
|
|
252
|
-
# @return [void]
|
|
372
|
+
# @return [Elements::PrintTitles, nil, void]
|
|
253
373
|
# @api public
|
|
254
|
-
#: (
|
|
255
|
-
|
|
374
|
+
#: () -> Elements::PrintTitles?
|
|
375
|
+
#: (?rows: (String | Integer | Range[Integer])?, ?columns: (String | Integer | Symbol | Range[untyped])?, ?sheet: String?) -> void
|
|
376
|
+
def print_titles(rows: nil, columns: nil, sheet: nil)
|
|
256
377
|
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
378
|
+
if rows.nil? && columns.nil?
|
|
379
|
+
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
380
|
+
return nil unless existing
|
|
381
|
+
|
|
382
|
+
c = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
383
|
+
r = Regexp.last_match(1).delete("$") if existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
|
|
384
|
+
return Elements::PrintTitles.new(rows: r, columns: c)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
c = DslHelpers.normalize_print_titles_columns(columns)
|
|
388
|
+
r = DslHelpers.normalize_print_titles_rows(rows)
|
|
257
389
|
existing = @defined_names.find { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
258
|
-
if existing && (
|
|
259
|
-
|
|
260
|
-
|
|
390
|
+
if existing && (r.nil? || c.nil?)
|
|
391
|
+
c = Regexp.last_match(1).delete("$") if c.nil? && existing[:value] =~ /'[^']*'!\$([A-Za-z]+:\$[A-Za-z]+)/
|
|
392
|
+
r = Regexp.last_match(1).delete("$") if r.nil? && existing[:value] =~ /'[^']*'!\$([0-9]+:\$[0-9]+)/
|
|
261
393
|
end
|
|
262
394
|
parts = []
|
|
263
|
-
parts << "'#{sheet_name}'!$#{
|
|
264
|
-
parts << "'#{sheet_name}'!$#{
|
|
395
|
+
parts << "'#{sheet_name}'!$#{c.sub(":", ":$")}" if c
|
|
396
|
+
parts << "'#{sheet_name}'!$#{r.sub(":", ":$")}" if r
|
|
265
397
|
value = parts.join(",")
|
|
266
398
|
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
267
|
-
defined_name("_xlnm.Print_Titles", value, sheet: sheet_name)
|
|
399
|
+
defined_name("_xlnm.Print_Titles", value, sheet: sheet_name, scope: sheet_name)
|
|
400
|
+
nil
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Sets repeating print titles.
|
|
404
|
+
#
|
|
405
|
+
# @param opts [Elements::PrintTitles, Hash, nil] Options with :rows and/or :columns.
|
|
406
|
+
# @return [Elements::PrintTitles, Hash, nil]
|
|
407
|
+
# @api public
|
|
408
|
+
#: (Elements::PrintTitles | Hash[Symbol, untyped] | nil opts) -> (Elements::PrintTitles | Hash[Symbol, untyped] | nil)
|
|
409
|
+
def print_titles=(opts)
|
|
410
|
+
if opts.nil?
|
|
411
|
+
sheet_name = @sheets.last&.name || "Sheet1"
|
|
412
|
+
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sheet_name }
|
|
413
|
+
else
|
|
414
|
+
h = opts.respond_to?(:to_h) ? opts.to_h : opts
|
|
415
|
+
print_titles(**h)
|
|
416
|
+
end
|
|
417
|
+
opts
|
|
268
418
|
end
|
|
269
419
|
|
|
270
420
|
# Sets repeating title rows for pagination on the target or active sheet.
|
|
@@ -303,9 +453,9 @@ module Xlsxrb
|
|
|
303
453
|
cols_str = if first.is_a?(String)
|
|
304
454
|
first.include?(":") ? first : "#{first}:#{last || first}"
|
|
305
455
|
else
|
|
306
|
-
first_name =
|
|
456
|
+
first_name = Elements::Cell._column_letter(first)
|
|
307
457
|
last_name = if last.is_a?(Integer)
|
|
308
|
-
|
|
458
|
+
Elements::Cell._column_letter(last)
|
|
309
459
|
elsif last
|
|
310
460
|
last.to_s
|
|
311
461
|
else
|
|
@@ -313,39 +463,94 @@ module Xlsxrb
|
|
|
313
463
|
end
|
|
314
464
|
"#{first_name}:#{last_name}"
|
|
315
465
|
end
|
|
316
|
-
print_titles(
|
|
466
|
+
print_titles(columns: cols_str, sheet: sheet)
|
|
317
467
|
end
|
|
318
468
|
|
|
319
|
-
# Sets workbook structure and window protection.
|
|
469
|
+
# Sets or returns workbook structure and window protection.
|
|
320
470
|
#
|
|
321
471
|
# @param opts [Hash] Protection options (e.g. lock_structure: true, password: "secret").
|
|
472
|
+
# @return [Elements::WorkbookProtection, nil, void]
|
|
473
|
+
# @api public
|
|
474
|
+
#: (**String | Integer | bool | nil opts) -> (Elements::WorkbookProtection | nil)
|
|
475
|
+
def workbook_protection(**opts)
|
|
476
|
+
if opts.empty?
|
|
477
|
+
@workbook_protection ? Elements::WorkbookProtection.new(**@workbook_protection) : nil
|
|
478
|
+
else
|
|
479
|
+
@workbook_protection = DslHelpers.normalize_workbook_protection(opts)
|
|
480
|
+
nil
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Sets workbook protection settings.
|
|
485
|
+
#
|
|
486
|
+
# @param opts [Hash, Elements::MetadataRecord, nil]
|
|
322
487
|
# @return [void]
|
|
323
488
|
# @api public
|
|
324
|
-
#: (
|
|
325
|
-
def
|
|
326
|
-
@workbook_protection = opts
|
|
489
|
+
#: (untyped opts) -> void
|
|
490
|
+
def workbook_protection=(opts)
|
|
491
|
+
@workbook_protection = opts.is_a?(Elements::MetadataRecord) ? opts.to_h : (opts && DslHelpers.normalize_workbook_protection(opts))
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Configures or queries Dublin Core document metadata properties.
|
|
495
|
+
#
|
|
496
|
+
# When called with no arguments, returns the {Elements::CoreProperties} value object.
|
|
497
|
+
# When called with keyword options, configures properties and returns nil.
|
|
498
|
+
#
|
|
499
|
+
# @param opts [Hash] Core properties map (:creator, :title, :subject, :description, etc.).
|
|
500
|
+
# @return [Elements::CoreProperties, void]
|
|
501
|
+
# @api public
|
|
502
|
+
#: () -> Elements::CoreProperties
|
|
503
|
+
#: (**untyped opts) -> void
|
|
504
|
+
def core_properties(**opts)
|
|
505
|
+
return Elements::CoreProperties.new(**@core_properties) if opts.empty?
|
|
506
|
+
|
|
507
|
+
@core_properties.merge!(opts)
|
|
508
|
+
nil
|
|
327
509
|
end
|
|
328
510
|
|
|
329
|
-
#
|
|
511
|
+
# Replaces core document properties.
|
|
330
512
|
#
|
|
331
|
-
# @param
|
|
332
|
-
# @param value [String, Integer, Time] Property value.
|
|
513
|
+
# @param props [Hash, Elements::CoreProperties]
|
|
333
514
|
# @return [void]
|
|
334
515
|
# @api public
|
|
335
|
-
#: (Symbol
|
|
336
|
-
def
|
|
337
|
-
|
|
516
|
+
#: (Hash[Symbol, untyped] | Elements::CoreProperties props) -> void
|
|
517
|
+
def core_properties=(props)
|
|
518
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
519
|
+
@core_properties = h.transform_keys(&:to_sym)
|
|
520
|
+
nil
|
|
338
521
|
end
|
|
339
522
|
|
|
340
|
-
#
|
|
523
|
+
# Configures or queries extended application metadata properties.
|
|
524
|
+
#
|
|
525
|
+
# When called with no arguments, returns the {Elements::AppProperties} value object.
|
|
526
|
+
# When called with keyword options, configures properties and returns nil.
|
|
341
527
|
#
|
|
342
|
-
# @param
|
|
343
|
-
# @
|
|
528
|
+
# @param opts [Hash] Application properties map (:company, :manager, :app_version, etc.).
|
|
529
|
+
# @return [Elements::AppProperties, void]
|
|
530
|
+
# @api public
|
|
531
|
+
#: () -> Elements::AppProperties
|
|
532
|
+
#: (**untyped opts) -> void
|
|
533
|
+
def app_properties(**opts)
|
|
534
|
+
return Elements::AppProperties.new(**@app_properties) if opts.empty?
|
|
535
|
+
|
|
536
|
+
opts.each do |k, v|
|
|
537
|
+
norm_name = k.to_sym == :application ? :app_name : k.to_sym
|
|
538
|
+
@app_properties[norm_name] = v
|
|
539
|
+
end
|
|
540
|
+
nil
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Replaces application document properties.
|
|
544
|
+
#
|
|
545
|
+
# @param props [Hash, Elements::AppProperties]
|
|
344
546
|
# @return [void]
|
|
345
547
|
# @api public
|
|
346
|
-
#: (Symbol
|
|
347
|
-
def
|
|
348
|
-
|
|
548
|
+
#: (Hash[Symbol, untyped] | Elements::AppProperties props) -> void
|
|
549
|
+
def app_properties=(props)
|
|
550
|
+
h = props.respond_to?(:to_h) ? props.to_h : (props || {})
|
|
551
|
+
@app_properties = {}
|
|
552
|
+
app_properties(**h)
|
|
553
|
+
nil
|
|
349
554
|
end
|
|
350
555
|
|
|
351
556
|
# Sets multiple core, app, and/or custom properties simultaneously.
|
|
@@ -357,8 +562,8 @@ module Xlsxrb
|
|
|
357
562
|
# @api public
|
|
358
563
|
#: (?core: Hash[Symbol, String | Integer | Time]?, ?app: Hash[Symbol, String | Integer | Time]?, ?custom: Hash[String | Symbol, untyped]?) -> void
|
|
359
564
|
def properties(core: nil, app: nil, custom: nil)
|
|
360
|
-
core
|
|
361
|
-
app
|
|
565
|
+
core_properties(**core) if core
|
|
566
|
+
app_properties(**app) if app
|
|
362
567
|
custom&.each { |k, v| custom_property(k.to_s, v) }
|
|
363
568
|
end
|
|
364
569
|
|
|
@@ -374,6 +579,18 @@ module Xlsxrb
|
|
|
374
579
|
@custom_properties << { name: name, value: value, type: type }
|
|
375
580
|
end
|
|
376
581
|
|
|
582
|
+
# Returns custom document properties.
|
|
583
|
+
#
|
|
584
|
+
# @return [Hash<String, Elements::CustomProperty>]
|
|
585
|
+
# @api public
|
|
586
|
+
#: () -> Hash[String, Elements::CustomProperty]
|
|
587
|
+
def custom_properties
|
|
588
|
+
@custom_properties.to_h do |cp|
|
|
589
|
+
prop = cp.is_a?(Elements::CustomProperty) ? cp : Elements::CustomProperty.new(name: (cp[:name] || cp["name"]).to_s, value: cp[:value] || cp["value"], type: cp[:type] || cp["type"] || :string)
|
|
590
|
+
[prop.name.to_s, prop]
|
|
591
|
+
end.freeze
|
|
592
|
+
end
|
|
593
|
+
|
|
377
594
|
# Builds and returns the compiled {Elements::Workbook}.
|
|
378
595
|
#
|
|
379
596
|
# @return [Elements::Workbook]
|
|
@@ -391,8 +608,8 @@ module Xlsxrb
|
|
|
391
608
|
|
|
392
609
|
# Auto-register print_area and print_titles defined names from sheet builders if not explicitly defined
|
|
393
610
|
@sheet_builders.each do |sb|
|
|
394
|
-
|
|
395
|
-
print_titles(rows: sb.print_titles
|
|
611
|
+
set_print_area(sb.print_area, sheet: sb.name) if sb.print_area && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sb.name }
|
|
612
|
+
print_titles(rows: sb.print_titles.rows, columns: sb.print_titles.columns, sheet: sb.name) if sb.print_titles && @defined_names.none? { |dn| dn[:name] == "_xlnm.Print_Titles" && dn[:local_sheet_name] == sb.name }
|
|
396
613
|
sb.defined_names&.each do |dn|
|
|
397
614
|
@defined_names << dn
|
|
398
615
|
end
|
|
@@ -427,7 +644,8 @@ module Xlsxrb
|
|
|
427
644
|
idx || @first_sheet
|
|
428
645
|
end
|
|
429
646
|
|
|
430
|
-
@workbook_properties[:
|
|
647
|
+
@workbook_properties[:active_sheet] = active_tab_idx unless active_tab_idx.nil?
|
|
648
|
+
@workbook_properties[:active_sheet_index] = active_tab_idx unless active_tab_idx.nil?
|
|
431
649
|
@workbook_properties[:first_sheet] = first_sheet_idx unless first_sheet_idx.nil?
|
|
432
650
|
@workbook_properties[:show_sheet_tabs] = @show_sheet_tabs unless @show_sheet_tabs.nil?
|
|
433
651
|
@workbook_properties[:tab_ratio] = @tab_ratio unless @tab_ratio.nil?
|
|
@@ -452,6 +670,17 @@ module Xlsxrb
|
|
|
452
670
|
|
|
453
671
|
private
|
|
454
672
|
|
|
673
|
+
#: (String? range, ?sheet: String?) -> String?
|
|
674
|
+
def set_print_area(range, sheet: nil)
|
|
675
|
+
sheet_name = sheet || @sheets.last&.name || "Sheet1"
|
|
676
|
+
@defined_names.reject! { |dn| dn[:name] == "_xlnm.Print_Area" && dn[:local_sheet_name] == sheet_name }
|
|
677
|
+
if range
|
|
678
|
+
value = "'#{sheet_name}'!#{absolute_range(range)}"
|
|
679
|
+
defined_name("_xlnm.Print_Area", value, sheet: sheet_name, scope: sheet_name)
|
|
680
|
+
end
|
|
681
|
+
range&.to_s
|
|
682
|
+
end
|
|
683
|
+
|
|
455
684
|
#: (untyped range) -> untyped
|
|
456
685
|
def absolute_range(range)
|
|
457
686
|
DslHelpers.absolute_range(range)
|
|
@@ -473,8 +702,8 @@ module Xlsxrb
|
|
|
473
702
|
|
|
474
703
|
#: (untyped sheets) -> (::Array[untyped | ::Hash[untyped, untyped]] | ::Array[untyped])
|
|
475
704
|
def process_styles(sheets)
|
|
476
|
-
# Collect all unique StyleBuilders from
|
|
477
|
-
all_style_builders =
|
|
705
|
+
# Collect all unique StyleBuilders from workbook and sheets
|
|
706
|
+
all_style_builders = @styles.dup
|
|
478
707
|
@sheet_builders.each do |sheet_builder|
|
|
479
708
|
sheet_builder.styles.each do |style_name, style_builder|
|
|
480
709
|
all_style_builders[style_name.to_s] = style_builder
|
|
@@ -496,7 +725,7 @@ module Xlsxrb
|
|
|
496
725
|
end
|
|
497
726
|
|
|
498
727
|
sheet.rows.each do |row|
|
|
499
|
-
raw_row_style = row.unmapped_data&.[](:style_index)
|
|
728
|
+
raw_row_style = row.style_index || row.unmapped_data&.[](:style_index)
|
|
500
729
|
next unless raw_row_style.is_a?(Hash)
|
|
501
730
|
|
|
502
731
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
@@ -506,6 +735,32 @@ module Xlsxrb
|
|
|
506
735
|
sb.apply_options!(**raw_row_style)
|
|
507
736
|
all_style_builders[inline_name] = sb
|
|
508
737
|
end
|
|
738
|
+
|
|
739
|
+
# Style names that were never defined are a mistake, not an unstyled cell.
|
|
740
|
+
sheet.columns.each { |col| DslHelpers.check_style_name!(col.style_index, all_style_builders) }
|
|
741
|
+
sheet.rows.each do |row|
|
|
742
|
+
DslHelpers.check_style_name!(row.style_index || row.unmapped_data&.[](:style_index), all_style_builders)
|
|
743
|
+
row.cells.each { |cell| DslHelpers.check_style_name!(cell.style, all_style_builders) }
|
|
744
|
+
end
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
# Date and Time cells styled without a number format get a variant of their style
|
|
748
|
+
# that keeps the default date or time format.
|
|
749
|
+
date_variants = {}
|
|
750
|
+
sheets.each do |sheet|
|
|
751
|
+
sheet.rows.each do |row|
|
|
752
|
+
row.cells.each do |cell|
|
|
753
|
+
style_name = cell.style
|
|
754
|
+
next unless style_name.is_a?(String) || style_name.is_a?(Symbol)
|
|
755
|
+
|
|
756
|
+
variant = DslHelpers.date_style_variant(cell.value, style_name.to_s, all_style_builders[style_name.to_s])
|
|
757
|
+
next unless variant
|
|
758
|
+
|
|
759
|
+
variant_name, number_format = variant
|
|
760
|
+
date_variants[[style_name.to_s, cell.value.is_a?(Time) || cell.value.is_a?(DateTime)]] = variant
|
|
761
|
+
all_style_builders[variant_name] ||= all_style_builders[style_name.to_s].with_number_format(variant_name, number_format)
|
|
762
|
+
end
|
|
763
|
+
end
|
|
509
764
|
end
|
|
510
765
|
|
|
511
766
|
if all_style_builders.empty?
|
|
@@ -537,14 +792,14 @@ module Xlsxrb
|
|
|
537
792
|
resolved_col_style = if raw_col_style.is_a?(Hash)
|
|
538
793
|
inline_name = "__inline_col_#{raw_col_style.hash}"
|
|
539
794
|
sb = all_style_builders[inline_name]
|
|
540
|
-
if sb&.
|
|
541
|
-
col_fmt_map[col.index] = sb.
|
|
795
|
+
if sb&.number_format
|
|
796
|
+
col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
|
|
542
797
|
end
|
|
543
798
|
style_name_to_id[inline_name]
|
|
544
799
|
elsif (raw_col_style.is_a?(String) || raw_col_style.is_a?(Symbol)) && style_name_to_id.key?(raw_col_style.to_s)
|
|
545
800
|
sb = all_style_builders[raw_col_style.to_s]
|
|
546
|
-
if sb&.
|
|
547
|
-
col_fmt_map[col.index] = sb.
|
|
801
|
+
if sb&.number_format
|
|
802
|
+
col_fmt_map[col.index] = sb.number_format.is_a?(String) ? sb.number_format : Ooxml::Utils::BUILTIN_NUM_FMT_CODES[sb.number_format]
|
|
548
803
|
end
|
|
549
804
|
style_name_to_id[raw_col_style.to_s]
|
|
550
805
|
else
|
|
@@ -561,8 +816,10 @@ module Xlsxrb
|
|
|
561
816
|
Elements::Column.new(
|
|
562
817
|
index: col.index,
|
|
563
818
|
width: col.width,
|
|
564
|
-
hidden: col.hidden
|
|
565
|
-
custom_width: col.custom_width
|
|
819
|
+
hidden: col.hidden?,
|
|
820
|
+
custom_width: col.custom_width?,
|
|
821
|
+
best_fit: col.best_fit?,
|
|
822
|
+
collapsed: col.collapsed?,
|
|
566
823
|
outline_level: col.outline_level,
|
|
567
824
|
style_index: resolved_col_style,
|
|
568
825
|
unmapped_data: unmapped,
|
|
@@ -572,7 +829,7 @@ module Xlsxrb
|
|
|
572
829
|
end
|
|
573
830
|
|
|
574
831
|
new_rows = sheet.rows.map do |row|
|
|
575
|
-
raw_row_style = row.unmapped_data&.[](:style_index)
|
|
832
|
+
raw_row_style = row.style_index || row.unmapped_data&.[](:style_index)
|
|
576
833
|
resolved_row_style = if raw_row_style.is_a?(Hash)
|
|
577
834
|
inline_name = "__inline_row_#{raw_row_style.hash}"
|
|
578
835
|
style_name_to_id[inline_name]
|
|
@@ -582,13 +839,16 @@ module Xlsxrb
|
|
|
582
839
|
raw_row_style
|
|
583
840
|
end
|
|
584
841
|
row_unmapped = row.unmapped_data
|
|
585
|
-
if
|
|
586
|
-
row_unmapped =
|
|
587
|
-
row_unmapped
|
|
842
|
+
if row_unmapped&.key?(:style_index)
|
|
843
|
+
row_unmapped = row_unmapped.dup
|
|
844
|
+
row_unmapped.delete(:style_index)
|
|
588
845
|
end
|
|
589
846
|
|
|
590
847
|
new_cells = row.cells.map do |cell|
|
|
591
|
-
raw_cell_style = cell.style_index
|
|
848
|
+
raw_cell_style = cell.style || cell.style_index
|
|
849
|
+
dated = (cell.value.is_a?(Date) || cell.value.is_a?(Time)) && (raw_cell_style.is_a?(String) || raw_cell_style.is_a?(Symbol))
|
|
850
|
+
date_variant = dated ? date_variants[[raw_cell_style.to_s, cell.value.is_a?(Time) || cell.value.is_a?(DateTime)]] : nil
|
|
851
|
+
raw_cell_style = date_variant.first if date_variant
|
|
592
852
|
resolved_cell_style = if (raw_cell_style.is_a?(String) || raw_cell_style.is_a?(Symbol)) && style_name_to_id.key?(raw_cell_style.to_s)
|
|
593
853
|
style_name_to_id[raw_cell_style.to_s]
|
|
594
854
|
elsif raw_cell_style.nil?
|
|
@@ -597,9 +857,9 @@ module Xlsxrb
|
|
|
597
857
|
raw_cell_style
|
|
598
858
|
end
|
|
599
859
|
|
|
600
|
-
target_fmt = cell.format_code || col_fmt_map[cell.column_index]
|
|
860
|
+
target_fmt = cell.format_code || date_variant&.last || col_fmt_map[cell.column_index]
|
|
601
861
|
|
|
602
|
-
if resolved_cell_style == cell.style_index && target_fmt == cell.format_code
|
|
862
|
+
if resolved_cell_style == (cell.style || cell.style_index) && target_fmt == cell.format_code
|
|
603
863
|
cell
|
|
604
864
|
else
|
|
605
865
|
Elements::Cell.new(
|
|
@@ -607,7 +867,8 @@ module Xlsxrb
|
|
|
607
867
|
column_index: cell.column_index,
|
|
608
868
|
value: cell.value,
|
|
609
869
|
formula: cell.formula,
|
|
610
|
-
|
|
870
|
+
style: (resolved_cell_style unless resolved_cell_style.is_a?(Integer)),
|
|
871
|
+
style_index: (resolved_cell_style if resolved_cell_style.is_a?(Integer)),
|
|
611
872
|
hyperlink: cell.hyperlink,
|
|
612
873
|
comment: cell.comment,
|
|
613
874
|
raw_value: cell.raw_value,
|
|
@@ -622,10 +883,11 @@ module Xlsxrb
|
|
|
622
883
|
index: row.index,
|
|
623
884
|
cells: new_cells,
|
|
624
885
|
height: row.height,
|
|
625
|
-
hidden: row.hidden
|
|
626
|
-
custom_height: row.custom_height
|
|
627
|
-
collapsed: row.collapsed
|
|
886
|
+
hidden: row.hidden?,
|
|
887
|
+
custom_height: row.custom_height?,
|
|
888
|
+
collapsed: row.collapsed?,
|
|
628
889
|
outline_level: row.outline_level,
|
|
890
|
+
style_index: resolved_row_style,
|
|
629
891
|
unmapped_data: row_unmapped,
|
|
630
892
|
errors: row.errors
|
|
631
893
|
)
|
|
@@ -637,7 +899,7 @@ module Xlsxrb
|
|
|
637
899
|
[updated_sheets, styles_definition]
|
|
638
900
|
end
|
|
639
901
|
|
|
640
|
-
#: (untyped writer) ->
|
|
902
|
+
#: (untyped writer) -> Hash[Symbol, untyped]
|
|
641
903
|
def extract_styles_from_writer(writer)
|
|
642
904
|
# Extract style definitions from the writer that can be reused
|
|
643
905
|
# This captures the fonts, fills, borders, and xf entries that were created
|
|
@@ -645,6 +907,7 @@ module Xlsxrb
|
|
|
645
907
|
fonts: writer.fonts.dup,
|
|
646
908
|
fills: writer.fills.dup,
|
|
647
909
|
borders: writer.borders.dup,
|
|
910
|
+
cell_xfs: writer.xf_entries.dup,
|
|
648
911
|
xf_entries: writer.xf_entries.dup,
|
|
649
912
|
num_fmts: writer.num_fmts.dup
|
|
650
913
|
}
|