xls-rs 0.1.2

A powerful CLI tool and library for spreadsheet manipulation with pandas-style operations. Supports CSV, Excel (XLSX, XLS, ODS), Parquet, and Avro formats with formula evaluation, data transformation, and comprehensive analytics capabilities.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use xls_rs::{CellRange, Converter, CsvHandler, ExcelHandler, FormulaEvaluator};
use std::fs;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};

static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);

fn unique_path(prefix: &str, ext: &str) -> String {
    let id = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    format!("{prefix}_{id}.{ext}")
}

#[test]
fn test_csv_read_write() {
    let handler = CsvHandler::new();
    let test_data = "1,2,3\n4,5,6\n7,8,9\n";

    // Write test CSV
    let input_path = "test_input.csv";
    fs::write(input_path, test_data).unwrap();

    // Read it back
    let output_path = "test_output.csv";
    handler.write_from_csv(input_path, output_path).unwrap();

    let content = fs::read_to_string(output_path).unwrap();
    assert_eq!(content, test_data);

    // Cleanup
    fs::remove_file(input_path).ok();
    fs::remove_file(output_path).ok();
}

#[test]
fn test_csv_formula() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "1,2\n3,4\n5,6\n";

    let input_path = "test_formula_input.csv";
    fs::write(input_path, test_data).unwrap();

    let output_path = "test_formula_output.csv";
    evaluator
        .apply_to_csv(input_path, output_path, "A1+B1", "C1")
        .unwrap();

    let content = fs::read_to_string(output_path).unwrap();
    // Check that the formula result (3.0) is in the output
    assert!(content.contains("3"));

    // Cleanup
    fs::remove_file(input_path).ok();
    fs::remove_file(output_path).ok();
}

#[test]
fn test_csv_to_excel_conversion() {
    let converter = Converter::new();
    let test_data = "1,2,3\n4,5,6\n";

    let csv_path = "test_convert_input.csv";
    fs::write(csv_path, test_data).unwrap();

    let excel_path = "test_convert_output.xlsx";
    converter.convert(csv_path, excel_path, None).unwrap();

    // Verify Excel file exists
    assert!(Path::new(excel_path).exists());

    // Read back and verify
    let handler = ExcelHandler::new();
    let content = handler.read_with_sheet(excel_path, None).unwrap();
    // Excel output format: numbers are converted to strings
    // Verify that we got some content (at least one row)
    assert!(!content.trim().is_empty(), "Excel file should contain data");
    // Check that numeric values are present (may be in any row)
    assert!(
        content.contains("4") || content.contains("5") || content.contains("6"),
        "Excel content should contain numeric values"
    );

    // Cleanup
    fs::remove_file(csv_path).ok();
    fs::remove_file(excel_path).ok();
}

