diff --git a/src/serializer.rs b/src/serializer.rs index b89bad2f..aa9af7a7 100644 --- a/src/serializer.rs +++ b/src/serializer.rs @@ -1167,9 +1167,7 @@ pub(crate) struct SerializerState { pub(crate) structs: HashMap>, pub(crate) current_struct: String, pub(crate) current_field: String, - pub(crate) current_col: ColNum, pub(crate) current_row: RowNum, - pub(crate) value_format: Option, } impl SerializerState { @@ -1179,32 +1177,30 @@ impl SerializerState { structs: HashMap::new(), current_struct: String::new(), current_field: String::new(), - current_col: 0, current_row: 0, - value_format: None, } } // Check if the current struct/field have been selected to be serialized by - // the user. If it has then set the row/col values for the next write() call. - pub(crate) fn is_known_field(&mut self) -> bool { + // the user. If it has then set the row value for the next write() call. + pub(crate) fn current_state(&mut self) -> Result<(RowNum, ColNum, Option), ()> { let Some(fields) = self.structs.get_mut(&self.current_struct) else { - return false; + return Err(()); }; let Some(field) = fields.get_mut(&self.current_field) else { - return false; + return Err(()); }; // Set the "current" cell values used to write the serialized data. - self.current_col = field.col; - self.current_row = field.row; - self.value_format = field.value_format.clone(); + let row = field.row; + let col = field.col; + let value_format = field.value_format.clone(); // Increment the row number for the next worksheet.write(). field.row += 1; - true + Ok((row, col, value_format)) } } diff --git a/src/worksheet.rs b/src/worksheet.rs index 39f25a3e..a2b5cf0d 100644 --- a/src/worksheet.rs +++ b/src/worksheet.rs @@ -6742,19 +6742,18 @@ impl Worksheet { &mut self, data: impl IntoExcelData, ) -> Result<(), XlsxError> { - if !self.serializer_state.is_known_field() { - return Ok(()); + let result = self.serializer_state.current_state(); + + match result { + Ok(result) => { + let (row, col, value_format) = result; + match &value_format { + Some(format) => self.write_with_format(row, col, data, format).map(|_| ()), + None => self.write(row, col, data).map(|_| ()), + } + } + Err(()) => Ok(()), } - - let row = self.serializer_state.current_row; - let col = self.serializer_state.current_col; - - match &self.serializer_state.value_format.clone() { - Some(format) => self.write_with_format(row, col, data, format)?, - None => self.write(row, col, data)?, - }; - - Ok(()) } // -----------------------------------------------------------------------