Exporting the Merged Cell : Exports the merged cell properly with testcase to verify the scenario (#76)
* adding the functionality of exporting the merged cell with testcase to vreify the above enhancement * addressing review comments : 1) moving the testing to appropriate folder 2) fixing lint errors 3) fixing the scenario when merge cell count is 0 * addressing 2nd round review comments : cosmetic fixes * addressing review comments : taking reference instead of cloning
This commit is contained in:
@@ -59,6 +59,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
) -> String {
|
||||
let mut sheet_data_str: Vec<String> = vec![];
|
||||
let mut cols_str: Vec<String> = vec![];
|
||||
let mut merged_cells_str: Vec<String> = vec![];
|
||||
|
||||
for col in &worksheet.cols {
|
||||
// <col min="4" max="4" width="12" customWidth="1"/>
|
||||
@@ -241,6 +242,12 @@ pub(crate) fn get_worksheet_xml(
|
||||
))
|
||||
}
|
||||
let sheet_data = sheet_data_str.join("");
|
||||
|
||||
for merge_cell_ref in &worksheet.merge_cells {
|
||||
merged_cells_str.push(format!("<mergeCell ref=\"{merge_cell_ref}\"/>"))
|
||||
}
|
||||
let merged_cells_count = merged_cells_str.len();
|
||||
|
||||
let cols = cols_str.join("");
|
||||
let cols = if cols.is_empty() {
|
||||
"".to_string()
|
||||
@@ -280,6 +287,16 @@ pub(crate) fn get_worksheet_xml(
|
||||
}
|
||||
}
|
||||
|
||||
let merge_cells_section = if merged_cells_count > 0 {
|
||||
format!(
|
||||
"<mergeCells count=\"{}\">{}</mergeCells>",
|
||||
merged_cells_count,
|
||||
merged_cells_str.join("")
|
||||
)
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
format!(
|
||||
"{XML_DECLARATION}
|
||||
<worksheet \
|
||||
@@ -295,6 +312,7 @@ xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">
|
||||
<sheetData>\
|
||||
{sheet_data}\
|
||||
</sheetData>\
|
||||
{merge_cells_section}\
|
||||
</worksheet>"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -334,3 +334,68 @@ fn no_export() {
|
||||
}
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
// This test verifies whether exporting the merged cells functionality is happening properly or not.
|
||||
// It first loads the excell having the merged cell and exports it to another xlsx and verifies whether merged
|
||||
// cell node is same in both of the xlsx file or not.
|
||||
#[test]
|
||||
fn test_exporting_merged_cells() {
|
||||
let temp_file_name = "temp_file_test_export_merged_cells.xlsx";
|
||||
let expected_merge_cell_ref = {
|
||||
// loading the xlsx file containing merged cells
|
||||
let example_file_name = "tests/example.xlsx";
|
||||
let mut model = load_from_xlsx(example_file_name, "en", "UTC").unwrap();
|
||||
let expected_merge_cell_ref = model
|
||||
.workbook
|
||||
.worksheets
|
||||
.first()
|
||||
.unwrap()
|
||||
.merge_cells
|
||||
.clone();
|
||||
// exporting and saving it in another xlsx
|
||||
model.evaluate();
|
||||
save_to_xlsx(&model, temp_file_name).unwrap();
|
||||
expected_merge_cell_ref
|
||||
};
|
||||
{
|
||||
let mut temp_model = load_from_xlsx(temp_file_name, "en", "UTC").unwrap();
|
||||
{
|
||||
// loading the previous file back and verifying whether
|
||||
// merged cells got exported properly or not
|
||||
let got_merge_cell_ref = &temp_model
|
||||
.workbook
|
||||
.worksheets
|
||||
.first()
|
||||
.unwrap()
|
||||
.merge_cells
|
||||
.clone();
|
||||
assert_eq!(expected_merge_cell_ref, *got_merge_cell_ref);
|
||||
fs::remove_file(temp_file_name).unwrap();
|
||||
}
|
||||
{
|
||||
// this block is to verify that if there are no
|
||||
// merged cells, exported xml should not have the
|
||||
// <mergeCells/> xml node
|
||||
temp_model
|
||||
.workbook
|
||||
.worksheets
|
||||
.get_mut(0)
|
||||
.unwrap()
|
||||
.merge_cells
|
||||
.clear();
|
||||
|
||||
save_to_xlsx(&temp_model, temp_file_name).unwrap();
|
||||
let temp_model2 = load_from_xlsx(temp_file_name, "en", "UTC").unwrap();
|
||||
let got_merge_cell_ref_cnt = &temp_model2
|
||||
.workbook
|
||||
.worksheets
|
||||
.first()
|
||||
.unwrap()
|
||||
.merge_cells
|
||||
.len();
|
||||
assert!(*got_merge_cell_ref_cnt == 0);
|
||||
}
|
||||
}
|
||||
|
||||
fs::remove_file(temp_file_name).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user