oxcel 0.1.0-x64-mingw-ucrt

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.
@@ -0,0 +1,295 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stringio"
4
+ require_relative "../oxcel"
5
+
6
+ module Oxcel
7
+ module Compat
8
+ # Drop-in migration adapter for the `caxlsx` (`Axlsx::Package`) gem backed by Oxcel's
9
+ # constant-memory Rust writer (`rust_xlsxwriter`).
10
+ #
11
+ # require "oxcel/compat/caxlsx"
12
+ #
13
+ # package = Oxcel::Compat::Caxlsx::Package.new
14
+ # package.use_shared_strings = true
15
+ # package.workbook.add_worksheet(name: "Sheet1") do |sheet|
16
+ # sheet.add_row(["ID", "Name"], types: %i[string string])
17
+ # sheet.add_row(["1", "Alice"], types: %i[string string])
18
+ # end
19
+ # package.serialize("out.xlsx")
20
+ # bytes = package.to_stream.read
21
+ module Caxlsx
22
+ # Utility for parsing A1 cell references ("A1", "B2:D5") and normalizing ARGB colors.
23
+ module A1Ref
24
+ module_function
25
+
26
+ def parse_cell(ref)
27
+ Oxcel::A1.parse_cell(ref)
28
+ end
29
+
30
+ def parse_range(range_str)
31
+ Oxcel::A1.parse_range(range_str)
32
+ end
33
+
34
+ def normalize_color(hex)
35
+ return nil if hex.nil?
36
+
37
+ raw = hex.to_s.strip.delete_prefix("#")
38
+ # caxlsx frequently uses 8-digit ARGB (e.g. "FF1F4E78"); strip leading alpha byte.
39
+ raw = raw[2, 6] if raw.length == 8 && raw.match?(/\A[0-9a-fA-F]{8}\z/)
40
+ "##{raw}"
41
+ end
42
+ end
43
+
44
+ # Translates caxlsx `wb.styles.add_style(...)` options into `Oxcel::Style`.
45
+ class Styles
46
+ def initialize(oxcel_workbook)
47
+ @oxcel_workbook = oxcel_workbook
48
+ end
49
+
50
+ def add_style(**options)
51
+ @oxcel_workbook.add_style(**translate_style(options))
52
+ end
53
+
54
+ private
55
+
56
+ def translate_style(opts)
57
+ out = {}
58
+ opts.each do |k, v|
59
+ case k
60
+ when :b, :bold
61
+ out[:bold] = !!v
62
+ when :i, :italic
63
+ out[:italic] = !!v
64
+ when :u, :underline
65
+ out[:underline] = v == true ? :single : v
66
+ when :strike, :strikethrough
67
+ out[:strikethrough] = !!v
68
+ when :sz, :font_size
69
+ out[:font_size] = v
70
+ when :font_name
71
+ out[:font_name] = v
72
+ when :fg_color, :font_color
73
+ out[:font_color] = A1Ref.normalize_color(v)
74
+ when :bg_color
75
+ out[:bg_color] = A1Ref.normalize_color(v)
76
+ when :format_code, :num_format, :format
77
+ out[:num_format] = v
78
+ when :alignment
79
+ translate_alignment(v, out) if v.is_a?(Hash)
80
+ when :border
81
+ translate_border(v, out)
82
+ when :align, :valign, :text_wrap, :shrink, :indent, :rotation,
83
+ :border_top, :border_bottom, :border_left, :border_right, :border_color
84
+ out[k] = v
85
+ end
86
+ end
87
+ out
88
+ end
89
+
90
+ def translate_alignment(align_hash, out)
91
+ out[:align] = align_hash[:horizontal] if align_hash.key?(:horizontal)
92
+ out[:valign] = align_hash[:vertical] if align_hash.key?(:vertical)
93
+ out[:text_wrap] = !!align_hash[:wrap_text] if align_hash.key?(:wrap_text)
94
+ out[:indent] = align_hash[:indent] if align_hash.key?(:indent)
95
+ out[:rotation] = align_hash[:text_rotation] if align_hash.key?(:text_rotation)
96
+ out[:shrink] = !!align_hash[:shrink_to_fit] if align_hash.key?(:shrink_to_fit)
97
+ end
98
+
99
+ def translate_border(border_opt, out)
100
+ if border_opt.is_a?(Hash)
101
+ style = border_opt[:style] || :thin
102
+ edges = border_opt[:edges]
103
+ if edges.is_a?(Array) && !edges.empty?
104
+ edges.each do |edge|
105
+ case edge.to_sym
106
+ when :top then out[:border_top] = style
107
+ when :bottom then out[:border_bottom] = style
108
+ when :left then out[:border_left] = style
109
+ when :right then out[:border_right] = style
110
+ end
111
+ end
112
+ else
113
+ out[:border] = style
114
+ end
115
+ out[:border_color] = A1Ref.normalize_color(border_opt[:color]) if border_opt[:color]
116
+ else
117
+ out[:border] = border_opt
118
+ end
119
+ end
120
+ end
121
+
122
+ # Lightweight shim for `sheet.sheet_view.pane { |p| p.state = :frozen; p.y_split = 1 }`
123
+ class Pane
124
+ attr_accessor :state, :x_split, :y_split
125
+
126
+ def initialize
127
+ @state = nil
128
+ @x_split = 0
129
+ @y_split = 0
130
+ end
131
+ end
132
+
133
+ class SheetView
134
+ def initialize(oxcel_sheet)
135
+ @oxcel_sheet = oxcel_sheet
136
+ end
137
+
138
+ def pane
139
+ p = Pane.new
140
+ yield p if block_given?
141
+ if p.state == :frozen || (p.y_split && p.y_split > 0) || (p.x_split && p.x_split > 0)
142
+ @oxcel_sheet.freeze_panes(p.y_split.to_i, p.x_split.to_i)
143
+ end
144
+ p
145
+ end
146
+ end
147
+
148
+ # caxlsx-compatible Worksheet wrapper over `Oxcel::Sheet`.
149
+ class Worksheet
150
+ attr_reader :name
151
+
152
+ def initialize(oxcel_sheet)
153
+ @oxcel_sheet = oxcel_sheet
154
+ @name = oxcel_sheet.name
155
+ end
156
+
157
+ def row_count
158
+ @oxcel_sheet.row_count
159
+ end
160
+
161
+ # Supports caxlsx `add_row(values, style: s_or_array, types: sym_or_array, height: num)`.
162
+ def add_row(values = [], style: nil, types: nil, height: nil)
163
+ coerced = types.nil? ? values : coerce_values(values, types)
164
+ row_idx = @oxcel_sheet.row_count
165
+ @oxcel_sheet.row_height(row_idx, height) if height
166
+
167
+ if style.is_a?(Array)
168
+ @oxcel_sheet.add_row(coerced, styles: style)
169
+ else
170
+ @oxcel_sheet.add_row(coerced, style: style)
171
+ end
172
+
173
+ self
174
+ end
175
+
176
+ def column_widths(*widths)
177
+ @oxcel_sheet.column_widths(*widths)
178
+ self
179
+ end
180
+
181
+ def merge_cells(range_str, text = "", style: nil)
182
+ r1, c1, r2, c2 = A1Ref.parse_range(range_str)
183
+ @oxcel_sheet.merge_range(r1, c1, r2, c2, text, style: style)
184
+ self
185
+ end
186
+
187
+ def auto_filter=(range_str)
188
+ r1, c1, r2, c2 = A1Ref.parse_range(range_str)
189
+ @oxcel_sheet.autofilter(r1, c1, r2, c2)
190
+ range_str
191
+ end
192
+
193
+ def sheet_view
194
+ @sheet_view ||= SheetView.new(@oxcel_sheet)
195
+ end
196
+
197
+ def add_comment(ref:, text:, author: nil)
198
+ _ = author
199
+ r, c = A1Ref.parse_cell(ref)
200
+ @oxcel_sheet.add_note(r, c, text.to_s)
201
+ self
202
+ end
203
+
204
+ def add_data_validation(range_str, opts = {})
205
+ r1, c1, r2, c2 = A1Ref.parse_range(range_str)
206
+ if opts[:type] == :list && opts[:formula1]
207
+ values = parse_list_formula(opts[:formula1])
208
+ @oxcel_sheet.add_dropdown(
209
+ r1, c1, r2, c2, values,
210
+ input_title: opts[:promptTitle],
211
+ input_message: opts[:prompt],
212
+ error_message: opts[:error]
213
+ )
214
+ end
215
+ self
216
+ end
217
+
218
+ private
219
+
220
+ def coerce_values(values, types)
221
+ values.each_with_index.map do |val, idx|
222
+ t = types.is_a?(Array) ? types[idx] : types
223
+ next val if t.nil? || val.nil?
224
+
225
+ case t.to_sym
226
+ when :string, :text
227
+ val.to_s
228
+ when :integer
229
+ val.to_i
230
+ when :float
231
+ val.to_f
232
+ when :boolean
233
+ !!val
234
+ else
235
+ val
236
+ end
237
+ end
238
+ end
239
+
240
+ def parse_list_formula(formula)
241
+ stripped = formula.to_s.strip
242
+ stripped = stripped[1..-2] if stripped.start_with?('"') && stripped.end_with?('"')
243
+ stripped.split(",").map(&:strip)
244
+ end
245
+ end
246
+
247
+ # caxlsx-compatible Workbook wrapper over `Oxcel::Workbook`.
248
+ class Workbook
249
+ attr_reader :worksheets
250
+
251
+ def initialize(oxcel_workbook, constant_memory: true)
252
+ @oxcel_workbook = oxcel_workbook
253
+ @constant_memory = constant_memory
254
+ @styles = Styles.new(oxcel_workbook)
255
+ @worksheets = []
256
+ end
257
+
258
+ def styles
259
+ yield @styles if block_given?
260
+ @styles
261
+ end
262
+
263
+ def add_worksheet(name: "Sheet1", constant_memory: @constant_memory)
264
+ oxcel_sheet = @oxcel_workbook.add_sheet(name, constant_memory: constant_memory)
265
+ ws = Worksheet.new(oxcel_sheet)
266
+ @worksheets << ws
267
+ yield ws if block_given?
268
+ ws
269
+ end
270
+ end
271
+
272
+ # caxlsx-compatible Package entry point (`Oxcel::Compat::Caxlsx::Package.new`).
273
+ class Package
274
+ attr_accessor :use_shared_strings, :use_autowidth
275
+ attr_reader :workbook
276
+
277
+ def initialize(constant_memory: true)
278
+ @oxcel_workbook = Oxcel::Workbook.new(constant_memory: constant_memory)
279
+ @workbook = Workbook.new(@oxcel_workbook, constant_memory: constant_memory)
280
+ @use_shared_strings = false
281
+ @use_autowidth = false
282
+ end
283
+
284
+ def serialize(path)
285
+ @oxcel_workbook.save(path.to_s)
286
+ true
287
+ end
288
+
289
+ def to_stream
290
+ StringIO.new(@oxcel_workbook.to_bytes)
291
+ end
292
+ end
293
+ end
294
+ end
295
+ end
@@ -0,0 +1,294 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../oxcel"
4
+
5
+ module Oxcel
6
+ module Compat
7
+ # Drop-in migration adapter for the `roo` gem backed by Oxcel's Rust reader.
8
+ #
9
+ # require "oxcel/compat/roo"
10
+ #
11
+ # xlsx = Oxcel::Compat::Roo::Excelx.new("orders.xlsx")
12
+ # sheet = xlsx.sheet(0)
13
+ # sheet.row(1) # 1-indexed, just like Roo
14
+ # sheet.each_row_streaming(offset: 1) { |cells| p cells.map(&:value) }
15
+ module Roo
16
+ # Lightweight Roo-compatible cell wrapper returned by `each_row_streaming`.
17
+ class Cell
18
+ attr_reader :value, :coordinate
19
+
20
+ def initialize(value, coordinate: nil)
21
+ @value = value
22
+ @coordinate = coordinate
23
+ end
24
+
25
+ def empty?
26
+ @value.nil? || @value == ""
27
+ end
28
+
29
+ def to_s
30
+ @value.to_s
31
+ end
32
+
33
+ def ==(other)
34
+ other.is_a?(Cell) ? @value == other.value : @value == other
35
+ end
36
+ end
37
+
38
+ # 1-indexed worksheet view matching Roo's core reading API.
39
+ class Sheet
40
+ attr_reader :name
41
+
42
+ def initialize(reader, name, coerce_integers: true)
43
+ @reader = reader
44
+ @name = name
45
+ @coerce_integers = coerce_integers
46
+ @rows = nil
47
+ end
48
+
49
+ def first_row
50
+ rows = loaded_rows
51
+ rows.empty? ? nil : 1
52
+ end
53
+
54
+ def last_row
55
+ rows = loaded_rows
56
+ rows.empty? ? nil : rows.size
57
+ end
58
+
59
+ def first_column
60
+ rows = loaded_rows
61
+ rows.empty? || rows.all?(&:empty?) ? nil : 1
62
+ end
63
+
64
+ def last_column
65
+ rows = loaded_rows
66
+ return nil if rows.empty?
67
+
68
+ max = rows.map(&:size).max || 0
69
+ max > 0 ? max : nil
70
+ end
71
+
72
+ # 1-indexed row lookup (matches Roo::Base#row).
73
+ def row(row_number)
74
+ raise ArgumentError, "row_number must be >= 1" if row_number < 1
75
+
76
+ loaded_rows[row_number - 1] || []
77
+ end
78
+
79
+ # 1-indexed cell lookup; `col` may be a 1-based Integer or Excel column letters (e.g. "A", "AA").
80
+ def cell(row_number, col)
81
+ raise ArgumentError, "row_number must be >= 1" if row_number < 1
82
+
83
+ col_index = resolve_column_index(col)
84
+ row_values = loaded_rows[row_number - 1]
85
+ row_values ? row_values[col_index] : nil
86
+ end
87
+
88
+ # True O(1) streaming iterator yielding Arrays of `Oxcel::Compat::Roo::Cell`.
89
+ def each_row_streaming(offset: 0, max_rows: nil, pad_cells: false)
90
+ unless block_given?
91
+ return enum_for(:each_row_streaming, offset: offset, max_rows: max_rows, pad_cells: pad_cells)
92
+ end
93
+
94
+ raise ArgumentError, "offset must be >= 0" if offset < 0
95
+
96
+ row_num = 0
97
+ yielded = 0
98
+ @reader.each_row(@name, coerce_integers: @coerce_integers) do |raw_row|
99
+ row_num += 1
100
+ next if row_num <= offset
101
+ break if max_rows && yielded >= max_rows
102
+
103
+ cells = raw_row.each_with_index.map do |val, col_idx|
104
+ Cell.new(val, coordinate: [row_num, col_idx + 1])
105
+ end
106
+ yield cells
107
+ yielded += 1
108
+ end
109
+ end
110
+
111
+ # Roo-compatible `parse`:
112
+ # - `sheet.parse(headers: true)` => Array<Hash> including the header row at index 0 (matches Roo 4.0)
113
+ # - `sheet.parse(id: "ID", name: /Name/)` => Array<Hash> mapped by header spec (excludes header row)
114
+ def parse(options = {})
115
+ opts = options.dup
116
+ clean = !!opts.delete(:clean)
117
+ headers_opt = opts.delete(:headers)
118
+
119
+ result = []
120
+ if headers_opt == true && opts.empty?
121
+ header_names = nil
122
+ @reader.each_row(@name, coerce_integers: @coerce_integers) do |raw_row|
123
+ row = clean ? clean_row(raw_row) : raw_row
124
+ if header_names.nil?
125
+ header_names = row.map { |h| h.nil? ? "" : h.to_s }
126
+ end
127
+ hash = {}
128
+ header_names.each_with_index { |k, i| hash[k] = row[i] }
129
+ result << hash
130
+ end
131
+ elsif !opts.empty?
132
+ col_map = nil
133
+ @reader.each_row(@name, coerce_integers: @coerce_integers) do |raw_row|
134
+ row = clean ? clean_row(raw_row) : raw_row
135
+ if col_map.nil?
136
+ col_map = match_header_spec(row, opts)
137
+ next
138
+ end
139
+ hash = {}
140
+ col_map.each { |key, idx| hash[key] = row[idx] }
141
+ result << hash
142
+ end
143
+ else
144
+ @reader.each_row(@name, coerce_integers: @coerce_integers) do |raw_row|
145
+ result << (clean ? clean_row(raw_row) : raw_row)
146
+ end
147
+ end
148
+ result
149
+ end
150
+
151
+ def each(headers = nil, &block)
152
+ return enum_for(:each, headers) unless block_given?
153
+
154
+ if headers.is_a?(Hash)
155
+ parse(headers).each(&block)
156
+ else
157
+ @reader.each_row(@name, coerce_integers: @coerce_integers, &block)
158
+ end
159
+ end
160
+
161
+ private
162
+
163
+ def loaded_rows
164
+ @rows ||= @reader.read(@name, coerce_integers: @coerce_integers)
165
+ end
166
+
167
+ def clean_row(row)
168
+ row.map { |v| v.is_a?(String) ? v.strip : v }
169
+ end
170
+
171
+ def match_header_spec(header_row, spec)
172
+ spec.each_with_object({}) do |(key, matcher), acc|
173
+ idx = header_row.index do |cell|
174
+ s = cell.nil? ? "" : cell.to_s
175
+ matcher.is_a?(Regexp) ? matcher.match?(s) : s == matcher.to_s
176
+ end
177
+ raise ArgumentError, "header #{matcher.inspect} not found in #{header_row.inspect}" if idx.nil?
178
+
179
+ acc[key] = idx
180
+ end
181
+ end
182
+
183
+ def resolve_column_index(col)
184
+ case col
185
+ when Integer
186
+ raise ArgumentError, "column must be >= 1" if col < 1
187
+
188
+ col - 1
189
+ when String, Symbol
190
+ Oxcel::A1.letters_to_col(col)
191
+ else
192
+ raise ArgumentError, "column must be an Integer or column letter String"
193
+ end
194
+ end
195
+ end
196
+
197
+ # Roo::Excelx-compatible workbook entry point.
198
+ class Excelx
199
+ attr_reader :default_sheet
200
+
201
+ def initialize(path_or_io, file_warning: :ignore, extension: nil, coerce_integers: true)
202
+ _ = file_warning
203
+ @reader = Oxcel::Reader.new(path_or_io, format: extension)
204
+ @coerce_integers = coerce_integers
205
+ @sheets_cache = {}
206
+ @default_sheet = @reader.sheet_names.first
207
+ end
208
+
209
+ def sheets
210
+ @reader.sheet_names
211
+ end
212
+
213
+ def default_sheet=(name)
214
+ unless sheets.include?(name.to_s)
215
+ raise RangeError, "sheet #{name.inspect} not found"
216
+ end
217
+
218
+ @default_sheet = name.to_s
219
+ end
220
+
221
+ def sheet(index_or_name)
222
+ resolved = case index_or_name
223
+ when Integer
224
+ sheets.fetch(index_or_name)
225
+ else
226
+ name = index_or_name.to_s
227
+ raise RangeError, "sheet #{name.inspect} not found" unless sheets.include?(name)
228
+
229
+ name
230
+ end
231
+ @sheets_cache[resolved] ||= Sheet.new(@reader, resolved, coerce_integers: @coerce_integers)
232
+ end
233
+
234
+ def first_row
235
+ current_sheet.first_row
236
+ end
237
+
238
+ def last_row
239
+ current_sheet.last_row
240
+ end
241
+
242
+ def first_column
243
+ current_sheet.first_column
244
+ end
245
+
246
+ def last_column
247
+ current_sheet.last_column
248
+ end
249
+
250
+ def row(row_number)
251
+ current_sheet.row(row_number)
252
+ end
253
+
254
+ def cell(row_number, col)
255
+ current_sheet.cell(row_number, col)
256
+ end
257
+
258
+ def each_row_streaming(offset: 0, max_rows: nil, pad_cells: false, &block)
259
+ current_sheet.each_row_streaming(offset: offset, max_rows: max_rows, pad_cells: pad_cells, &block)
260
+ end
261
+
262
+ def parse(options = {})
263
+ current_sheet.parse(options)
264
+ end
265
+
266
+ def each(headers = nil, &block)
267
+ current_sheet.each(headers, &block)
268
+ end
269
+
270
+ def close
271
+ @reader.close
272
+ end
273
+
274
+ private
275
+
276
+ def current_sheet
277
+ raise RangeError, "workbook has no sheets" if @default_sheet.nil?
278
+
279
+ sheet(@default_sheet)
280
+ end
281
+ end
282
+
283
+ # Roo::Spreadsheet-compatible factory (`Oxcel::Compat::Roo::Spreadsheet.open(path)`).
284
+ module Spreadsheet
285
+ module_function
286
+
287
+ def open(path_or_io, options = {})
288
+ ext = options[:extension] || options["extension"]
289
+ Excelx.new(path_or_io, extension: ext)
290
+ end
291
+ end
292
+ end
293
+ end
294
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oxcel
4
+ # Global configuration (Alba / Kaminari style):
5
+ #
6
+ # Oxcel.configure do |config|
7
+ # config.coerce_integers = true
8
+ # config.constant_memory = true
9
+ # config.default_date_format = "yyyy/mm/dd"
10
+ # config.default_datetime_format = "yyyy/mm/dd hh:mm:ss"
11
+ # end
12
+ class Configuration
13
+ attr_accessor :coerce_integers, :constant_memory, :default_date_format, :default_datetime_format
14
+
15
+ def initialize
16
+ @coerce_integers = false
17
+ @constant_memory = false
18
+ @default_date_format = "yyyy-mm-dd"
19
+ @default_datetime_format = "yyyy-mm-dd hh:mm:ss"
20
+ end
21
+
22
+ def inspect
23
+ "#<#{self.class.name} coerce_integers=#{@coerce_integers.inspect} " \
24
+ "constant_memory=#{@constant_memory.inspect} " \
25
+ "default_date_format=#{@default_date_format.inspect} " \
26
+ "default_datetime_format=#{@default_datetime_format.inspect}>"
27
+ end
28
+ end
29
+ end