#[test]
fn test_formula_sum() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "1,2\n3,4\n5,6\n";

    let input_path = unique_path("test_sum_input", "csv");
    let output_path = unique_path("test_sum_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "SUM(A1:A3)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // SUM(1+3+5) = 9
    assert!(
        content.contains("9"),
        "SUM result should be 9, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_average() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "10,20\n30,40\n50,60\n";

    let input_path = unique_path("test_avg_input", "csv");
    let output_path = unique_path("test_avg_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "AVERAGE(A1:A3)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // AVERAGE(10+30+50)/3 = 30
    assert!(
        content.contains("30"),
        "AVERAGE result should be 30, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_min() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "5,2\n3,8\n1,4\n";

    let input_path = unique_path("test_min_input", "csv");
    let output_path = unique_path("test_min_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "MIN(A1:B3)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // MIN of all values = 1
    assert!(
        content.contains("1"),
        "MIN result should be 1, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_max() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "5,2\n3,8\n1,4\n";

    let input_path = unique_path("test_max_input", "csv");
    let output_path = unique_path("test_max_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "MAX(A1:B3)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // MAX of all values = 8
    assert!(
        content.contains("8"),
        "MAX result should be 8, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_count() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "1,2\n3,4\n5,6\n";

    let input_path = unique_path("test_count_input", "csv");
    let output_path = unique_path("test_count_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "COUNT(A1:B3)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // COUNT of 6 cells = 6
    assert!(
        content.contains("6"),
        "COUNT result should be 6, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_arithmetic_multiply() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "3,4\n5,6\n";

    let input_path = unique_path("test_mult_input", "csv");
    let output_path = unique_path("test_mult_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "A1*B1", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // 3*4 = 12
    assert!(
        content.contains("12"),
        "Multiply result should be 12, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_arithmetic_divide() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "20,4\n5,6\n";

    let input_path = unique_path("test_div_input", "csv");
    let output_path = unique_path("test_div_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "A1/B1", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    // 20/4 = 5
    assert!(
        content.contains("5"),
        "Divide result should be 5, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_if_true() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "10,5\n3,4\n";

    let input_path = unique_path("test_if_true_input", "csv");
    let output_path = unique_path("test_if_true_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // IF(A1>B1, 100, 0) -> A1(10) > B1(5) is true, so result is 100
    evaluator
        .apply_to_csv(&input_path, &output_path, "IF(A1>B1, 100, 0)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("100"),
        "IF true branch should be 100, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_if_false() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "3,5\n1,2\n";

    let input_path = unique_path("test_if_false_input", "csv");
    let output_path = unique_path("test_if_false_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // IF(A1>B1, 100, 0) -> A1(3) > B1(5) is false, so result is 0
    evaluator
        .apply_to_csv(&input_path, &output_path, "IF(A1>B1, 100, 0)", "C1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains(",0") || content.starts_with("0"),
        "IF false branch should be 0, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_concat() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "Hello,World\n1,2\n";

    let input_path = unique_path("test_concat_input", "csv");
    let output_path = unique_path("test_concat_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // CONCAT("Result: ", A2, B2) -> "Result: 12"
    evaluator
        .apply_to_csv(
            &input_path,
            &output_path,
            "CONCAT(\"Result: \", A2, B2)",
            "C1",
        )
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("Result: 12"),
        "CONCAT should produce 'Result: 12', got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_read_range() {
    let handler = CsvHandler::new();
    let test_data = "1,2,3,4\n5,6,7,8\n9,10,11,12\n";

    let input_path = unique_path("test_range_input", "csv");
    fs::write(&input_path, test_data).unwrap();

    // Read range B1:C2 (columns 1-2, rows 0-1)
    let range = CellRange::parse("B1:C2").unwrap();
    let data = handler.read_range(&input_path, &range).unwrap();

    assert_eq!(data.len(), 2, "Should have 2 rows");
    assert_eq!(data[0], vec!["2", "3"], "First row should be [2, 3]");
    assert_eq!(data[1], vec!["6", "7"], "Second row should be [6, 7]");

    fs::remove_file(&input_path).ok();
}

#[test]
fn test_read_json() {
    let handler = CsvHandler::new();
    let test_data = "1,2\n3,4\n";

    let input_path = unique_path("test_json_input", "csv");
    fs::write(&input_path, test_data).unwrap();

    let json = handler.read_as_json(&input_path).unwrap();

    assert!(json.contains("["), "Should be JSON array");
    assert!(json.contains("\"1\""), "Should contain '1'");
    assert!(json.contains("\"4\""), "Should contain '4'");

    fs::remove_file(&input_path).ok();
}

#[test]
fn test_formula_vlookup() {
    let evaluator = FormulaEvaluator::new();
    // Lookup table: ID, Name, Value
    let test_data = "1,Apple,10\n2,Banana,20\n3,Cherry,30\n";

    let input_path = unique_path("test_vlookup_input", "csv");
    let output_path = unique_path("test_vlookup_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // VLOOKUP(2, A1:C3, 3) - Find 2 in first column, return value from 3rd column
    evaluator
        .apply_to_csv(&input_path, &output_path, "VLOOKUP(2, A1:C3, 3)", "D1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("20"),
        "VLOOKUP should find 20, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_sumif() {
    let evaluator = FormulaEvaluator::new();
    // Values with some > 5
    let test_data = "3,10\n7,20\n2,30\n8,40\n";

    let input_path = unique_path("test_sumif_input", "csv");
    let output_path = unique_path("test_sumif_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // SUMIF(A1:A4, ">5", B1:B4) - Sum B values where A > 5 (20 + 40 = 60)
    evaluator
        .apply_to_csv(
            &input_path,
            &output_path,
            "SUMIF(A1:A4, \">5\", B1:B4)",
            "C1",
        )
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("60"),
        "SUMIF should be 60, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_countif() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "5\n10\n15\n20\n25\n";

    let input_path = unique_path("test_countif_input", "csv");
    let output_path = unique_path("test_countif_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    // COUNTIF(A1:A5, ">10") - Count values > 10 (15, 20, 25 = 3)
    evaluator
        .apply_to_csv(&input_path, &output_path, "COUNTIF(A1:A5, \">10\")", "B1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("3"),
        "COUNTIF should be 3, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}

#[test]
fn test_formula_round() {
    let evaluator = FormulaEvaluator::new();
    let test_data = "3.14159\n";

    let input_path = unique_path("test_round_input", "csv");
    let output_path = unique_path("test_round_output", "csv");
    fs::write(&input_path, test_data).unwrap();

    evaluator
        .apply_to_csv(&input_path, &output_path, "ROUND(A1, 2)", "B1")
        .unwrap();

    let content = fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("3.14"),
        "ROUND should be 3.14, got: {content}"
    );

    fs::remove_file(&input_path).ok();
    fs::remove_file(&output_path).ok();
}