Skip to content

Commit

Permalink
formula: fix boolean result output
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Feb 1, 2025
1 parent 38c7bac commit 3ed5703
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17677,7 +17677,14 @@ impl Worksheet {
};

let formula = crate::xmlwriter::escape_xml_data(formula);
let result = crate::xmlwriter::escape_xml_data(result);
let mut result = crate::xmlwriter::escape_xml_data(result);

if result == "TRUE" {
result = Cow::Owned("1".to_string());
}
if result == "FALSE" {
result = Cow::Owned("0".to_string());
}

write!(
writer,
Expand Down Expand Up @@ -17725,7 +17732,14 @@ impl Worksheet {
};

let formula = crate::xmlwriter::escape_xml_data(formula);
let result = crate::xmlwriter::escape_xml_data(result);
let mut result = crate::xmlwriter::escape_xml_data(result);

if result == "TRUE" {
result = Cow::Owned("1".to_string());
}
if result == "FALSE" {
result = Cow::Owned("0".to_string());
}

write!(
writer,
Expand Down
Binary file added tests/input/formula_results01.xlsx
Binary file not shown.
79 changes: 79 additions & 0 deletions tests/integration/formula_results01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2025, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{Workbook, XlsxError};

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

worksheet
.write_formula(0, 0, "1+1")?
.set_formula_result(0, 0, "2");

worksheet
.write_formula(1, 0, "\"Foo\"")?
.set_formula_result(1, 0, "Foo");

worksheet
.write_formula(2, 0, "IF(B3,FALSE,TRUE)")?
.set_formula_result(2, 0, "TRUE");

worksheet
.write_formula(3, 0, "IF(B4,TRUE,FALSE)")?
.set_formula_result(3, 0, "FALSE");

worksheet
.write_formula(4, 0, "#DIV/0!")?
.set_formula_result(4, 0, "#DIV/0!");

worksheet
.write_formula(5, 0, "#N/A")?
.set_formula_result(5, 0, "#N/A");

worksheet
.write_formula(6, 0, "#NAME?")?
.set_formula_result(6, 0, "#NAME?");

worksheet
.write_formula(7, 0, "#NULL!")?
.set_formula_result(7, 0, "#NULL!");

worksheet
.write_formula(8, 0, "#NUM!")?
.set_formula_result(8, 0, "#NUM!");

worksheet
.write_formula(9, 0, "#REF!")?
.set_formula_result(9, 0, "#REF!");

worksheet
.write_formula(10, 0, "#VALUE!")?
.set_formula_result(10, 0, "#VALUE!");

worksheet
.write_formula(11, 0, "1/0")?
.set_formula_result(11, 0, "#DIV/0!");

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_formula_results01() {
let test_runner = common::TestRunner::new()
.set_name("formula_results01")
.set_function(create_new_xlsx_file)
.ignore_calc_chain()
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
1 change: 1 addition & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ mod format21;
mod format22;
mod format23;
mod format24;
mod formula_results01;
mod gridlines01;
mod header_image01;
mod header_image02;
Expand Down

0 comments on commit 3ed5703

Please sign in to comment.