spreadsheet-to-json 0.1.5

Asynchronous conversion of Excel and OpenDocument spreadsheets as well as CSV and TSV files to JSON or JSONL
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
use std::str::FromStr;
use std::sync::Arc;

use csv::{ReaderBuilder, StringRecord};
use heck::ToSnakeCase;
use tokio::sync::mpsc;
use serde_json::{Number, Value};
use simple_string_patterns::*;
use indexmap::IndexMap;
use std::path::Path;

use calamine::{open_workbook_auto, Data, Reader};

use crate::headers::*;
use crate::data_set::*;
use crate::is_truthy::*;
use crate::Extension;
use crate::Format;
use crate::OptionSet;
use crate::euro_number_format::is_euro_number_format;
use crate::PathData;
use crate::RowOptionSet;
use crate::error::GenericError;

/// Output the result set with captured rows (up to the maximum allowed) directly.
/// This is now synchronous and calls the asynchronous function using a runtime.
pub fn process_spreadsheet_direct(opts: &OptionSet) -> Result<ResultSet, GenericError> {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(render_spreadsheet_core(opts, None, None))
}

/// Output the result set with captured rows (up to the maximum allowed) directly.
/// Use this in an async function using the tokio runtime if you direct results
/// without a save callback
pub async fn render_spreadsheet_direct(opts: &OptionSet) -> Result<ResultSet, GenericError> {
  render_spreadsheet_core(opts, None, None).await
}

/// Output the result set with deferred row saving and optional output reference
pub async fn process_spreadsheet_async(
  opts: &OptionSet,
  save_func: Box<dyn Fn(IndexMap<String, Value>) -> Result<(), GenericError> + Send + Sync>,
  out_ref: Option<&str>
  ) -> Result<ResultSet, GenericError> {
  render_spreadsheet_core(opts, Some(save_func), out_ref).await
}

/// Output the result set with captured rows (up to the maximum allowed) directly.
/// with optional asynchronous row save method and output reference
pub async fn render_spreadsheet_core(
    opts: &OptionSet,
    save_opt: Option<Box<dyn Fn(IndexMap<String, Value>) -> Result<(), GenericError> + Send + Sync>>,
    out_ref: Option<&str>
) -> Result<ResultSet, GenericError> {
    if let Some(filepath) = opts.path.clone() {
        let path = Path::new(&filepath);
        if !path.exists() {
            #[allow(dead_code)]
            return Err(GenericError("file_unavailable"));
        }
        let path_data = PathData::new(path);
        if path_data.is_valid() {
            if path_data.use_calamine() {
                read_workbook_core(&path_data, opts, save_opt, out_ref).await
            } else {
                read_csv_core(&path_data, opts, save_opt, out_ref).await
            }
        } else {
            Err(GenericError("unsupported_format"))
        }
    } else {
        Err(GenericError("no_filepath_specified"))
    }
}

/// Parse spreadsheets with an optional callback method to save rows asynchronously and an optional output reference
/// that may be a file name or database identifier
pub async fn read_workbook_core<'a>(
    path_data: &PathData<'a>, opts: &OptionSet,
    save_opt: Option<Box<dyn Fn(IndexMap<String, Value>) -> Result<(), GenericError> + Send + Sync>>,
    out_ref: Option<&str>
) -> Result<ResultSet, GenericError> {
    if let Ok(mut workbook) = open_workbook_auto(path_data.path()) {
        let columns = opts.rows.columns.clone();
        let max_rows = opts.max_rows();
        let (sheet_name_opt, sheet_names, sheet_index) = match_sheet_name_and_index(&mut workbook, opts);
        let capture_rows = opts.capture_rows();
        if let Some(first_sheet_name) = sheet_name_opt {
            let range = workbook.worksheet_range(&first_sheet_name.clone())?;
            let mut headers: Vec<String> = vec![];
            let mut has_headers = false;
            let capture_headers = !opts.omit_header;
            let source_rows = range.rows();
            let mut rows: Vec<IndexMap<String, Value>> = vec![];
            let mut row_index = 0;
            let header_row_index = opts.header_row_index();
            let match_header_row_below = capture_headers && header_row_index > 0;

            if let Some(first_row) = range.headers() {
                headers = build_header_keys(&first_row, &columns, &opts.field_mode);
                has_headers = !match_header_row_below;
            }
            let total = source_rows.clone().count();
            if capture_rows || match_header_row_below {
                let max_row_count = if capture_rows {
                    max_rows
                } else {
                    header_row_index + 2
                };

                for row in source_rows.clone().take(max_row_count).collect::<Vec<&[Data]>>() {
                    if row_index >= max_row_count {
                        break;
                    }
                    if match_header_row_below && (row_index + 1) == header_row_index {
                        let h_row = row.into_iter().map(|c| c.to_string().to_snake_case()).collect::<Vec<String>>();
                        headers = build_header_keys(&h_row, &columns, &opts.field_mode);
                        has_headers = true;
                    } else if (has_headers || !capture_headers) && capture_rows {
                        // only capture rows if headers are either omitted or have already been captured
                        let row_map = workbook_row_to_map(row, &opts.rows, &headers);
                        rows.push(row_map);
                    }
                    row_index += 1;
                }
            }
            if let Some(save_method) = save_opt {
                let (tx, mut rx) = mpsc::channel(32);
                let opts = Arc::new(opts.clone()); // Clone opts if possible, or wrap in Arc
                let headers = headers.clone();     // Clone headers since it's used in the task
                let first_sheet_name_clone = first_sheet_name.clone();
                tokio::spawn(async move {
                    if let Ok(range) = workbook.worksheet_range(&first_sheet_name_clone) {
                        let source_rows = range.rows();
                        for row in source_rows {
                            let row_map = workbook_row_to_map(row, &opts.rows, &headers);
                            if tx.send(row_map).await.is_err() {
                                // Channel closed, stop sending
                                break;
                            }
                        }
                    }
                });

                // Process the rows as they come in
                while let Some(row) = rx.recv().await {
                    save_method(row)?;
                }
            }
            let info = WorkbookInfo::new(path_data, &first_sheet_name, sheet_index, &sheet_names);
            let ds = DataSet::from_count_and_rows(total, rows, opts);
            Ok(ResultSet::new(info, &headers, ds, out_ref))
        } else {
            Err(GenericError("workbook_with_no_sheets"))
        }
    } else {
        Err(GenericError("cannot_open_workbook"))
    }
}

