oxcel 0.1.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 +7 -0
- data/CHANGELOG.md +16 -0
- data/Cargo.lock +829 -0
- data/Cargo.toml +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +212 -0
- data/docs/MIGRATING_FROM_CAXLSX.md +120 -0
- data/docs/MIGRATING_FROM_ROO.md +99 -0
- data/ext/oxcel/Cargo.toml +15 -0
- data/ext/oxcel/extconf.rb +8 -0
- data/ext/oxcel/src/error.rs +20 -0
- data/ext/oxcel/src/format.rs +200 -0
- data/ext/oxcel/src/lib.rs +65 -0
- data/ext/oxcel/src/reader.rs +524 -0
- data/ext/oxcel/src/writer.rs +801 -0
- data/lib/oxcel/a1.rb +66 -0
- data/lib/oxcel/compat/caxlsx.rb +295 -0
- data/lib/oxcel/compat/roo.rb +294 -0
- data/lib/oxcel/configuration.rb +29 -0
- data/lib/oxcel/diff.rb +235 -0
- data/lib/oxcel/rails.rb +57 -0
- data/lib/oxcel/reader.rb +135 -0
- data/lib/oxcel/resource.rb +317 -0
- data/lib/oxcel/sheet.rb +188 -0
- data/lib/oxcel/types.rb +130 -0
- data/lib/oxcel/version.rb +5 -0
- data/lib/oxcel/workbook.rb +50 -0
- data/lib/oxcel.rb +169 -0
- data/rbi/oxcel.rbi +1144 -0
- data/sig/oxcel.rbs +404 -0
- data/skills/oxcel/SKILL.md +152 -0
- metadata +89 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oxcel
|
|
4
|
+
# Alba-inspired declarative Excel exporter DSL with zero-boilerplate Rails integration
|
|
5
|
+
# and batched constant-memory Rust streaming.
|
|
6
|
+
#
|
|
7
|
+
# class OrderExporter
|
|
8
|
+
# include Oxcel::Resource
|
|
9
|
+
#
|
|
10
|
+
# sheet_name "売上データ"
|
|
11
|
+
# constant_memory true
|
|
12
|
+
# freeze_header true
|
|
13
|
+
# autofilter true
|
|
14
|
+
# header_style bold: true, bg_color: "#1F4E78", font_color: "#FFFFFF", align: :center
|
|
15
|
+
# page_setup orientation: :landscape, paper_size: :a4, fit_to_pages: [1, 0], repeat_rows: [0, 0]
|
|
16
|
+
#
|
|
17
|
+
# column :id, header: "ID", width: 10
|
|
18
|
+
# column :customer, header: "顧客名", width: 24 do |order|
|
|
19
|
+
# order.customer_name
|
|
20
|
+
# end
|
|
21
|
+
# column :amount, header: "金額", width: 14, num_format: "#,##0", align: :right
|
|
22
|
+
# column :ordered_on, header: "注文日", width: 14, num_format: "yyyy/mm/dd"
|
|
23
|
+
# column :internal, header: "内部", if: ->(params) { params[:admin] }
|
|
24
|
+
#
|
|
25
|
+
# after_write do |sheet|
|
|
26
|
+
# sheet.add_row(["合計", nil, Oxcel.formula("=SUM(C2:C#{sheet.row_count})")])
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# OrderExporter.new(Order.find_each, params: { admin: true }).save("orders.xlsx")
|
|
31
|
+
# bytes = OrderExporter.new(Order.find_each).to_bytes
|
|
32
|
+
module Resource
|
|
33
|
+
ColumnDef = Struct.new(:name, :header, :width, :style_options, :condition, :extractor, keyword_init: true)
|
|
34
|
+
|
|
35
|
+
# Encapsulates all class-level Resource settings so subclass inheritance never drifts.
|
|
36
|
+
class Config
|
|
37
|
+
attr_accessor :columns, :sheet_name, :constant_memory, :freeze_header,
|
|
38
|
+
:autofilter, :include_header, :header_style, :header_height,
|
|
39
|
+
:page_setup, :tab_color, :after_write_hooks
|
|
40
|
+
|
|
41
|
+
def initialize
|
|
42
|
+
@columns = []
|
|
43
|
+
@sheet_name = nil
|
|
44
|
+
@constant_memory = true
|
|
45
|
+
@freeze_header = false
|
|
46
|
+
@autofilter = false
|
|
47
|
+
@include_header = true
|
|
48
|
+
@header_style = nil
|
|
49
|
+
@header_height = nil
|
|
50
|
+
@page_setup = nil
|
|
51
|
+
@tab_color = nil
|
|
52
|
+
@after_write_hooks = []
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def deep_copy
|
|
56
|
+
copy = dup
|
|
57
|
+
copy.columns = @columns.dup
|
|
58
|
+
copy.header_style = @header_style&.dup
|
|
59
|
+
copy.page_setup = @page_setup&.dup
|
|
60
|
+
copy.after_write_hooks = @after_write_hooks.dup
|
|
61
|
+
copy
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
STYLE_KEYS = %i[
|
|
66
|
+
bold italic strikethrough underline font_size font_name font_color bg_color
|
|
67
|
+
num_format format align valign border border_top border_bottom border_left
|
|
68
|
+
border_right border_color text_wrap shrink indent rotation
|
|
69
|
+
].freeze
|
|
70
|
+
|
|
71
|
+
def self.included(base)
|
|
72
|
+
base.extend(ClassMethods)
|
|
73
|
+
base.instance_variable_set(:@_oxcel_config, Config.new)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
module ClassMethods
|
|
77
|
+
def inherited(subclass)
|
|
78
|
+
super
|
|
79
|
+
subclass.instance_variable_set(:@_oxcel_config, _oxcel_config.deep_copy)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def _oxcel_config
|
|
83
|
+
@_oxcel_config ||= Config.new
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def _oxcel_columns
|
|
87
|
+
_oxcel_config.columns
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def sheet_name(name = nil)
|
|
91
|
+
return _oxcel_config.sheet_name || default_sheet_name if name.nil?
|
|
92
|
+
|
|
93
|
+
_oxcel_config.sheet_name = name.to_s
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def constant_memory(enabled = nil)
|
|
97
|
+
return _oxcel_config.constant_memory if enabled.nil?
|
|
98
|
+
|
|
99
|
+
_oxcel_config.constant_memory = !!enabled
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def freeze_header(enabled = nil)
|
|
103
|
+
return _oxcel_config.freeze_header if enabled.nil?
|
|
104
|
+
|
|
105
|
+
_oxcel_config.freeze_header = !!enabled
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def autofilter(enabled = nil)
|
|
109
|
+
return _oxcel_config.autofilter if enabled.nil?
|
|
110
|
+
|
|
111
|
+
_oxcel_config.autofilter = !!enabled
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def include_header(enabled = nil)
|
|
115
|
+
return _oxcel_config.include_header if enabled.nil?
|
|
116
|
+
|
|
117
|
+
_oxcel_config.include_header = !!enabled
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def header_style(**options)
|
|
121
|
+
return _oxcel_config.header_style if options.empty?
|
|
122
|
+
|
|
123
|
+
_oxcel_config.header_style = options
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def header_height(height = nil)
|
|
127
|
+
return _oxcel_config.header_height if height.nil?
|
|
128
|
+
|
|
129
|
+
_oxcel_config.header_height = height.to_f
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def page_setup(**options)
|
|
133
|
+
return _oxcel_config.page_setup if options.empty?
|
|
134
|
+
|
|
135
|
+
_oxcel_config.page_setup = options
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def tab_color(hex = nil)
|
|
139
|
+
return _oxcel_config.tab_color if hex.nil?
|
|
140
|
+
|
|
141
|
+
_oxcel_config.tab_color = hex.to_s
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Declare an export column.
|
|
145
|
+
# Any style option (`num_format:`, `format:`, `align:`, `bold:`, `bg_color:`, etc.)
|
|
146
|
+
# can be passed directly or nested inside `style: { ... }`.
|
|
147
|
+
def column(name, header: nil, width: nil, style: nil, if: nil, **inline_style, &block)
|
|
148
|
+
raise ArgumentError, "column name cannot be nil" if name.nil?
|
|
149
|
+
|
|
150
|
+
condition = binding.local_variable_get(:if)
|
|
151
|
+
unknown = inline_style.keys - STYLE_KEYS
|
|
152
|
+
unless unknown.empty?
|
|
153
|
+
raise ArgumentError, "unknown column option(s): #{unknown.join(', ')}"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
merged_style = (style || {}).merge(inline_style)
|
|
157
|
+
_oxcel_config.columns << ColumnDef.new(
|
|
158
|
+
name: name.to_sym,
|
|
159
|
+
header: (header || name.to_s),
|
|
160
|
+
width: width,
|
|
161
|
+
style_options: merged_style.empty? ? nil : merged_style,
|
|
162
|
+
condition: condition,
|
|
163
|
+
extractor: block
|
|
164
|
+
)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Alba-style shorthand for declaring multiple plain columns at once:
|
|
168
|
+
# columns :id, :name, :email
|
|
169
|
+
def columns(*names, **shared_options)
|
|
170
|
+
names.each { |n| column(n, **shared_options) }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Register a hook executed after all data rows have been streamed (e.g. for totals/notes).
|
|
174
|
+
# Receives `(sheet)` or `(sheet, params)` or `(sheet, workbook, params)`.
|
|
175
|
+
def after_write(&block)
|
|
176
|
+
raise ArgumentError, "block is required for after_write" unless block_given?
|
|
177
|
+
|
|
178
|
+
_oxcel_config.after_write_hooks << block
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
private
|
|
182
|
+
|
|
183
|
+
def default_sheet_name
|
|
184
|
+
raw = self.name ? self.name.split("::").last : "Sheet1"
|
|
185
|
+
raw.slice(0, 31)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
attr_reader :records, :params
|
|
190
|
+
|
|
191
|
+
def initialize(records, params: {}, sheet_name: nil, constant_memory: nil)
|
|
192
|
+
@records = records
|
|
193
|
+
@params = params
|
|
194
|
+
@sheet_name_override = sheet_name
|
|
195
|
+
@constant_memory_override = constant_memory
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Write this resource as a worksheet inside an existing Oxcel::Workbook (supports multi-sheet workbooks!)
|
|
199
|
+
def write_to(workbook)
|
|
200
|
+
klass = self.class
|
|
201
|
+
cfg = klass._oxcel_config
|
|
202
|
+
cols = active_columns(cfg.columns)
|
|
203
|
+
cm = @constant_memory_override.nil? ? cfg.constant_memory : !!@constant_memory_override
|
|
204
|
+
sname = (@sheet_name_override || klass.sheet_name).to_s
|
|
205
|
+
|
|
206
|
+
col_styles = cols.map do |c|
|
|
207
|
+
c.style_options ? workbook.add_style(**c.style_options) : nil
|
|
208
|
+
end
|
|
209
|
+
h_style = cfg.header_style ? workbook.add_style(**cfg.header_style) : nil
|
|
210
|
+
|
|
211
|
+
# Pre-resolve extractors once before the hot loop (Alba-style zero-overhead dispatch)
|
|
212
|
+
extractors = cols.map { |c| compile_extractor(c) }
|
|
213
|
+
col_count = cols.size
|
|
214
|
+
|
|
215
|
+
workbook.add_sheet(sname, constant_memory: cm) do |sheet|
|
|
216
|
+
sheet.tab_color(cfg.tab_color) if cfg.tab_color
|
|
217
|
+
sheet.page_setup(**cfg.page_setup) if cfg.page_setup
|
|
218
|
+
|
|
219
|
+
widths = cols.map(&:width)
|
|
220
|
+
sheet.column_widths(*widths) if widths.any?
|
|
221
|
+
|
|
222
|
+
if cfg.include_header
|
|
223
|
+
sheet.row_height(0, cfg.header_height) if cfg.header_height
|
|
224
|
+
sheet.add_row(cols.map(&:header), style: h_style)
|
|
225
|
+
sheet.freeze_panes(1, 0) if cfg.freeze_header
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
sheet.stream(@records, styles: col_styles) do |record|
|
|
229
|
+
row = Array.new(col_count)
|
|
230
|
+
i = 0
|
|
231
|
+
while i < col_count
|
|
232
|
+
row[i] = extractors[i].call(record)
|
|
233
|
+
i += 1
|
|
234
|
+
end
|
|
235
|
+
row
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
cfg.after_write_hooks.each do |hook|
|
|
239
|
+
invoke_hook(hook, sheet, workbook)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
if cfg.autofilter && cfg.include_header && col_count > 0 && sheet.row_count > 0
|
|
243
|
+
sheet.autofilter(0, 0, sheet.row_count - 1, col_count - 1)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def save(path)
|
|
249
|
+
cm = @constant_memory_override.nil? ? self.class.constant_memory : !!@constant_memory_override
|
|
250
|
+
Oxcel.write(path, constant_memory: cm) { |wb| write_to(wb) }
|
|
251
|
+
end
|
|
252
|
+
alias to_file save
|
|
253
|
+
|
|
254
|
+
def to_bytes
|
|
255
|
+
cm = @constant_memory_override.nil? ? self.class.constant_memory : !!@constant_memory_override
|
|
256
|
+
Oxcel.to_bytes(constant_memory: cm) { |wb| write_to(wb) }
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def inspect
|
|
260
|
+
sname = (@sheet_name_override || self.class.sheet_name).to_s
|
|
261
|
+
"#<#{self.class.name} sheet=#{sname.inspect} columns=#{self.class._oxcel_columns.size}>"
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
private
|
|
265
|
+
|
|
266
|
+
def invoke_hook(hook, sheet, workbook)
|
|
267
|
+
arity = hook.parameters.size
|
|
268
|
+
if hook.parameters.any? { |type, _| type == :rest } || arity >= 3
|
|
269
|
+
hook.call(sheet, workbook, @params)
|
|
270
|
+
elsif arity == 2
|
|
271
|
+
hook.call(sheet, @params)
|
|
272
|
+
elsif arity == 1
|
|
273
|
+
hook.call(sheet)
|
|
274
|
+
else
|
|
275
|
+
hook.call
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def active_columns(cols)
|
|
280
|
+
cols.select do |col|
|
|
281
|
+
cond = col.condition
|
|
282
|
+
if cond.nil?
|
|
283
|
+
true
|
|
284
|
+
elsif cond.arity == 0
|
|
285
|
+
!!cond.call
|
|
286
|
+
else
|
|
287
|
+
!!cond.call(@params)
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def compile_extractor(col)
|
|
293
|
+
if col.extractor
|
|
294
|
+
block = col.extractor
|
|
295
|
+
block_params = block.parameters
|
|
296
|
+
if block_params.empty?
|
|
297
|
+
->(_rec) { block.call }
|
|
298
|
+
elsif block_params.size >= 2 || block_params.any? { |type, _| type == :rest }
|
|
299
|
+
params = @params
|
|
300
|
+
->(rec) { block.call(rec, params) }
|
|
301
|
+
else
|
|
302
|
+
block
|
|
303
|
+
end
|
|
304
|
+
else
|
|
305
|
+
sym = col.name
|
|
306
|
+
str = sym.to_s
|
|
307
|
+
->(rec) do
|
|
308
|
+
if rec.is_a?(Hash)
|
|
309
|
+
rec.key?(sym) ? rec[sym] : rec[str]
|
|
310
|
+
else
|
|
311
|
+
rec.public_send(sym)
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
end
|
data/lib/oxcel/sheet.rb
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oxcel
|
|
4
|
+
# Worksheet writer handle backed by Rust's rust_xlsxwriter.
|
|
5
|
+
class Sheet
|
|
6
|
+
BATCH_SIZE = 1024
|
|
7
|
+
|
|
8
|
+
attr_reader :name, :row_count
|
|
9
|
+
|
|
10
|
+
def initialize(native, index, name, workbook)
|
|
11
|
+
@native = native
|
|
12
|
+
@index = index
|
|
13
|
+
@name = name
|
|
14
|
+
@workbook = workbook
|
|
15
|
+
@row_count = 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Append a row. Supports:
|
|
19
|
+
# - `style:` single Oxcel::Style or Hash applied to the whole row
|
|
20
|
+
# - `styles:` Array of Oxcel::Style / Hash / nil applied per column
|
|
21
|
+
# - Inline `Oxcel.cell(val, style: ...)` elements inside `values`
|
|
22
|
+
def add_row(values, style: nil, styles: nil)
|
|
23
|
+
format_id = resolve_style_id(style)
|
|
24
|
+
col_format_ids = resolve_column_styles(styles)
|
|
25
|
+
@native.write_row(@index, @row_count, values, format_id, col_format_ids)
|
|
26
|
+
@row_count += 1
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
alias << add_row
|
|
30
|
+
|
|
31
|
+
# Bulk-write rows in batched Rust FFI calls for high throughput
|
|
32
|
+
def add_rows(enum, style: nil, styles: nil)
|
|
33
|
+
format_id = resolve_style_id(style)
|
|
34
|
+
col_format_ids = resolve_column_styles(styles)
|
|
35
|
+
|
|
36
|
+
if enum.is_a?(Array) && enum.size <= BATCH_SIZE
|
|
37
|
+
written = @native.write_rows(@index, @row_count, enum, format_id, col_format_ids)
|
|
38
|
+
@row_count += written
|
|
39
|
+
return self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
write_batched(enum, format_id, col_format_ids)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Stream an ActiveRecord relation / Enumerable directly into batched rows
|
|
46
|
+
def stream(enum, style: nil, styles: nil, &block)
|
|
47
|
+
format_id = resolve_style_id(style)
|
|
48
|
+
col_format_ids = resolve_column_styles(styles)
|
|
49
|
+
write_batched(enum, format_id, col_format_ids, &block)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def write_cell(row, col, value, style: nil)
|
|
53
|
+
@native.write_cell(@index, row, col, value, resolve_style_id(style))
|
|
54
|
+
@row_count = row + 1 if row >= @row_count
|
|
55
|
+
self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def merge_range(first_row, first_col, last_row, last_col, text = "", style: nil)
|
|
59
|
+
@native.merge_range(@index, first_row, first_col, last_row, last_col, text.to_s, resolve_style_id(style))
|
|
60
|
+
@row_count = last_row + 1 if last_row >= @row_count
|
|
61
|
+
self
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def autofilter(first_row, first_col, last_row, last_col)
|
|
65
|
+
@native.autofilter(@index, first_row, first_col, last_row, last_col)
|
|
66
|
+
self
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def column_width(col, width)
|
|
70
|
+
@native.set_column_width(@index, col, width.to_f)
|
|
71
|
+
self
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def column_widths(*widths)
|
|
75
|
+
widths.each_with_index do |w, col|
|
|
76
|
+
@native.set_column_width(@index, col, w.to_f) unless w.nil?
|
|
77
|
+
end
|
|
78
|
+
self
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def row_height(row, height)
|
|
82
|
+
@native.set_row_height(@index, row, height.to_f)
|
|
83
|
+
self
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def freeze_panes(row, col = 0)
|
|
87
|
+
@native.set_freeze_panes(@index, row, col)
|
|
88
|
+
self
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def autofit
|
|
92
|
+
@native.autofit(@index)
|
|
93
|
+
self
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def tab_color(hex)
|
|
97
|
+
@native.set_tab_color(@index, hex.to_s)
|
|
98
|
+
self
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def gridlines(visible)
|
|
102
|
+
@native.set_screen_gridlines(@index, !!visible)
|
|
103
|
+
self
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def protect(password: nil)
|
|
107
|
+
@native.protect(@index, password&.to_s)
|
|
108
|
+
self
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def add_note(row, col, text)
|
|
112
|
+
@native.insert_note(@index, row, col, text.to_s)
|
|
113
|
+
self
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def add_dropdown(first_row, first_col, last_row, last_col, values, input_title: nil, input_message: nil, error_message: nil)
|
|
117
|
+
@native.add_dropdown(
|
|
118
|
+
@index,
|
|
119
|
+
first_row,
|
|
120
|
+
first_col,
|
|
121
|
+
last_row,
|
|
122
|
+
last_col,
|
|
123
|
+
values.map(&:to_s),
|
|
124
|
+
input_title&.to_s,
|
|
125
|
+
input_message&.to_s,
|
|
126
|
+
error_message&.to_s
|
|
127
|
+
)
|
|
128
|
+
self
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Print & Page setup for B2B reports:
|
|
132
|
+
# s.page_setup(orientation: :landscape, paper_size: :a4, fit_to_pages: [1, 0],
|
|
133
|
+
# repeat_rows: [0, 0], print_gridlines: true, header: "&C売上レポート", footer: "&R&P / &N")
|
|
134
|
+
def page_setup(**options)
|
|
135
|
+
normalized = options.each_with_object({}) do |(k, v), acc|
|
|
136
|
+
acc[k.to_s] = v.is_a?(Symbol) ? v.to_s : v
|
|
137
|
+
end
|
|
138
|
+
@native.set_page_setup(@index, normalized)
|
|
139
|
+
self
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def inspect
|
|
143
|
+
"#<#{self.class.name} name=#{@name.inspect} rows=#{@row_count}>"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
private
|
|
147
|
+
|
|
148
|
+
def write_batched(enum, format_id, col_format_ids)
|
|
149
|
+
batch = []
|
|
150
|
+
enum.each do |item|
|
|
151
|
+
batch << (block_given? ? yield(item) : item)
|
|
152
|
+
if batch.size >= BATCH_SIZE
|
|
153
|
+
written = @native.write_rows(@index, @row_count, batch, format_id, col_format_ids)
|
|
154
|
+
@row_count += written
|
|
155
|
+
batch.clear
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
unless batch.empty?
|
|
159
|
+
written = @native.write_rows(@index, @row_count, batch, format_id, col_format_ids)
|
|
160
|
+
@row_count += written
|
|
161
|
+
end
|
|
162
|
+
self
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def resolve_style_id(style)
|
|
166
|
+
case style
|
|
167
|
+
when nil
|
|
168
|
+
nil
|
|
169
|
+
when Style
|
|
170
|
+
if style.workbook && !style.workbook.equal?(@workbook)
|
|
171
|
+
raise ArgumentError, "Oxcel::Style belongs to a different Oxcel::Workbook"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
style.id
|
|
175
|
+
when Hash
|
|
176
|
+
@workbook.add_style(**style).id
|
|
177
|
+
else
|
|
178
|
+
raise ArgumentError, "style must be an Oxcel::Style, Hash, or nil"
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def resolve_column_styles(styles)
|
|
183
|
+
return nil if styles.nil?
|
|
184
|
+
|
|
185
|
+
styles.map { |s| resolve_style_id(s) }
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
data/lib/oxcel/types.rb
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oxcel
|
|
4
|
+
# Explicit Excel formula wrapper: Oxcel.formula("=SUM(A2:A100)")
|
|
5
|
+
class Formula
|
|
6
|
+
attr_reader :expression
|
|
7
|
+
|
|
8
|
+
def initialize(expression)
|
|
9
|
+
@expression = expression.to_s
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def _oxcel_formula
|
|
13
|
+
@expression
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_s
|
|
17
|
+
@expression
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def ==(other)
|
|
21
|
+
other.is_a?(Formula) && @expression == other.expression
|
|
22
|
+
end
|
|
23
|
+
alias eql? ==
|
|
24
|
+
|
|
25
|
+
def hash
|
|
26
|
+
[Formula, @expression].hash
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def inspect
|
|
30
|
+
"#<#{self.class.name} #{@expression.inspect}>"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Hyperlink wrapper: Oxcel.link("https://example.com", "Example")
|
|
35
|
+
class Hyperlink
|
|
36
|
+
attr_reader :url, :text
|
|
37
|
+
|
|
38
|
+
def initialize(url, text = nil)
|
|
39
|
+
@url = url.to_s
|
|
40
|
+
@text = text&.to_s
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def _oxcel_url
|
|
44
|
+
@url
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def _oxcel_text
|
|
48
|
+
@text
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def ==(other)
|
|
52
|
+
other.is_a?(Hyperlink) && @url == other.url && @text == other.text
|
|
53
|
+
end
|
|
54
|
+
alias eql? ==
|
|
55
|
+
|
|
56
|
+
def hash
|
|
57
|
+
[Hyperlink, @url, @text].hash
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def inspect
|
|
61
|
+
if @text
|
|
62
|
+
"#<#{self.class.name} url=#{@url.inspect} text=#{@text.inspect}>"
|
|
63
|
+
else
|
|
64
|
+
"#<#{self.class.name} url=#{@url.inspect}>"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Per-cell styled value wrapper: Oxcel.cell(1200, style: money_style)
|
|
70
|
+
class Cell
|
|
71
|
+
attr_reader :value, :style
|
|
72
|
+
|
|
73
|
+
def initialize(value, style: nil)
|
|
74
|
+
if !style.nil? && !style.is_a?(Style)
|
|
75
|
+
raise ArgumentError, "cell style must be an Oxcel::Style or nil"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
@value = value
|
|
79
|
+
@style = style
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def _oxcel_cell_value
|
|
83
|
+
@value
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def _oxcel_cell_style_id
|
|
87
|
+
@style&.id
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def ==(other)
|
|
91
|
+
other.is_a?(Cell) && @value == other.value && @style == other.style
|
|
92
|
+
end
|
|
93
|
+
alias eql? ==
|
|
94
|
+
|
|
95
|
+
def hash
|
|
96
|
+
[Cell, @value, @style].hash
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def inspect
|
|
100
|
+
if @style
|
|
101
|
+
"#<#{self.class.name} value=#{@value.inspect} style=#{@style.inspect}>"
|
|
102
|
+
else
|
|
103
|
+
"#<#{self.class.name} value=#{@value.inspect}>"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Reusable style handle backed by a cached Rust Format
|
|
109
|
+
class Style
|
|
110
|
+
attr_reader :id, :workbook
|
|
111
|
+
|
|
112
|
+
def initialize(id, workbook = nil)
|
|
113
|
+
@id = id
|
|
114
|
+
@workbook = workbook
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def ==(other)
|
|
118
|
+
other.is_a?(Style) && @id == other.id && @workbook.equal?(other.workbook)
|
|
119
|
+
end
|
|
120
|
+
alias eql? ==
|
|
121
|
+
|
|
122
|
+
def hash
|
|
123
|
+
[Style, @id, @workbook&.object_id].hash
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def inspect
|
|
127
|
+
"#<#{self.class.name} id=#{@id}>"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oxcel
|
|
4
|
+
# High-level workbook writer powered by Rust's rust_xlsxwriter.
|
|
5
|
+
class Workbook
|
|
6
|
+
def initialize(constant_memory: nil, default_date_format: nil, default_datetime_format: nil)
|
|
7
|
+
cfg = Oxcel.config
|
|
8
|
+
date_fmt = (default_date_format || cfg.default_date_format)&.to_s
|
|
9
|
+
datetime_fmt = (default_datetime_format || cfg.default_datetime_format)&.to_s
|
|
10
|
+
|
|
11
|
+
@native = NativeWriter.new(date_fmt, datetime_fmt)
|
|
12
|
+
@constant_memory = constant_memory.nil? ? cfg.constant_memory : !!constant_memory
|
|
13
|
+
@styles = {}
|
|
14
|
+
@sheet_names = []
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def closed?
|
|
18
|
+
@native.closed?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def add_style(**options)
|
|
22
|
+
normalized = options.each_with_object({}) do |(k, v), acc|
|
|
23
|
+
acc[k.to_s] = v.is_a?(Symbol) ? v.to_s : v
|
|
24
|
+
end
|
|
25
|
+
@styles[normalized] ||= Style.new(@native.add_format(normalized), self)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def add_sheet(name = "Sheet1", constant_memory: @constant_memory)
|
|
29
|
+
sheet_name = name.to_s
|
|
30
|
+
idx = @native.add_sheet(sheet_name, !!constant_memory)
|
|
31
|
+
@sheet_names << sheet_name
|
|
32
|
+
sheet = Sheet.new(@native, idx, sheet_name, self)
|
|
33
|
+
yield sheet if block_given?
|
|
34
|
+
sheet
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def save(path)
|
|
38
|
+
@native.save(path.to_s)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_bytes
|
|
42
|
+
@native.to_bytes
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def inspect
|
|
46
|
+
state = closed? ? " (closed)" : ""
|
|
47
|
+
"#<#{self.class.name} sheets=#{@sheet_names.inspect} constant_memory=#{@constant_memory}#{state}>"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|