fix: export created at and last modified cells to csv (#5235)

* fix: export created at and last modified cells to csv

* fix: export csv test
This commit is contained in:
Richard Shiue
2024-05-01 12:24:11 +08:00
committed by GitHub
parent 10a1571910
commit 1d73174b0c
2 changed files with 59 additions and 15 deletions

View File

@ -1,9 +1,13 @@
use collab_database::database::Database;
use collab_database::fields::Field;
use collab_database::rows::Cell;
use indexmap::IndexMap;
use flowy_error::{FlowyError, FlowyResult};
use crate::entities::FieldType;
use crate::services::cell::stringify_cell;
use crate::services::field::{TimestampCellData, TimestampCellDataWrapper};
#[derive(Debug, Clone, Copy)]
pub enum CSVFormat {
@ -40,15 +44,32 @@ impl CSVExport {
field_by_field_id.insert(field.id.clone(), field);
});
let rows = database.get_rows_for_view(&inline_view_id);
let stringify = |cell: &Cell, field: &Field, style: CSVFormat| match style {
CSVFormat::Original => stringify_cell(cell, field),
CSVFormat::META => serde_json::to_string(cell).unwrap_or_else(|_| "".to_string()),
};
for row in rows {
let cells = field_by_field_id
.iter()
.map(|(field_id, field)| match row.cells.get(field_id) {
None => "".to_string(),
Some(cell) => match style {
CSVFormat::Original => stringify_cell(cell, field),
CSVFormat::META => serde_json::to_string(cell).unwrap_or_else(|_| "".to_string()),
},
.map(|(field_id, field)| {
let field_type = FieldType::from(field.field_type);
match field_type {
FieldType::LastEditedTime | FieldType::CreatedTime => {
let cell_data = if field_type.is_created_time() {
TimestampCellData::new(row.created_at)
} else {
TimestampCellData::new(row.modified_at)
};
let cell = Cell::from(TimestampCellDataWrapper::from((field_type, cell_data)));
stringify(&cell, field, style)
},
_ => match row.cells.get(field_id) {
None => "".to_string(),
Some(cell) => stringify(cell, field, style),
},
}
})
.collect::<Vec<_>>();