/// Process a CSV/TSV file asynchronously with an optional row save method 
/// and output reference (file or database table reference)
pub async fn read_csv_core<'a>(
    path_data: &PathData<'a>,
    opts: &OptionSet,
    save_opt: Option<Box<dyn Fn(IndexMap<String, Value>) -> Result<(), GenericError> + Send + Sync>>,
    out_ref: Option<&str>
) -> Result<ResultSet, GenericError> {
    let separator = match path_data.mode() {
        Extension::Tsv => b't',
        _ => b',',
    };
    if let Ok(mut rdr) = ReaderBuilder::new().delimiter(separator).from_path(path_data.path()) {
        let capture_header = opts.omit_header == false;
        let mut rows: Vec<IndexMap<String, Value>> = vec![];
        let mut line_count = 0;
        let has_max = opts.max.is_some();

        let max_line_usize = opts.max_rows();
        let mut headers: Vec<String> = vec![];
        let capture_rows = opts.capture_rows();
        if capture_header {
            if let Ok(hdrs) = rdr.headers() {
                headers = hdrs.into_iter().map(|s| s.to_owned()).collect();
            }
            let columns = opts.rows.columns.clone();
            headers = build_header_keys(&headers, &columns, &opts.field_mode);
        }

        let mut total = 0;
        if capture_rows {
            for result in rdr.records() {
                if has_max && line_count >= max_line_usize {
                    break;
                }
                if let Some(row) = csv_row_result_to_values(result, Arc::new(&opts.rows)) {
                    rows.push(to_index_map(&row, &headers));
                    line_count += 1;
                }
            }
            total = line_count + rdr.records().count() + 1;
        } else {
            // duplicate reader for accurate non-consuming count
            if let Ok(mut count_rdr) = ReaderBuilder::new().from_path(&path_data.path()) {
                total = count_rdr.records().count();
            }
            // Spawn a task to read from CSV and save data row by row
            if let Some(save_method) = save_opt {
                let (tx, mut rx) = mpsc::channel(32);
                let opts = Arc::new(opts.clone()); // Clone opts if possible, or wrap in Arc
                let headers = headers.clone();     // Clone headers since it's used in the task
                tokio::spawn(async move {
                    for result in rdr.records() {
                        if let Some(row) = csv_row_result_to_values(result, Arc::new(&opts.rows)) {
                            let row_map = to_index_map(&row, &headers);
                            if tx.send(row_map).await.is_err() {
                                // Channel closed, stop sending
                                break;
                            }
                        }
                    }
                });

                // Process the rows as they come in
                while let Some(row) = rx.recv().await {
                    save_method(row)?;
                }
            }
        }
        let info = WorkbookInfo::simple(path_data);
        let ds = DataSet::from_count_and_rows(total, rows, opts);
        Ok(ResultSet::new(info, &headers, ds, out_ref))
    } else {
        let error_msg = match path_data.ext() {
            Extension::Tsv => "unreadable_tsv_file",
            _ => "unreadable_csv_file"
        };
        Err(GenericError(error_msg))
    }
}

// Convert an array of row data to an IndexMap of serde_json::Value objects
fn workbook_row_to_map(row: &[Data], opts: &RowOptionSet, headers: &[String]) -> IndexMap<String, Value> {
    to_index_map(&workbook_row_to_values(row, &opts), headers)
}

// Convert an array of row data to a vector of serde_json::Value objects
fn workbook_row_to_values(row: &[Data], opts: &RowOptionSet) -> Vec<Value> {
    let mut c_index = 0;
    let mut cells: Vec<Value> = vec![];
    for cell in row {
        let value = workbook_cell_to_value(cell, Arc::new(opts), c_index);
        cells.push(value);
        c_index += 1;
    }
    cells
}

