Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/structs/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,39 @@ impl Cell {
self
}

#[inline]
pub fn set_formula_result_number<T>(&mut self, value: T) -> &mut Self
where
T: Into<f64>,
{
self.cell_value.set_formula_result_number(value);
self
}

#[inline]
pub fn set_formula_result_bool(&mut self, value: bool) -> &mut Self {
self.cell_value.set_formula_result_bool(value);
self
}

#[inline]
pub fn set_formula_result_error(&mut self, value: crate::CellErrorType) -> &mut Self {
self.cell_value.set_formula_result_error(value);
self
}

#[inline]
pub fn set_formula_result_string<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.cell_value.set_formula_result_string(value);
self
}

#[inline]
pub fn set_formula_result_blank(&mut self) -> &mut Self {
self.cell_value.set_formula_result_blank();
self
}

#[inline]
pub fn set_blank(&mut self) -> &mut Self {
self.cell_value.set_blank();
Expand Down Expand Up @@ -703,8 +736,7 @@ impl Cell {
write_text_node(writer, prm);
}
"e" => {
let prm = "#VALUE!";
write_text_node(writer, prm);
write_text_node(writer, self.value());
}
_ => write_text_node_conversion(writer, self.value()),
}
Expand Down
116 changes: 115 additions & 1 deletion src/structs/cell_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ impl CellValue {
#[inline]
pub(crate) fn data_type_crate(&self) -> &str {
match &self.formula {
Some(_) => "str",
Some(_) => match &self.raw_value {
CellRawValue::String(_) | CellRawValue::RichText(_) | CellRawValue::Lazy(_) => {
"str"
}
CellRawValue::Bool(_) => "b",
CellRawValue::Error(_) => "e",
CellRawValue::Numeric(_) => "n",
CellRawValue::Empty => "",
},
None => self.raw_value.data_type(),
}
}
Expand Down Expand Up @@ -266,6 +274,59 @@ impl CellValue {
self
}

/// Sets a formula cached result as a numeric value.
///
/// This method only updates the cached value (`<v>`) and does not remove
/// the formula object (`<f>`).
#[inline]
pub fn set_formula_result_number<T>(&mut self, value: T) -> &mut Self
where
T: Into<f64>,
{
self.raw_value = CellRawValue::Numeric(value.into());
self
}

/// Sets a formula cached result as a boolean value.
///
/// This method only updates the cached value (`<v>`) and does not remove
/// the formula object (`<f>`).
#[inline]
pub fn set_formula_result_bool(&mut self, value: bool) -> &mut Self {
self.raw_value = CellRawValue::Bool(value);
self
}

/// Sets a formula cached result as an Excel error value.
///
/// This method only updates the cached value (`<v>`) and does not remove
/// the formula object (`<f>`).
#[inline]
pub fn set_formula_result_error(&mut self, value: CellErrorType) -> &mut Self {
self.raw_value = CellRawValue::Error(value);
self
}

/// Sets a formula cached result as a string value.
///
/// This method only updates the cached value (`<v>`) and does not remove
/// the formula object (`<f>`).
#[inline]
pub fn set_formula_result_string<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.raw_value = CellRawValue::String(value.into().into_boxed_str());
self
}

/// Sets a formula cached result as blank.
///
/// This method only updates the cached value (`<v>`) and does not remove
/// the formula object (`<f>`).
#[inline]
pub fn set_formula_result_blank(&mut self) -> &mut Self {
self.raw_value = CellRawValue::Empty;
self
}

#[inline]
pub fn set_error<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.set_value_crate(value);
Expand Down Expand Up @@ -440,4 +501,57 @@ mod tests {
assert!(cell.raw_value.is_error());
assert_eq!(cell.raw_value, CellRawValue::Error(CellErrorType::Null));
}

#[test]
fn formula_cached_value_data_type_tracks_raw_value_kind() {
let mut obj = CellValue::default();

obj.set_formula("1+1").set_formula_result_default("2");
assert_eq!(obj.data_type_crate(), "n");

obj.set_formula_result_default("TRUE");
assert_eq!(obj.data_type_crate(), "b");

obj.set_formula_result_default("#N/A");
assert_eq!(obj.data_type_crate(), "e");

obj.set_formula_result_default("OK");
assert_eq!(obj.data_type_crate(), "str");

obj.set_formula_result_default("");
assert_eq!(obj.data_type_crate(), "");

obj.remove_formula();
obj.set_value_string("OK");
assert_eq!(obj.data_type_crate(), "s");
}

#[test]
fn typed_formula_result_helpers_preserve_formula() {
let mut obj = CellValue::default();
obj.set_formula("A1+1");

obj.set_formula_result_number(3.5);
assert_eq!(obj.formula(), "A1+1");
assert_eq!(obj.raw_value, CellRawValue::Numeric(3.5));

obj.set_formula_result_bool(false);
assert_eq!(obj.formula(), "A1+1");
assert_eq!(obj.raw_value, CellRawValue::Bool(false));

obj.set_formula_result_error(CellErrorType::Ref);
assert_eq!(obj.formula(), "A1+1");
assert_eq!(obj.raw_value, CellRawValue::Error(CellErrorType::Ref));

obj.set_formula_result_string("OK");
assert_eq!(obj.formula(), "A1+1");
assert!(matches!(
&obj.raw_value,
CellRawValue::String(value) if value.as_ref() == "OK"
));

obj.set_formula_result_blank();
assert_eq!(obj.formula(), "A1+1");
assert_eq!(obj.raw_value, CellRawValue::Empty);
}
}
Loading
Loading