-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worksheet: add/fix handling of NaN/Inf numbers
Feature request #130
- Loading branch information
Showing
8 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright 2022-2025, John McNamara, [email protected] | ||
|
||
//! The following example demonstrates handling NaN and Infinity values and also | ||
//! setting custom string representations. | ||
use rust_xlsxwriter::{Workbook, XlsxError}; | ||
|
||
fn main() -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
// Write NaN and Infinity default values. | ||
worksheet.write(0, 0, "Default:")?; | ||
worksheet.write(0, 1, f64::NAN)?; | ||
worksheet.write(1, 1, f64::INFINITY)?; | ||
worksheet.write(2, 1, f64::NEG_INFINITY)?; | ||
|
||
// Overwrite the default values. | ||
worksheet.set_nan_string("Nan"); | ||
worksheet.set_infinity_string("Infinity"); | ||
worksheet.set_neg_infinity_string("NegInfinity"); | ||
|
||
// Write NaN and Infinity custom values. | ||
worksheet.write(4, 0, "Custom:")?; | ||
worksheet.write(4, 1, f64::NAN)?; | ||
worksheet.write(5, 1, f64::INFINITY)?; | ||
worksheet.write(6, 1, f64::NEG_INFINITY)?; | ||
|
||
workbook.save("worksheet.xlsx")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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 a rust_xlsxwriter file to compare against an Excel file. | ||
fn create_new_xlsx_file_1(filename: &str) -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
worksheet.write(0, 0, "NAN")?; | ||
worksheet.write(1, 0, "INF")?; | ||
worksheet.write(2, 0, "-INF")?; | ||
|
||
workbook.save(filename)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// Test Nan and Inf mapping. | ||
fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
worksheet.write(0, 0, f64::NAN)?; | ||
worksheet.write(1, 0, f64::INFINITY)?; | ||
worksheet.write(2, 0, f64::NEG_INFINITY)?; | ||
|
||
workbook.save(filename)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_bootstrap73_1() { | ||
let test_runner = common::TestRunner::new() | ||
.set_name("bootstrap73") | ||
.set_function(create_new_xlsx_file_1) | ||
.unique("1") | ||
.initialize(); | ||
|
||
test_runner.assert_eq(); | ||
test_runner.cleanup(); | ||
} | ||
|
||
#[test] | ||
fn test_bootstrap73_2() { | ||
let test_runner = common::TestRunner::new() | ||
.set_name("bootstrap73") | ||
.set_function(create_new_xlsx_file_2) | ||
.unique("2") | ||
.initialize(); | ||
|
||
test_runner.assert_eq(); | ||
test_runner.cleanup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 a rust_xlsxwriter file to compare against an Excel file. | ||
fn create_new_xlsx_file_1(filename: &str) -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
worksheet.write(0, 0, "Nan")?; | ||
worksheet.write(1, 0, "Inf")?; | ||
worksheet.write(2, 0, "NegInf")?; | ||
|
||
workbook.save(filename)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// Test Nan and Inf mapping. | ||
fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> { | ||
let mut workbook = Workbook::new(); | ||
let worksheet = workbook.add_worksheet(); | ||
|
||
worksheet.set_nan_string("Nan"); | ||
worksheet.set_infinity_string("Inf"); | ||
worksheet.set_neg_infinity_string("NegInf"); | ||
|
||
worksheet.write(0, 0, f64::NAN)?; | ||
worksheet.write(1, 0, f64::INFINITY)?; | ||
worksheet.write(2, 0, f64::NEG_INFINITY)?; | ||
|
||
workbook.save(filename)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_bootstrap74_1() { | ||
let test_runner = common::TestRunner::new() | ||
.set_name("bootstrap74") | ||
.set_function(create_new_xlsx_file_1) | ||
.unique("1") | ||
.initialize(); | ||
|
||
test_runner.assert_eq(); | ||
test_runner.cleanup(); | ||
} | ||
|
||
#[test] | ||
fn test_bootstrap74_2() { | ||
let test_runner = common::TestRunner::new() | ||
.set_name("bootstrap74") | ||
.set_function(create_new_xlsx_file_2) | ||
.unique("2") | ||
.initialize(); | ||
|
||
test_runner.assert_eq(); | ||
test_runner.cleanup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters