xlsx_diff/core/
mod.rs

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
use std::error::Error;
use std::path::PathBuf;
extern crate serde_json;
mod diff;
mod parse;
use diff::DiffItem;
use parse::CalamineWorkbook;
use parse::{FileLike, SerializableData};
use serde::Serialize;
#[derive(Serialize)]
struct ModifiedSheet {
    sheet_name: String,
    diff: Vec<DiffItem<Vec<SerializableData>>>,
}
#[derive(Serialize)]
struct OriginData {
    old: CalamineWorkbook,
    new: CalamineWorkbook,
}
#[derive(Serialize)]
pub struct DiffResult {
    added_sheets: Vec<String>,
    removed_sheets: Vec<String>,
    modified_sheets: Vec<ModifiedSheet>,
    data: Option<OriginData>,
}
/**
 * Compare two xlsx files
 * @param old_file_path old file path
 * @param new_file_path new file path
 * @param raw_data output raw data
 * @param header_row todo
 * @returns diff result
 */
pub fn diff_xlsx(
    old_file_path: PathBuf,
    new_file_path: PathBuf,
    raw_data: bool,
    // header_row: usize,
) -> Result<DiffResult, Box<dyn Error>> {
    let mut modified_sheets: Vec<ModifiedSheet> = vec![];
    // file path
    let wb_old: parse::CalamineWorkbook = parse::load_workbook(&FileLike::Path(old_file_path))?;
    let wb_new = parse::load_workbook(&FileLike::Path(new_file_path))?;
    // find added and removed sheets
    let added_sheets: Vec<String> = wb_new
        .sheet_names
        .iter()
        .filter(|sheet_name| !wb_old.sheet_names.contains(*sheet_name))
        .map(|sheet_name| sheet_name.to_string())
        .collect();
    let removed_sheets: Vec<String> = wb_old
        .sheet_names
        .iter()
        .filter(|sheet_name| !wb_new.sheet_names.contains(*sheet_name))
        .map(|sheet_name| sheet_name.to_string())
        .collect();
    // find modified sheets
    for sheet_name in wb_old.sheet_names.iter() {
        if wb_new.sheet_names.contains(sheet_name) {
            let wb_old_first_sheet_data: &Vec<Vec<SerializableData>> =
                wb_old.data.get(&sheet_name.to_string()).unwrap();
            let wb_new_first_sheet_data = wb_new.data.get(&sheet_name.to_string()).unwrap();
            let res = diff::myers_diff(&wb_old_first_sheet_data, &wb_new_first_sheet_data);
            modified_sheets.push(ModifiedSheet {
                sheet_name: sheet_name.to_string(),
                diff: res,
            });
        }
    }
    return Ok(DiffResult {
        added_sheets,
        removed_sheets,
        modified_sheets,
        data: if raw_data {
            Some(OriginData {
                old: wb_old,
                new: wb_new,
            })
        } else {
            None
        },
    });
}