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,801 @@
|
|
|
1
|
+
use std::cell::RefCell;
|
|
2
|
+
|
|
3
|
+
use magnus::{prelude::*, r_hash::ForEach, Error, Float, Integer, RArray, RHash, RString, Ruby, Value};
|
|
4
|
+
use rust_xlsxwriter::{
|
|
5
|
+
DataValidation, ExcelDateTime as WriteDateTime, Format, Note, Url, Workbook,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
use crate::error::{closed_err, file_err, oxcel_err};
|
|
9
|
+
use crate::format::{build_format, parse_color};
|
|
10
|
+
|
|
11
|
+
const EXCEL_MAX_COL: usize = 16_383; // Column XFD (16,384 columns, 0-indexed)
|
|
12
|
+
|
|
13
|
+
struct WriterState {
|
|
14
|
+
workbook: Option<Workbook>,
|
|
15
|
+
formats: Vec<Format>,
|
|
16
|
+
default_date_format: Format,
|
|
17
|
+
default_datetime_format: Format,
|
|
18
|
+
default_plain_format: Format,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#[magnus::wrap(class = "Oxcel::NativeWriter", free_immediately, size)]
|
|
22
|
+
pub struct NativeWriter {
|
|
23
|
+
state: RefCell<WriterState>,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
struct WriteCtx<'a> {
|
|
27
|
+
ruby: &'a Ruby,
|
|
28
|
+
ws: &'a mut rust_xlsxwriter::Worksheet,
|
|
29
|
+
formats: &'a [Format],
|
|
30
|
+
default_date_fmt: &'a Format,
|
|
31
|
+
default_datetime_fmt: &'a Format,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
impl<'a> WriteCtx<'a> {
|
|
35
|
+
fn write_cell(
|
|
36
|
+
&mut self,
|
|
37
|
+
row: u32,
|
|
38
|
+
col: u16,
|
|
39
|
+
val: Value,
|
|
40
|
+
fmt: Option<&Format>,
|
|
41
|
+
) -> Result<(), Error> {
|
|
42
|
+
let ruby = self.ruby;
|
|
43
|
+
if val.is_nil() {
|
|
44
|
+
if let Some(f) = fmt {
|
|
45
|
+
self.ws
|
|
46
|
+
.write_blank(row, col, f)
|
|
47
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
48
|
+
}
|
|
49
|
+
return Ok(());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if val.is_kind_of(ruby.class_true_class()) {
|
|
53
|
+
match fmt {
|
|
54
|
+
Some(f) => self.ws.write_boolean_with_format(row, col, true, f),
|
|
55
|
+
None => self.ws.write_boolean(row, col, true),
|
|
56
|
+
}
|
|
57
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
58
|
+
return Ok(());
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if val.is_kind_of(ruby.class_false_class()) {
|
|
62
|
+
match fmt {
|
|
63
|
+
Some(f) => self.ws.write_boolean_with_format(row, col, false, f),
|
|
64
|
+
None => self.ws.write_boolean(row, col, false),
|
|
65
|
+
}
|
|
66
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
67
|
+
return Ok(());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if let Some(int_val) = Integer::from_value(val) {
|
|
71
|
+
let num = match int_val.to_i64() {
|
|
72
|
+
Ok(i) => i as f64,
|
|
73
|
+
Err(_) => val.funcall("to_f", ())?,
|
|
74
|
+
};
|
|
75
|
+
match fmt {
|
|
76
|
+
Some(f) => self.ws.write_number_with_format(row, col, num, f),
|
|
77
|
+
None => self.ws.write_number(row, col, num),
|
|
78
|
+
}
|
|
79
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
80
|
+
return Ok(());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if let Some(flt_val) = Float::from_value(val) {
|
|
84
|
+
let num = flt_val.to_f64();
|
|
85
|
+
match fmt {
|
|
86
|
+
Some(f) => self.ws.write_number_with_format(row, col, num, f),
|
|
87
|
+
None => self.ws.write_number(row, col, num),
|
|
88
|
+
}
|
|
89
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
90
|
+
return Ok(());
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if let Some(str_val) = RString::from_value(val) {
|
|
94
|
+
if let Ok(s) = unsafe { str_val.as_str() } {
|
|
95
|
+
match fmt {
|
|
96
|
+
Some(f) => self.ws.write_string_with_format(row, col, s, f),
|
|
97
|
+
None => self.ws.write_string(row, col, s),
|
|
98
|
+
}
|
|
99
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
100
|
+
} else {
|
|
101
|
+
let utf8: String = val.funcall("encode", ("UTF-8",))?;
|
|
102
|
+
match fmt {
|
|
103
|
+
Some(f) => self.ws.write_string_with_format(row, col, &utf8, f),
|
|
104
|
+
None => self.ws.write_string(row, col, &utf8),
|
|
105
|
+
}
|
|
106
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
107
|
+
}
|
|
108
|
+
return Ok(());
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if val.is_kind_of(ruby.class_time()) {
|
|
112
|
+
let y: u16 = val.funcall("year", ())?;
|
|
113
|
+
let m: u8 = val.funcall("month", ())?;
|
|
114
|
+
let d: u8 = val.funcall("day", ())?;
|
|
115
|
+
let h: u16 = val.funcall("hour", ())?;
|
|
116
|
+
let min: u8 = val.funcall("min", ())?;
|
|
117
|
+
let sec: u8 = val.funcall("sec", ())?;
|
|
118
|
+
let dt = WriteDateTime::from_ymd(y, m, d)
|
|
119
|
+
.and_then(|dt| dt.and_hms(h, min, sec))
|
|
120
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
121
|
+
let use_fmt = fmt.unwrap_or(self.default_datetime_fmt);
|
|
122
|
+
self.ws
|
|
123
|
+
.write_datetime_with_format(row, col, &dt, use_fmt)
|
|
124
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
125
|
+
return Ok(());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if val.respond_to("_oxcel_formula", false)? {
|
|
129
|
+
let formula: String = val.funcall("_oxcel_formula", ())?;
|
|
130
|
+
match fmt {
|
|
131
|
+
Some(f) => self.ws.write_formula_with_format(row, col, formula.as_str(), f),
|
|
132
|
+
None => self.ws.write_formula(row, col, formula.as_str()),
|
|
133
|
+
}
|
|
134
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
135
|
+
return Ok(());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if val.respond_to("_oxcel_url", false)? {
|
|
139
|
+
let url_str: String = val.funcall("_oxcel_url", ())?;
|
|
140
|
+
let text_opt: Option<String> = val.funcall("_oxcel_text", ())?;
|
|
141
|
+
let mut link = Url::new(&url_str);
|
|
142
|
+
if let Some(ref t) = text_opt {
|
|
143
|
+
link = link.set_text(t);
|
|
144
|
+
}
|
|
145
|
+
match fmt {
|
|
146
|
+
Some(f) => self.ws.write_url_with_format(row, col, link, f),
|
|
147
|
+
None => self.ws.write_url(row, col, link),
|
|
148
|
+
}
|
|
149
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
150
|
+
return Ok(());
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if val.respond_to("year", false)?
|
|
154
|
+
&& val.respond_to("month", false)?
|
|
155
|
+
&& val.respond_to("day", false)?
|
|
156
|
+
{
|
|
157
|
+
let y: u16 = val.funcall("year", ())?;
|
|
158
|
+
let m: u8 = val.funcall("month", ())?;
|
|
159
|
+
let d: u8 = val.funcall("day", ())?;
|
|
160
|
+
if val.respond_to("hour", false)?
|
|
161
|
+
&& val.respond_to("min", false)?
|
|
162
|
+
&& val.respond_to("sec", false)?
|
|
163
|
+
{
|
|
164
|
+
let h: u16 = val.funcall("hour", ())?;
|
|
165
|
+
let min: u8 = val.funcall("min", ())?;
|
|
166
|
+
let sec: u8 = val.funcall("sec", ())?;
|
|
167
|
+
let dt = WriteDateTime::from_ymd(y, m, d)
|
|
168
|
+
.and_then(|dt| dt.and_hms(h, min, sec))
|
|
169
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
170
|
+
let use_fmt = fmt.unwrap_or(self.default_datetime_fmt);
|
|
171
|
+
self.ws
|
|
172
|
+
.write_datetime_with_format(row, col, &dt, use_fmt)
|
|
173
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
174
|
+
} else {
|
|
175
|
+
let dt = WriteDateTime::from_ymd(y, m, d)
|
|
176
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
177
|
+
let use_fmt = fmt.unwrap_or(self.default_date_fmt);
|
|
178
|
+
self.ws
|
|
179
|
+
.write_datetime_with_format(row, col, &dt, use_fmt)
|
|
180
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
181
|
+
}
|
|
182
|
+
return Ok(());
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if val.is_kind_of(ruby.class_numeric()) {
|
|
186
|
+
let num: f64 = val.funcall("to_f", ())?;
|
|
187
|
+
match fmt {
|
|
188
|
+
Some(f) => self.ws.write_number_with_format(row, col, num, f),
|
|
189
|
+
None => self.ws.write_number(row, col, num),
|
|
190
|
+
}
|
|
191
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
192
|
+
return Ok(());
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let s: String = val.funcall("to_s", ())?;
|
|
196
|
+
match fmt {
|
|
197
|
+
Some(f) => self.ws.write_string_with_format(row, col, &s, f),
|
|
198
|
+
None => self.ws.write_string(row, col, &s),
|
|
199
|
+
}
|
|
200
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
201
|
+
Ok(())
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fn write_row(
|
|
205
|
+
&mut self,
|
|
206
|
+
row_idx: u32,
|
|
207
|
+
values: RArray,
|
|
208
|
+
row_fmt: Option<&Format>,
|
|
209
|
+
col_fmt_ids: &[Option<usize>],
|
|
210
|
+
) -> Result<(), Error> {
|
|
211
|
+
let ruby = self.ruby;
|
|
212
|
+
for (col_idx, raw_val) in values.into_iter().enumerate() {
|
|
213
|
+
if col_idx > EXCEL_MAX_COL {
|
|
214
|
+
return Err(Error::new(
|
|
215
|
+
ruby.exception_arg_error(),
|
|
216
|
+
format!("column index {col_idx} exceeds Excel maximum ({EXCEL_MAX_COL})"),
|
|
217
|
+
));
|
|
218
|
+
}
|
|
219
|
+
let col = col_idx as u16;
|
|
220
|
+
let mut val = raw_val;
|
|
221
|
+
let mut cell_fmt = row_fmt;
|
|
222
|
+
|
|
223
|
+
if let Some(Some(id)) = col_fmt_ids.get(col_idx) {
|
|
224
|
+
cell_fmt = Some(self.formats.get(*id).ok_or_else(|| {
|
|
225
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
226
|
+
})?);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if val.respond_to("_oxcel_cell_value", false)? {
|
|
230
|
+
let inner_val: Value = val.funcall("_oxcel_cell_value", ())?;
|
|
231
|
+
let inner_fmt_id: Option<usize> = val.funcall("_oxcel_cell_style_id", ())?;
|
|
232
|
+
val = inner_val;
|
|
233
|
+
if let Some(id) = inner_fmt_id {
|
|
234
|
+
cell_fmt = Some(self.formats.get(id).ok_or_else(|| {
|
|
235
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
236
|
+
})?);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
self.write_cell(row_idx, col, val, cell_fmt)?;
|
|
241
|
+
}
|
|
242
|
+
Ok(())
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
fn extract_col_fmt_ids(
|
|
247
|
+
ruby: &Ruby,
|
|
248
|
+
col_format_ids: Option<RArray>,
|
|
249
|
+
) -> Result<Vec<Option<usize>>, Error> {
|
|
250
|
+
let mut out = Vec::new();
|
|
251
|
+
if let Some(arr) = col_format_ids {
|
|
252
|
+
out.reserve(arr.len());
|
|
253
|
+
for item in arr.into_iter() {
|
|
254
|
+
if item.is_nil() {
|
|
255
|
+
out.push(None);
|
|
256
|
+
} else {
|
|
257
|
+
let id: usize = magnus::TryConvert::try_convert(item).map_err(|_| {
|
|
258
|
+
Error::new(
|
|
259
|
+
ruby.exception_type_error(),
|
|
260
|
+
"column format_id must be Integer or nil",
|
|
261
|
+
)
|
|
262
|
+
})?;
|
|
263
|
+
out.push(Some(id));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
Ok(out)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
impl NativeWriter {
|
|
271
|
+
pub fn new(date_fmt: Option<String>, datetime_fmt: Option<String>) -> Self {
|
|
272
|
+
let df = date_fmt.unwrap_or_else(|| "yyyy-mm-dd".to_string());
|
|
273
|
+
let dtf = datetime_fmt.unwrap_or_else(|| "yyyy-mm-dd hh:mm:ss".to_string());
|
|
274
|
+
Self {
|
|
275
|
+
state: RefCell::new(WriterState {
|
|
276
|
+
workbook: Some(Workbook::new()),
|
|
277
|
+
formats: Vec::new(),
|
|
278
|
+
default_date_format: Format::new().set_num_format(&df),
|
|
279
|
+
default_datetime_format: Format::new().set_num_format(&dtf),
|
|
280
|
+
default_plain_format: Format::new(),
|
|
281
|
+
}),
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
pub fn is_closed(&self) -> bool {
|
|
286
|
+
self.state
|
|
287
|
+
.try_borrow()
|
|
288
|
+
.map(|st| st.workbook.is_none())
|
|
289
|
+
.unwrap_or(false)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
fn borrow_state_mut(&self, ruby: &Ruby) -> Result<std::cell::RefMut<'_, WriterState>, Error> {
|
|
293
|
+
self.state
|
|
294
|
+
.try_borrow_mut()
|
|
295
|
+
.map_err(|_| oxcel_err(ruby, "workbook is currently busy (re-entrant call)"))
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn with_worksheet<F>(&self, ruby: &Ruby, sheet_idx: usize, f: F) -> Result<(), Error>
|
|
299
|
+
where
|
|
300
|
+
F: FnOnce(&mut rust_xlsxwriter::Worksheet) -> Result<(), Error>,
|
|
301
|
+
{
|
|
302
|
+
let mut st = self.borrow_state_mut(ruby)?;
|
|
303
|
+
let wb = st
|
|
304
|
+
.workbook
|
|
305
|
+
.as_mut()
|
|
306
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
307
|
+
let ws = wb
|
|
308
|
+
.worksheet_from_index(sheet_idx)
|
|
309
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
310
|
+
f(ws)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
pub fn add_format(ruby: &Ruby, rb_self: &Self, opts: RHash) -> Result<usize, Error> {
|
|
314
|
+
let fmt = build_format(ruby, opts)?;
|
|
315
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
316
|
+
let id = st.formats.len();
|
|
317
|
+
st.formats.push(fmt);
|
|
318
|
+
Ok(id)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
pub fn add_sheet(
|
|
322
|
+
ruby: &Ruby,
|
|
323
|
+
rb_self: &Self,
|
|
324
|
+
name: String,
|
|
325
|
+
constant_memory: bool,
|
|
326
|
+
) -> Result<usize, Error> {
|
|
327
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
328
|
+
let wb = st
|
|
329
|
+
.workbook
|
|
330
|
+
.as_mut()
|
|
331
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
332
|
+
let idx = wb.worksheets().len();
|
|
333
|
+
let ws = if constant_memory {
|
|
334
|
+
wb.add_worksheet_with_constant_memory()
|
|
335
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?
|
|
336
|
+
} else {
|
|
337
|
+
wb.add_worksheet()
|
|
338
|
+
};
|
|
339
|
+
ws.set_name(&name)
|
|
340
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
341
|
+
Ok(idx)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
pub fn write_row(
|
|
345
|
+
ruby: &Ruby,
|
|
346
|
+
rb_self: &Self,
|
|
347
|
+
sheet_idx: usize,
|
|
348
|
+
row_idx: u32,
|
|
349
|
+
values: RArray,
|
|
350
|
+
format_id: Option<usize>,
|
|
351
|
+
col_format_ids: Option<RArray>,
|
|
352
|
+
) -> Result<(), Error> {
|
|
353
|
+
let col_ids = extract_col_fmt_ids(ruby, col_format_ids)?;
|
|
354
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
355
|
+
let WriterState {
|
|
356
|
+
workbook,
|
|
357
|
+
formats,
|
|
358
|
+
default_date_format,
|
|
359
|
+
default_datetime_format,
|
|
360
|
+
..
|
|
361
|
+
} = &mut *st;
|
|
362
|
+
let wb = workbook
|
|
363
|
+
.as_mut()
|
|
364
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
365
|
+
let row_fmt = match format_id {
|
|
366
|
+
Some(id) => Some(formats.get(id).ok_or_else(|| {
|
|
367
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
368
|
+
})?),
|
|
369
|
+
None => None,
|
|
370
|
+
};
|
|
371
|
+
let ws = wb
|
|
372
|
+
.worksheet_from_index(sheet_idx)
|
|
373
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
374
|
+
|
|
375
|
+
let mut ctx = WriteCtx {
|
|
376
|
+
ruby,
|
|
377
|
+
ws,
|
|
378
|
+
formats,
|
|
379
|
+
default_date_fmt: default_date_format,
|
|
380
|
+
default_datetime_fmt: default_datetime_format,
|
|
381
|
+
};
|
|
382
|
+
ctx.write_row(row_idx, values, row_fmt, &col_ids)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
pub fn write_rows(
|
|
386
|
+
ruby: &Ruby,
|
|
387
|
+
rb_self: &Self,
|
|
388
|
+
sheet_idx: usize,
|
|
389
|
+
start_row: u32,
|
|
390
|
+
rows: RArray,
|
|
391
|
+
format_id: Option<usize>,
|
|
392
|
+
col_format_ids: Option<RArray>,
|
|
393
|
+
) -> Result<usize, Error> {
|
|
394
|
+
let col_ids = extract_col_fmt_ids(ruby, col_format_ids)?;
|
|
395
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
396
|
+
let WriterState {
|
|
397
|
+
workbook,
|
|
398
|
+
formats,
|
|
399
|
+
default_date_format,
|
|
400
|
+
default_datetime_format,
|
|
401
|
+
..
|
|
402
|
+
} = &mut *st;
|
|
403
|
+
let wb = workbook
|
|
404
|
+
.as_mut()
|
|
405
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
406
|
+
let row_fmt = match format_id {
|
|
407
|
+
Some(id) => Some(formats.get(id).ok_or_else(|| {
|
|
408
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
409
|
+
})?),
|
|
410
|
+
None => None,
|
|
411
|
+
};
|
|
412
|
+
let ws = wb
|
|
413
|
+
.worksheet_from_index(sheet_idx)
|
|
414
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
415
|
+
|
|
416
|
+
let mut ctx = WriteCtx {
|
|
417
|
+
ruby,
|
|
418
|
+
ws,
|
|
419
|
+
formats,
|
|
420
|
+
default_date_fmt: default_date_format,
|
|
421
|
+
default_datetime_fmt: default_datetime_format,
|
|
422
|
+
};
|
|
423
|
+
let mut count = 0usize;
|
|
424
|
+
for row_val in rows.into_iter() {
|
|
425
|
+
let row_arr = RArray::from_value(row_val).ok_or_else(|| {
|
|
426
|
+
Error::new(ruby.exception_type_error(), "each row must be an Array")
|
|
427
|
+
})?;
|
|
428
|
+
ctx.write_row(start_row + count as u32, row_arr, row_fmt, &col_ids)?;
|
|
429
|
+
count += 1;
|
|
430
|
+
}
|
|
431
|
+
Ok(count)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
pub fn write_cell(
|
|
435
|
+
ruby: &Ruby,
|
|
436
|
+
rb_self: &Self,
|
|
437
|
+
sheet_idx: usize,
|
|
438
|
+
row_idx: u32,
|
|
439
|
+
col_idx: u16,
|
|
440
|
+
val: Value,
|
|
441
|
+
format_id: Option<usize>,
|
|
442
|
+
) -> Result<(), Error> {
|
|
443
|
+
if col_idx as usize > EXCEL_MAX_COL {
|
|
444
|
+
return Err(Error::new(
|
|
445
|
+
ruby.exception_arg_error(),
|
|
446
|
+
format!("column index {col_idx} exceeds Excel maximum ({EXCEL_MAX_COL})"),
|
|
447
|
+
));
|
|
448
|
+
}
|
|
449
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
450
|
+
let WriterState {
|
|
451
|
+
workbook,
|
|
452
|
+
formats,
|
|
453
|
+
default_date_format,
|
|
454
|
+
default_datetime_format,
|
|
455
|
+
..
|
|
456
|
+
} = &mut *st;
|
|
457
|
+
let wb = workbook
|
|
458
|
+
.as_mut()
|
|
459
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
460
|
+
let fmt = match format_id {
|
|
461
|
+
Some(id) => Some(formats.get(id).ok_or_else(|| {
|
|
462
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
463
|
+
})?),
|
|
464
|
+
None => None,
|
|
465
|
+
};
|
|
466
|
+
let ws = wb
|
|
467
|
+
.worksheet_from_index(sheet_idx)
|
|
468
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
469
|
+
|
|
470
|
+
let mut ctx = WriteCtx {
|
|
471
|
+
ruby,
|
|
472
|
+
ws,
|
|
473
|
+
formats,
|
|
474
|
+
default_date_fmt: default_date_format,
|
|
475
|
+
default_datetime_fmt: default_datetime_format,
|
|
476
|
+
};
|
|
477
|
+
ctx.write_cell(row_idx, col_idx, val, fmt)
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#[allow(clippy::too_many_arguments)]
|
|
481
|
+
pub fn merge_range(
|
|
482
|
+
ruby: &Ruby,
|
|
483
|
+
rb_self: &Self,
|
|
484
|
+
sheet_idx: usize,
|
|
485
|
+
first_row: u32,
|
|
486
|
+
first_col: u16,
|
|
487
|
+
last_row: u32,
|
|
488
|
+
last_col: u16,
|
|
489
|
+
text: String,
|
|
490
|
+
format_id: Option<usize>,
|
|
491
|
+
) -> Result<(), Error> {
|
|
492
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
493
|
+
let WriterState {
|
|
494
|
+
workbook,
|
|
495
|
+
formats,
|
|
496
|
+
default_plain_format,
|
|
497
|
+
..
|
|
498
|
+
} = &mut *st;
|
|
499
|
+
let wb = workbook
|
|
500
|
+
.as_mut()
|
|
501
|
+
.ok_or_else(|| closed_err(ruby, "workbook already closed"))?;
|
|
502
|
+
let fmt = match format_id {
|
|
503
|
+
Some(id) => formats.get(id).ok_or_else(|| {
|
|
504
|
+
Error::new(ruby.exception_arg_error(), format!("invalid format_id: {id}"))
|
|
505
|
+
})?,
|
|
506
|
+
None => default_plain_format,
|
|
507
|
+
};
|
|
508
|
+
let ws = wb
|
|
509
|
+
.worksheet_from_index(sheet_idx)
|
|
510
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
511
|
+
ws.merge_range(first_row, first_col, last_row, last_col, &text, fmt)
|
|
512
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
513
|
+
Ok(())
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
pub fn autofilter(
|
|
517
|
+
ruby: &Ruby,
|
|
518
|
+
rb_self: &Self,
|
|
519
|
+
sheet_idx: usize,
|
|
520
|
+
first_row: u32,
|
|
521
|
+
first_col: u16,
|
|
522
|
+
last_row: u32,
|
|
523
|
+
last_col: u16,
|
|
524
|
+
) -> Result<(), Error> {
|
|
525
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
526
|
+
ws.autofilter(first_row, first_col, last_row, last_col)
|
|
527
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
528
|
+
Ok(())
|
|
529
|
+
})
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
pub fn set_column_width(
|
|
533
|
+
ruby: &Ruby,
|
|
534
|
+
rb_self: &Self,
|
|
535
|
+
sheet_idx: usize,
|
|
536
|
+
col: u16,
|
|
537
|
+
width: f64,
|
|
538
|
+
) -> Result<(), Error> {
|
|
539
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
540
|
+
ws.set_column_width(col, width)
|
|
541
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
542
|
+
Ok(())
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
pub fn set_row_height(
|
|
547
|
+
ruby: &Ruby,
|
|
548
|
+
rb_self: &Self,
|
|
549
|
+
sheet_idx: usize,
|
|
550
|
+
row: u32,
|
|
551
|
+
height: f64,
|
|
552
|
+
) -> Result<(), Error> {
|
|
553
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
554
|
+
ws.set_row_height(row, height)
|
|
555
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
556
|
+
Ok(())
|
|
557
|
+
})
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
pub fn set_freeze_panes(
|
|
561
|
+
ruby: &Ruby,
|
|
562
|
+
rb_self: &Self,
|
|
563
|
+
sheet_idx: usize,
|
|
564
|
+
row: u32,
|
|
565
|
+
col: u16,
|
|
566
|
+
) -> Result<(), Error> {
|
|
567
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
568
|
+
ws.set_freeze_panes(row, col)
|
|
569
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
570
|
+
Ok(())
|
|
571
|
+
})
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
pub fn autofit(ruby: &Ruby, rb_self: &Self, sheet_idx: usize) -> Result<(), Error> {
|
|
575
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
576
|
+
ws.autofit();
|
|
577
|
+
Ok(())
|
|
578
|
+
})
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
pub fn set_tab_color(
|
|
582
|
+
ruby: &Ruby,
|
|
583
|
+
rb_self: &Self,
|
|
584
|
+
sheet_idx: usize,
|
|
585
|
+
hex: String,
|
|
586
|
+
) -> Result<(), Error> {
|
|
587
|
+
let color = parse_color(ruby, &hex)?;
|
|
588
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
589
|
+
ws.set_tab_color(color);
|
|
590
|
+
Ok(())
|
|
591
|
+
})
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
pub fn set_screen_gridlines(
|
|
595
|
+
ruby: &Ruby,
|
|
596
|
+
rb_self: &Self,
|
|
597
|
+
sheet_idx: usize,
|
|
598
|
+
enable: bool,
|
|
599
|
+
) -> Result<(), Error> {
|
|
600
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
601
|
+
ws.set_screen_gridlines(enable);
|
|
602
|
+
Ok(())
|
|
603
|
+
})
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
pub fn protect(
|
|
607
|
+
ruby: &Ruby,
|
|
608
|
+
rb_self: &Self,
|
|
609
|
+
sheet_idx: usize,
|
|
610
|
+
password: Option<String>,
|
|
611
|
+
) -> Result<(), Error> {
|
|
612
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
613
|
+
match password {
|
|
614
|
+
Some(ref pw) => {
|
|
615
|
+
ws.protect_with_password(pw);
|
|
616
|
+
}
|
|
617
|
+
None => {
|
|
618
|
+
ws.protect();
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
Ok(())
|
|
622
|
+
})
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
pub fn insert_note(
|
|
626
|
+
ruby: &Ruby,
|
|
627
|
+
rb_self: &Self,
|
|
628
|
+
sheet_idx: usize,
|
|
629
|
+
row: u32,
|
|
630
|
+
col: u16,
|
|
631
|
+
text: String,
|
|
632
|
+
) -> Result<(), Error> {
|
|
633
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
634
|
+
let note = Note::new(&text);
|
|
635
|
+
ws.insert_note(row, col, ¬e)
|
|
636
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
637
|
+
Ok(())
|
|
638
|
+
})
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
#[allow(clippy::too_many_arguments)]
|
|
642
|
+
pub fn add_dropdown(
|
|
643
|
+
ruby: &Ruby,
|
|
644
|
+
rb_self: &Self,
|
|
645
|
+
sheet_idx: usize,
|
|
646
|
+
first_row: u32,
|
|
647
|
+
first_col: u16,
|
|
648
|
+
last_row: u32,
|
|
649
|
+
last_col: u16,
|
|
650
|
+
values: Vec<String>,
|
|
651
|
+
input_title: Option<String>,
|
|
652
|
+
input_message: Option<String>,
|
|
653
|
+
error_message: Option<String>,
|
|
654
|
+
) -> Result<(), Error> {
|
|
655
|
+
let mut dv = DataValidation::new()
|
|
656
|
+
.allow_list_strings(&values)
|
|
657
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
658
|
+
if let Some(ref t) = input_title {
|
|
659
|
+
dv = dv
|
|
660
|
+
.set_input_title(t)
|
|
661
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
662
|
+
}
|
|
663
|
+
if let Some(ref m) = input_message {
|
|
664
|
+
dv = dv
|
|
665
|
+
.set_input_message(m)
|
|
666
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
667
|
+
}
|
|
668
|
+
if let Some(ref e) = error_message {
|
|
669
|
+
dv = dv
|
|
670
|
+
.set_error_message(e)
|
|
671
|
+
.map_err(|err| Error::new(ruby.exception_arg_error(), err.to_string()))?;
|
|
672
|
+
}
|
|
673
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
674
|
+
ws.add_data_validation(first_row, first_col, last_row, last_col, &dv)
|
|
675
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
676
|
+
Ok(())
|
|
677
|
+
})
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
pub fn set_page_setup(
|
|
681
|
+
ruby: &Ruby,
|
|
682
|
+
rb_self: &Self,
|
|
683
|
+
sheet_idx: usize,
|
|
684
|
+
opts: RHash,
|
|
685
|
+
) -> Result<(), Error> {
|
|
686
|
+
rb_self.with_worksheet(ruby, sheet_idx, |ws| {
|
|
687
|
+
opts.foreach(|k: String, v: Value| {
|
|
688
|
+
if v.is_nil() {
|
|
689
|
+
return Ok(ForEach::Continue);
|
|
690
|
+
}
|
|
691
|
+
match k.as_str() {
|
|
692
|
+
"orientation" => {
|
|
693
|
+
let o: String = magnus::TryConvert::try_convert(v)?;
|
|
694
|
+
match o.as_str() {
|
|
695
|
+
"landscape" => {
|
|
696
|
+
ws.set_landscape();
|
|
697
|
+
}
|
|
698
|
+
"portrait" => {
|
|
699
|
+
ws.set_portrait();
|
|
700
|
+
}
|
|
701
|
+
other => {
|
|
702
|
+
return Err(Error::new(
|
|
703
|
+
ruby.exception_arg_error(),
|
|
704
|
+
format!("unknown orientation: {other:?} (expected :landscape or :portrait)"),
|
|
705
|
+
));
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
"paper_size" => {
|
|
710
|
+
let code: u8 = if let Some(i) = Integer::from_value(v) {
|
|
711
|
+
i.to_u8()?
|
|
712
|
+
} else {
|
|
713
|
+
let s: String = magnus::TryConvert::try_convert(v)?;
|
|
714
|
+
match s.as_str() {
|
|
715
|
+
"letter" => 1,
|
|
716
|
+
"legal" => 5,
|
|
717
|
+
"a3" => 8,
|
|
718
|
+
"a4" => 9,
|
|
719
|
+
"a5" => 11,
|
|
720
|
+
"b4" => 12,
|
|
721
|
+
"b5" => 13,
|
|
722
|
+
other => {
|
|
723
|
+
return Err(Error::new(
|
|
724
|
+
ruby.exception_arg_error(),
|
|
725
|
+
format!("unknown paper_size: {other:?}"),
|
|
726
|
+
));
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
ws.set_paper_size(code);
|
|
731
|
+
}
|
|
732
|
+
"fit_to_pages" => {
|
|
733
|
+
let arr = RArray::from_value(v).ok_or_else(|| {
|
|
734
|
+
Error::new(
|
|
735
|
+
ruby.exception_type_error(),
|
|
736
|
+
"fit_to_pages expects [width, height]",
|
|
737
|
+
)
|
|
738
|
+
})?;
|
|
739
|
+
let w: u16 = arr.entry(0)?;
|
|
740
|
+
let h: u16 = arr.entry(1)?;
|
|
741
|
+
ws.set_print_fit_to_pages(w, h);
|
|
742
|
+
}
|
|
743
|
+
"repeat_rows" => {
|
|
744
|
+
let arr = RArray::from_value(v).ok_or_else(|| {
|
|
745
|
+
Error::new(
|
|
746
|
+
ruby.exception_type_error(),
|
|
747
|
+
"repeat_rows expects [first, last]",
|
|
748
|
+
)
|
|
749
|
+
})?;
|
|
750
|
+
let first: u32 = arr.entry(0)?;
|
|
751
|
+
let last: u32 = arr.entry(1)?;
|
|
752
|
+
ws.set_repeat_rows(first, last)
|
|
753
|
+
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
754
|
+
}
|
|
755
|
+
"print_gridlines" => {
|
|
756
|
+
let b: bool = magnus::TryConvert::try_convert(v)?;
|
|
757
|
+
ws.set_print_gridlines(b);
|
|
758
|
+
}
|
|
759
|
+
"header" => {
|
|
760
|
+
let h: String = magnus::TryConvert::try_convert(v)?;
|
|
761
|
+
ws.set_header(&h);
|
|
762
|
+
}
|
|
763
|
+
"footer" => {
|
|
764
|
+
let f: String = magnus::TryConvert::try_convert(v)?;
|
|
765
|
+
ws.set_footer(&f);
|
|
766
|
+
}
|
|
767
|
+
other => {
|
|
768
|
+
return Err(Error::new(
|
|
769
|
+
ruby.exception_arg_error(),
|
|
770
|
+
format!("unknown page_setup option: {other:?}"),
|
|
771
|
+
));
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
Ok(ForEach::Continue)
|
|
775
|
+
})
|
|
776
|
+
})
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
pub fn save(ruby: &Ruby, rb_self: &Self, path: String) -> Result<(), Error> {
|
|
780
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
781
|
+
let mut wb = st
|
|
782
|
+
.workbook
|
|
783
|
+
.take()
|
|
784
|
+
.ok_or_else(|| closed_err(ruby, "workbook already saved"))?;
|
|
785
|
+
wb.save(&path)
|
|
786
|
+
.map_err(|e| file_err(ruby, e.to_string()))?;
|
|
787
|
+
Ok(())
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
pub fn to_bytes(ruby: &Ruby, rb_self: &Self) -> Result<RString, Error> {
|
|
791
|
+
let mut st = rb_self.borrow_state_mut(ruby)?;
|
|
792
|
+
let mut wb = st
|
|
793
|
+
.workbook
|
|
794
|
+
.take()
|
|
795
|
+
.ok_or_else(|| closed_err(ruby, "workbook already saved"))?;
|
|
796
|
+
let buf = wb
|
|
797
|
+
.save_to_buffer()
|
|
798
|
+
.map_err(|e| oxcel_err(ruby, e.to_string()))?;
|
|
799
|
+
Ok(ruby.str_from_slice(&buf))
|
|
800
|
+
}
|
|
801
|
+
}
|