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,524 @@
|
|
|
1
|
+
use std::cell::{Cell, RefCell};
|
|
2
|
+
use std::fs::File;
|
|
3
|
+
use std::io::{BufReader, Cursor, Read, Seek};
|
|
4
|
+
|
|
5
|
+
use calamine::{
|
|
6
|
+
open_workbook_auto, open_workbook_auto_from_rs, open_workbook_from_rs, Data, DataRef,
|
|
7
|
+
ExcelDateTime, Ods, Range, Reader, SheetType, SheetVisible, Sheets, Xls, Xlsb, Xlsx,
|
|
8
|
+
};
|
|
9
|
+
use chrono::{Datelike, Timelike};
|
|
10
|
+
use magnus::{prelude::*, Error, RArray, RHash, RString, Ruby, Value};
|
|
11
|
+
|
|
12
|
+
use crate::error::{closed_err, file_err, oxcel_err};
|
|
13
|
+
|
|
14
|
+
enum WorkbookSource {
|
|
15
|
+
File(Sheets<BufReader<File>>),
|
|
16
|
+
Memory(Sheets<Cursor<Vec<u8>>>),
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl WorkbookSource {
|
|
20
|
+
fn sheet_names(&self) -> Vec<String> {
|
|
21
|
+
match self {
|
|
22
|
+
WorkbookSource::File(s) => s.sheet_names().to_vec(),
|
|
23
|
+
WorkbookSource::Memory(s) => s.sheet_names().to_vec(),
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn sheets_metadata(&self) -> &[calamine::Sheet] {
|
|
28
|
+
match self {
|
|
29
|
+
WorkbookSource::File(s) => s.sheets_metadata(),
|
|
30
|
+
WorkbookSource::Memory(s) => s.sheets_metadata(),
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fn worksheet_range(&mut self, name: &str) -> Result<Range<Data>, calamine::Error> {
|
|
35
|
+
match self {
|
|
36
|
+
WorkbookSource::File(s) => s.worksheet_range(name),
|
|
37
|
+
WorkbookSource::Memory(s) => s.worksheet_range(name),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn worksheet_formula(&mut self, name: &str) -> Result<Range<String>, calamine::Error> {
|
|
42
|
+
match self {
|
|
43
|
+
WorkbookSource::File(s) => s.worksheet_formula(name),
|
|
44
|
+
WorkbookSource::Memory(s) => s.worksheet_formula(name),
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#[magnus::wrap(class = "Oxcel::NativeReader", free_immediately, size)]
|
|
50
|
+
pub struct NativeReader {
|
|
51
|
+
cached_sheet_names: Vec<String>,
|
|
52
|
+
closed: Cell<bool>,
|
|
53
|
+
source: RefCell<Option<WorkbookSource>>,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fn excel_datetime_to_ruby(ruby: &Ruby, dt: &ExcelDateTime) -> Result<Value, Error> {
|
|
57
|
+
if let Some(ndt) = dt.as_datetime() {
|
|
58
|
+
let is_pure_date = dt.as_f64().fract() == 0.0
|
|
59
|
+
&& ndt.hour() == 0
|
|
60
|
+
&& ndt.minute() == 0
|
|
61
|
+
&& ndt.second() == 0
|
|
62
|
+
&& ndt.nanosecond() == 0;
|
|
63
|
+
|
|
64
|
+
if is_pure_date {
|
|
65
|
+
if let Ok(date_cls) = ruby.class_object().const_get::<_, Value>("Date") {
|
|
66
|
+
return date_cls.funcall("new", (ndt.year(), ndt.month(), ndt.day()));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
let time_cls = ruby.class_time();
|
|
70
|
+
time_cls.funcall(
|
|
71
|
+
"utc",
|
|
72
|
+
(
|
|
73
|
+
ndt.year(),
|
|
74
|
+
ndt.month(),
|
|
75
|
+
ndt.day(),
|
|
76
|
+
ndt.hour(),
|
|
77
|
+
ndt.minute(),
|
|
78
|
+
ndt.second(),
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
} else {
|
|
82
|
+
Ok(ruby.float_from_f64(dt.as_f64()).as_value())
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fn float_to_ruby(ruby: &Ruby, f: f64, coerce_integers: bool) -> Value {
|
|
87
|
+
if coerce_integers
|
|
88
|
+
&& f.is_finite()
|
|
89
|
+
&& f.fract() == 0.0
|
|
90
|
+
&& f >= (i64::MIN as f64)
|
|
91
|
+
&& f <= (i64::MAX as f64)
|
|
92
|
+
{
|
|
93
|
+
ruby.integer_from_i64(f as i64).as_value()
|
|
94
|
+
} else {
|
|
95
|
+
ruby.float_from_f64(f).as_value()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn data_to_ruby(ruby: &Ruby, cell: &Data, coerce_integers: bool) -> Result<Value, Error> {
|
|
100
|
+
match cell {
|
|
101
|
+
Data::Empty => Ok(ruby.qnil().as_value()),
|
|
102
|
+
Data::String(s) => Ok(ruby.str_new(s).as_value()),
|
|
103
|
+
Data::Int(i) => Ok(ruby.integer_from_i64(*i).as_value()),
|
|
104
|
+
Data::Float(f) => Ok(float_to_ruby(ruby, *f, coerce_integers)),
|
|
105
|
+
Data::Bool(b) => Ok(if *b {
|
|
106
|
+
ruby.qtrue().as_value()
|
|
107
|
+
} else {
|
|
108
|
+
ruby.qfalse().as_value()
|
|
109
|
+
}),
|
|
110
|
+
Data::DateTime(dt) => excel_datetime_to_ruby(ruby, dt),
|
|
111
|
+
Data::DateTimeIso(s) | Data::DurationIso(s) => Ok(ruby.str_new(s).as_value()),
|
|
112
|
+
Data::Error(e) => Ok(ruby.str_new(&format!("{e:?}")).as_value()),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn data_ref_to_ruby(ruby: &Ruby, cell: &DataRef<'_>, coerce_integers: bool) -> Result<Value, Error> {
|
|
117
|
+
match cell {
|
|
118
|
+
DataRef::Empty => Ok(ruby.qnil().as_value()),
|
|
119
|
+
DataRef::String(s) => Ok(ruby.str_new(s).as_value()),
|
|
120
|
+
DataRef::SharedString(s) => Ok(ruby.str_new(s).as_value()),
|
|
121
|
+
DataRef::Int(i) => Ok(ruby.integer_from_i64(*i).as_value()),
|
|
122
|
+
DataRef::Float(f) => Ok(float_to_ruby(ruby, *f, coerce_integers)),
|
|
123
|
+
DataRef::Bool(b) => Ok(if *b {
|
|
124
|
+
ruby.qtrue().as_value()
|
|
125
|
+
} else {
|
|
126
|
+
ruby.qfalse().as_value()
|
|
127
|
+
}),
|
|
128
|
+
DataRef::DateTime(dt) => excel_datetime_to_ruby(ruby, dt),
|
|
129
|
+
DataRef::DateTimeIso(s) | DataRef::DurationIso(s) => Ok(ruby.str_new(s).as_value()),
|
|
130
|
+
DataRef::Error(e) => Ok(ruby.str_new(&format!("{e:?}")).as_value()),
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// True O(1) memory, GC-safe streaming row iterator for `.xlsx` using calamine's XlsxCellReader.
|
|
135
|
+
/// All Ruby Values are stored directly into GC-managed `RArray` instances (never in an unmarked
|
|
136
|
+
/// Rust heap `Vec<Value>`), ensuring safety under Ruby GC compaction and `GC.stress = true`.
|
|
137
|
+
fn stream_xlsx_rows<RS: Read + Seek>(
|
|
138
|
+
ruby: &Ruby,
|
|
139
|
+
xlsx: &mut Xlsx<RS>,
|
|
140
|
+
name: &str,
|
|
141
|
+
coerce_integers: bool,
|
|
142
|
+
skip_empty: bool,
|
|
143
|
+
) -> Result<(), Error> {
|
|
144
|
+
let mut reader = xlsx
|
|
145
|
+
.worksheet_cells_reader(name)
|
|
146
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed to stream sheet {name:?}: {e}")))?;
|
|
147
|
+
|
|
148
|
+
let dims = reader.dimensions();
|
|
149
|
+
let start_col = dims.start.1;
|
|
150
|
+
let mut max_cols = if dims.end.1 >= dims.start.1 {
|
|
151
|
+
(dims.end.1 - dims.start.1 + 1) as usize
|
|
152
|
+
} else {
|
|
153
|
+
0
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
let mut current_row_idx: Option<u32> = None;
|
|
157
|
+
let mut current_row: Option<RArray> = None;
|
|
158
|
+
let mut current_row_has_non_nil = false;
|
|
159
|
+
|
|
160
|
+
while let Some(cell) = reader
|
|
161
|
+
.next_cell()
|
|
162
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed reading cell in sheet {name:?}: {e}")))?
|
|
163
|
+
{
|
|
164
|
+
let (r, c) = cell.get_position();
|
|
165
|
+
let val = data_ref_to_ruby(ruby, cell.get_value(), coerce_integers)?;
|
|
166
|
+
|
|
167
|
+
match current_row_idx {
|
|
168
|
+
None => {
|
|
169
|
+
current_row_idx = Some(r);
|
|
170
|
+
current_row = Some(ruby.ary_new_capa(max_cols));
|
|
171
|
+
current_row_has_non_nil = false;
|
|
172
|
+
}
|
|
173
|
+
Some(prev_r) if r != prev_r => {
|
|
174
|
+
if let Some(rb_row) = current_row.take() {
|
|
175
|
+
if !skip_empty || current_row_has_non_nil {
|
|
176
|
+
let _: Value = ruby.yield_value(rb_row)?;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if !skip_empty && r > prev_r + 1 {
|
|
181
|
+
for _ in (prev_r + 1)..r {
|
|
182
|
+
let blank = ruby.ary_new_capa(max_cols);
|
|
183
|
+
for _ in 0..max_cols {
|
|
184
|
+
blank.push(ruby.qnil())?;
|
|
185
|
+
}
|
|
186
|
+
let _: Value = ruby.yield_value(blank)?;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
current_row_idx = Some(r);
|
|
191
|
+
current_row = Some(ruby.ary_new_capa(max_cols));
|
|
192
|
+
current_row_has_non_nil = false;
|
|
193
|
+
}
|
|
194
|
+
_ => {}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let rel_col = c.saturating_sub(start_col) as usize;
|
|
198
|
+
if rel_col + 1 > max_cols {
|
|
199
|
+
max_cols = rel_col + 1;
|
|
200
|
+
}
|
|
201
|
+
if !val.is_nil() {
|
|
202
|
+
current_row_has_non_nil = true;
|
|
203
|
+
}
|
|
204
|
+
if let Some(ref rb_row) = current_row {
|
|
205
|
+
rb_row.store(rel_col as isize, val)?;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if let Some(rb_row) = current_row.take() {
|
|
210
|
+
if !skip_empty || current_row_has_non_nil {
|
|
211
|
+
let _: Value = ruby.yield_value(rb_row)?;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
Ok(())
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
fn open_typed_rs<RS: Read + Seek>(
|
|
219
|
+
ruby: &Ruby,
|
|
220
|
+
data: RS,
|
|
221
|
+
fmt: &str,
|
|
222
|
+
) -> Result<Sheets<RS>, Error> {
|
|
223
|
+
let result = match fmt {
|
|
224
|
+
"xlsx" | "xlsm" => open_workbook_from_rs::<Xlsx<RS>, RS>(data)
|
|
225
|
+
.map(Sheets::Xlsx)
|
|
226
|
+
.map_err(calamine::Error::Xlsx),
|
|
227
|
+
"xls" => open_workbook_from_rs::<Xls<RS>, RS>(data)
|
|
228
|
+
.map(Sheets::Xls)
|
|
229
|
+
.map_err(calamine::Error::Xls),
|
|
230
|
+
"xlsb" => open_workbook_from_rs::<Xlsb<RS>, RS>(data)
|
|
231
|
+
.map(Sheets::Xlsb)
|
|
232
|
+
.map_err(calamine::Error::Xlsb),
|
|
233
|
+
"ods" => open_workbook_from_rs::<Ods<RS>, RS>(data)
|
|
234
|
+
.map(Sheets::Ods)
|
|
235
|
+
.map_err(calamine::Error::Ods),
|
|
236
|
+
other => {
|
|
237
|
+
return Err(Error::new(
|
|
238
|
+
ruby.exception_arg_error(),
|
|
239
|
+
format!("unsupported format: {other:?} (expected :xlsx, :xlsm, :xlsb, :xls, :ods)"),
|
|
240
|
+
));
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
result.map_err(|e| file_err(ruby, format!("Failed to open workbook: {e}")))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
impl NativeReader {
|
|
247
|
+
fn from_source(source: WorkbookSource) -> Self {
|
|
248
|
+
let cached_sheet_names = source.sheet_names();
|
|
249
|
+
Self {
|
|
250
|
+
cached_sheet_names,
|
|
251
|
+
closed: Cell::new(false),
|
|
252
|
+
source: RefCell::new(Some(source)),
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
fn with_source<T, F>(&self, ruby: &Ruby, busy_msg: &str, f: F) -> Result<T, Error>
|
|
257
|
+
where
|
|
258
|
+
F: FnOnce(&WorkbookSource) -> Result<T, Error>,
|
|
259
|
+
{
|
|
260
|
+
if self.closed.get() {
|
|
261
|
+
return Err(closed_err(ruby, "reader already closed"));
|
|
262
|
+
}
|
|
263
|
+
let guard = self
|
|
264
|
+
.source
|
|
265
|
+
.try_borrow()
|
|
266
|
+
.map_err(|_| oxcel_err(ruby, busy_msg))?;
|
|
267
|
+
let src = guard
|
|
268
|
+
.as_ref()
|
|
269
|
+
.ok_or_else(|| closed_err(ruby, "reader already closed"))?;
|
|
270
|
+
f(src)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
fn with_source_mut<T, F>(&self, ruby: &Ruby, f: F) -> Result<T, Error>
|
|
274
|
+
where
|
|
275
|
+
F: FnOnce(&mut WorkbookSource) -> Result<T, Error>,
|
|
276
|
+
{
|
|
277
|
+
if self.closed.get() {
|
|
278
|
+
return Err(closed_err(ruby, "reader already closed"));
|
|
279
|
+
}
|
|
280
|
+
let mut guard = self.source.try_borrow_mut().map_err(|_| {
|
|
281
|
+
oxcel_err(
|
|
282
|
+
ruby,
|
|
283
|
+
"reader is currently streaming; open a separate Oxcel::Reader for nested reads",
|
|
284
|
+
)
|
|
285
|
+
})?;
|
|
286
|
+
let src = guard
|
|
287
|
+
.as_mut()
|
|
288
|
+
.ok_or_else(|| closed_err(ruby, "reader already closed"))?;
|
|
289
|
+
f(src)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
pub fn open(ruby: &Ruby, path: String, format: Option<String>) -> Result<Self, Error> {
|
|
293
|
+
// Disk-backed O(1) memory path when explicit format is provided (no whole-file RAM copy!)
|
|
294
|
+
if let Some(fmt) = format {
|
|
295
|
+
let file = File::open(&path)
|
|
296
|
+
.map_err(|e| file_err(ruby, format!("Failed to open file {path:?}: {e}")))?;
|
|
297
|
+
let sheets = open_typed_rs(ruby, BufReader::new(file), &fmt)?;
|
|
298
|
+
return Ok(Self::from_source(WorkbookSource::File(sheets)));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Try extension-based disk-backed open first; fall back to magic-byte inspection for extensionless Tempfiles.
|
|
302
|
+
match open_workbook_auto(&path) {
|
|
303
|
+
Ok(sheets) => Ok(Self::from_source(WorkbookSource::File(sheets))),
|
|
304
|
+
Err(first_err) => {
|
|
305
|
+
if let Ok(mut file) = File::open(&path) {
|
|
306
|
+
let mut magic = [0u8; 8];
|
|
307
|
+
if file.read(&mut magic).is_ok() {
|
|
308
|
+
let guessed = if magic.starts_with(b"PK\x03\x04") {
|
|
309
|
+
Some("xlsx")
|
|
310
|
+
} else if magic.starts_with(&[0xD0, 0xCF, 0x11, 0xE0]) {
|
|
311
|
+
Some("xls")
|
|
312
|
+
} else {
|
|
313
|
+
None
|
|
314
|
+
};
|
|
315
|
+
if let Some(fmt) = guessed {
|
|
316
|
+
if let Ok(f2) = File::open(&path) {
|
|
317
|
+
if let Ok(sheets) = open_typed_rs(ruby, BufReader::new(f2), fmt) {
|
|
318
|
+
return Ok(Self::from_source(WorkbookSource::File(sheets)));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
let mut bytes = Vec::new();
|
|
323
|
+
if let Ok(mut f3) = File::open(&path) {
|
|
324
|
+
if f3.read_to_end(&mut bytes).is_ok() {
|
|
325
|
+
if let Ok(sheets) = open_workbook_auto_from_rs(Cursor::new(bytes)) {
|
|
326
|
+
return Ok(Self::from_source(WorkbookSource::Memory(sheets)));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
Err(file_err(
|
|
333
|
+
ruby,
|
|
334
|
+
format!("Failed to open workbook {path:?}: {first_err}"),
|
|
335
|
+
))
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
pub fn open_bytes(ruby: &Ruby, bytes: RString, format: Option<String>) -> Result<Self, Error> {
|
|
341
|
+
let data = unsafe { bytes.as_slice().to_vec() };
|
|
342
|
+
let sheets = match format {
|
|
343
|
+
Some(fmt) => open_typed_rs(ruby, Cursor::new(data), &fmt)?,
|
|
344
|
+
None => open_workbook_auto_from_rs(Cursor::new(data))
|
|
345
|
+
.map_err(|e| file_err(ruby, format!("Failed to open workbook from bytes: {e}")))?,
|
|
346
|
+
};
|
|
347
|
+
Ok(Self::from_source(WorkbookSource::Memory(sheets)))
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
pub fn close(&self) {
|
|
351
|
+
self.closed.set(true);
|
|
352
|
+
if let Ok(mut guard) = self.source.try_borrow_mut() {
|
|
353
|
+
guard.take();
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
pub fn is_closed(&self) -> bool {
|
|
358
|
+
self.closed.get()
|
|
359
|
+
|| self
|
|
360
|
+
.source
|
|
361
|
+
.try_borrow()
|
|
362
|
+
.map(|guard| guard.is_none())
|
|
363
|
+
.unwrap_or(false)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
pub fn sheet_names(&self) -> Vec<String> {
|
|
367
|
+
self.cached_sheet_names.clone()
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
pub fn sheets_metadata(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
371
|
+
rb_self.with_source(
|
|
372
|
+
ruby,
|
|
373
|
+
"reader is currently streaming; cannot call sheets_metadata re-entrantly",
|
|
374
|
+
|src| {
|
|
375
|
+
let meta = src.sheets_metadata();
|
|
376
|
+
let out = ruby.ary_new_capa(meta.len());
|
|
377
|
+
for s in meta {
|
|
378
|
+
let h = ruby.hash_new();
|
|
379
|
+
h.aset(ruby.to_symbol("name"), s.name.as_str())?;
|
|
380
|
+
let vis = match s.visible {
|
|
381
|
+
SheetVisible::Visible => "visible",
|
|
382
|
+
SheetVisible::Hidden => "hidden",
|
|
383
|
+
SheetVisible::VeryHidden => "very_hidden",
|
|
384
|
+
};
|
|
385
|
+
h.aset(ruby.to_symbol("visibility"), ruby.to_symbol(vis))?;
|
|
386
|
+
let typ = match s.typ {
|
|
387
|
+
SheetType::WorkSheet => "worksheet",
|
|
388
|
+
SheetType::DialogSheet => "dialog_sheet",
|
|
389
|
+
SheetType::MacroSheet => "macro_sheet",
|
|
390
|
+
SheetType::ChartSheet => "chart_sheet",
|
|
391
|
+
SheetType::Vba => "vba",
|
|
392
|
+
};
|
|
393
|
+
h.aset(ruby.to_symbol("type"), ruby.to_symbol(typ))?;
|
|
394
|
+
out.push(h)?;
|
|
395
|
+
}
|
|
396
|
+
Ok(out)
|
|
397
|
+
},
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
pub fn dimensions(ruby: &Ruby, rb_self: &Self, name: String) -> Result<(usize, usize), Error> {
|
|
402
|
+
rb_self.with_source_mut(ruby, |src| {
|
|
403
|
+
let range = src
|
|
404
|
+
.worksheet_range(&name)
|
|
405
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed to read sheet {name:?}: {e}")))?;
|
|
406
|
+
Ok((range.height(), range.width()))
|
|
407
|
+
})
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
pub fn merged_regions(ruby: &Ruby, rb_self: &Self, name: String) -> Result<RArray, Error> {
|
|
411
|
+
rb_self.with_source_mut(ruby, |src| {
|
|
412
|
+
let dims = match src {
|
|
413
|
+
WorkbookSource::File(Sheets::Xlsx(x)) => x
|
|
414
|
+
.merge_cells_by_sheet_name(&name)
|
|
415
|
+
.map_err(|e| {
|
|
416
|
+
oxcel_err(ruby, format!("Failed to read merged cells in {name:?}: {e}"))
|
|
417
|
+
})?,
|
|
418
|
+
WorkbookSource::Memory(Sheets::Xlsx(x)) => x
|
|
419
|
+
.merge_cells_by_sheet_name(&name)
|
|
420
|
+
.map_err(|e| {
|
|
421
|
+
oxcel_err(ruby, format!("Failed to read merged cells in {name:?}: {e}"))
|
|
422
|
+
})?,
|
|
423
|
+
_ => Vec::new(),
|
|
424
|
+
};
|
|
425
|
+
let out = ruby.ary_new_capa(dims.len());
|
|
426
|
+
for d in dims {
|
|
427
|
+
let tuple = ruby.ary_new_capa(4);
|
|
428
|
+
tuple.push(d.start.0)?;
|
|
429
|
+
tuple.push(d.start.1)?;
|
|
430
|
+
tuple.push(d.end.0)?;
|
|
431
|
+
tuple.push(d.end.1)?;
|
|
432
|
+
out.push(tuple)?;
|
|
433
|
+
}
|
|
434
|
+
Ok(out)
|
|
435
|
+
})
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
pub fn formulas(ruby: &Ruby, rb_self: &Self, name: String) -> Result<RHash, Error> {
|
|
439
|
+
rb_self.with_source_mut(ruby, |src| {
|
|
440
|
+
let range = src
|
|
441
|
+
.worksheet_formula(&name)
|
|
442
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed to read formulas in {name:?}: {e}")))?;
|
|
443
|
+
let (start_r, start_c) = range.start().unwrap_or((0, 0));
|
|
444
|
+
let out = ruby.hash_new();
|
|
445
|
+
for (r, c, formula) in range.used_cells() {
|
|
446
|
+
if !formula.is_empty() {
|
|
447
|
+
let coord = ruby.ary_new_capa(2);
|
|
448
|
+
coord.push(start_r as usize + r)?;
|
|
449
|
+
coord.push(start_c as usize + c)?;
|
|
450
|
+
out.aset(coord, ruby.str_new(formula))?;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
Ok(out)
|
|
454
|
+
})
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
pub fn read_sheet(
|
|
458
|
+
ruby: &Ruby,
|
|
459
|
+
rb_self: &Self,
|
|
460
|
+
name: String,
|
|
461
|
+
coerce_integers: bool,
|
|
462
|
+
) -> Result<RArray, Error> {
|
|
463
|
+
rb_self.with_source_mut(ruby, |src| {
|
|
464
|
+
let range = src
|
|
465
|
+
.worksheet_range(&name)
|
|
466
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed to read sheet {name:?}: {e}")))?;
|
|
467
|
+
|
|
468
|
+
let rows = ruby.ary_new_capa(range.height());
|
|
469
|
+
for row in range.rows() {
|
|
470
|
+
let rb_row = ruby.ary_new_capa(row.len());
|
|
471
|
+
for cell in row {
|
|
472
|
+
rb_row.push(data_to_ruby(ruby, cell, coerce_integers)?)?;
|
|
473
|
+
}
|
|
474
|
+
rows.push(rb_row)?;
|
|
475
|
+
}
|
|
476
|
+
Ok(rows)
|
|
477
|
+
})
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
pub fn each_row(
|
|
481
|
+
ruby: &Ruby,
|
|
482
|
+
rb_self: &Self,
|
|
483
|
+
name: String,
|
|
484
|
+
coerce_integers: bool,
|
|
485
|
+
skip_empty: bool,
|
|
486
|
+
) -> Result<Value, Error> {
|
|
487
|
+
if !ruby.block_given() {
|
|
488
|
+
return Err(Error::new(
|
|
489
|
+
ruby.exception_arg_error(),
|
|
490
|
+
"block is required for each_row",
|
|
491
|
+
));
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
rb_self.with_source_mut(ruby, |src| {
|
|
495
|
+
match src {
|
|
496
|
+
WorkbookSource::File(Sheets::Xlsx(xlsx)) => {
|
|
497
|
+
stream_xlsx_rows(ruby, xlsx, &name, coerce_integers, skip_empty)?;
|
|
498
|
+
return Ok(ruby.qnil().as_value());
|
|
499
|
+
}
|
|
500
|
+
WorkbookSource::Memory(Sheets::Xlsx(xlsx)) => {
|
|
501
|
+
stream_xlsx_rows(ruby, xlsx, &name, coerce_integers, skip_empty)?;
|
|
502
|
+
return Ok(ruby.qnil().as_value());
|
|
503
|
+
}
|
|
504
|
+
_ => {}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
let range = src
|
|
508
|
+
.worksheet_range(&name)
|
|
509
|
+
.map_err(|e| oxcel_err(ruby, format!("Failed to read sheet {name:?}: {e}")))?;
|
|
510
|
+
|
|
511
|
+
for row in range.rows() {
|
|
512
|
+
if skip_empty && row.iter().all(|c| matches!(c, Data::Empty)) {
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
let rb_row = ruby.ary_new_capa(row.len());
|
|
516
|
+
for cell in row {
|
|
517
|
+
rb_row.push(data_to_ruby(ruby, cell, coerce_integers)?)?;
|
|
518
|
+
}
|
|
519
|
+
let _: Value = ruby.yield_value(rb_row)?;
|
|
520
|
+
}
|
|
521
|
+
Ok(ruby.qnil().as_value())
|
|
522
|
+
})
|
|
523
|
+
}
|
|
524
|
+
}
|