// convert a spreadsheet data cell to a polymorphic serde_json::Value object
fn workbook_cell_to_value(cell: &Data, opts: Arc<&RowOptionSet>, c_index: usize) -> Value {
    let col = opts.column(c_index);
    let format = if let Some(c) = col {
        c.format.to_owned()
    } else {
        Format::Auto
    };
    match cell {
        Data::Int(i) => Value::Number(Number::from_i128(*i as i128).unwrap()),
        Data::Float(f) => {
            match format {
                Format::Integer => Value::Number(Number::from_i128(*f as i128).unwrap()),
                _ => Value::Number(Number::from_f64(*f).unwrap())
            }
        },
        Data::DateTimeIso(d) => Value::String(d.to_owned()),
        Data::DateTime(d) => {
            let ndt = d.as_datetime();
            let dt_ref = if let Some(dt) = ndt {
                let fmt_str = match format {
                    Format::Date => "%Y-%m-%d",
                    _ => if opts.date_only {
                        "%Y-%m-%d"
                    } else {
                        "%Y-%m-%dT%H:%M:%S.000Z"
                    }
                };
                dt.format(fmt_str).to_string()
            } else {
                "".to_string()
            };
            Value::String(dt_ref)
        },
        Data::Bool(b) => Value::Bool(*b),
        // For other types, convert to string since JSON can't directly represent them as unquoted values
        Data::Empty => Value::Null,
        _ => Value::String(cell.to_string())
    }
}

// Convert csv rows to value
fn csv_row_result_to_values(result: Result<StringRecord, csv::Error>, opts: Arc<&RowOptionSet>) -> Option<Vec<Value>> {
    if let Ok(record) = result {
        let mut row: Vec<Value> = vec![];
        let mut ci: usize = 0;
        for cell in record.into_iter() {
            let new_cell = csv_cell_to_json_value(cell, opts.clone(), ci);
            row.push(new_cell);
            ci += 1;
        }
        return Some(row)
    }
    None
}

// convert CSV cell &str value to a polymorphic serde_json::VALUE
fn csv_cell_to_json_value(cell: &str, opts: Arc<&RowOptionSet>, index: usize) -> Value {
    let has_number = cell.to_first_number::<f64>().is_some();
    // clean cell to check if it's numeric
    let col = opts.column(index);
    let fmt = if let Some(c) = col.cloned() {
        c.format
    } else {
        Format::Auto
    };
    let euro_num_mode = if let Some(c) = col.cloned() {
        c.euro_number_format
    } else {
        opts.euro_number_format
    };
    let num_cell = if has_number {
        let euro_num_mode = is_euro_number_format(cell, euro_num_mode);
        if euro_num_mode {
            cell.replace(",", ".").replace(",", ".")
        } else {
            cell.replace(",", "")
        }
    } else {
        cell.to_owned()
    };
    let mut new_cell = Value::Null;
    if num_cell.len() > 0 && num_cell.is_numeric() {
        if let Ok(float_val) = serde_json::Number::from_str(&num_cell) {
            match fmt {
                Format::Integer => {
                    if let Some(int_val) = Number::from_i128(float_val.as_i128().unwrap_or(0)) {
                        new_cell = Value::Number(int_val);
                    }
                },
                Format::Boolean => {
                    // only 1.0 or more will evaluate as true
                    new_cell = Value::Bool(float_val.as_f64().unwrap_or(0f64) >= 1.0);
                },
                _ => {
                    new_cell = Value::Number(float_val);
                }
            }
        }
    } else if let Some(is_true) = is_truthy_core(cell, false) {
        new_cell = Value::Bool(is_true);
    } else {
        new_cell = match fmt {
            Format::Truthy => {
                if let Some(is_true) = is_truthy_standard(cell, false) {
                    Value::Bool(is_true)
                } else {
                    Value::Null
                }
            }
            _ => Value::String(cell.to_string())
        };
    }
    new_cell
}



#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_direct_processing_xlsx() {
      let sample_path = "data/sample-data-1.xlsx";

      // instantiate the OptionSet with a sample path and a maximum row count of 1000 rows as the source file has 401 rows
      // (although )the default max is 10,000)
      let opts = OptionSet::new(sample_path).max_row_count(1_000);

      let result = process_spreadsheet_direct(&opts); 
      
      // The source file should have 1 header row and 400 data rows
      assert_eq!(result.unwrap().num_rows,401);
  }

  #[test]
  fn test_direct_processing_csv() {
    let sample_path = "data/sample-data-1.csv";

    // instantiate the OptionSet with a sample path and a maximum row count of 1000 rows as the source file has 401 rows
    // (although )the default max is 10,000)
    let opts = OptionSet::new(sample_path).max_row_count(1_000);

    let result = process_spreadsheet_direct(&opts); 
    
    // The source file should have 1 header row and 400 data rows
    assert_eq!(result.unwrap().num_rows,401);
}

}