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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 15 deletions

View File

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

View File

@ -1,3 +1,5 @@
use chrono::{DateTime, Local, Offset};
use collab_database::database::timestamp;
use flowy_database2::entities::FieldType; use flowy_database2::entities::FieldType;
use flowy_database2::services::cell::stringify_cell; use flowy_database2::services::cell::stringify_cell;
use flowy_database2::services::field::CHECK; use flowy_database2::services::field::CHECK;
@ -27,15 +29,36 @@ async fn export_csv_test() {
let test = DatabaseEditorTest::new_grid().await; let test = DatabaseEditorTest::new_grid().await;
let database = test.editor.clone(); let database = test.editor.clone();
let s = database.export_csv(CSVFormat::Original).await.unwrap(); let s = database.export_csv(CSVFormat::Original).await.unwrap();
let expected = r#"Name,Price,Time,Status,Platform,is urgent,link,TODO,Last Modified,Created At,Related let format = "%Y/%m/%d %R";
A,$1,2022/03/14,,"Google,Facebook",Yes,AppFlowy website - https://www.appflowy.io,First thing,,, let naive = chrono::NaiveDateTime::from_timestamp_opt(timestamp(), 0).unwrap();
,$2,2022/03/14,,"Google,Twitter",Yes,,"Have breakfast,Have lunch,Take a nap,Have dinner,Shower and head to bed",,, let offset = Local::now().offset().fix();
C,$3,2022/03/14,Completed,"Facebook,Google,Twitter",No,,,,, let date_time = DateTime::<Local>::from_naive_utc_and_offset(naive, offset);
DA,$14,2022/11/17,Completed,,No,,Task 1,,, let date_string = format!("{}", date_time.format(format));
AE,,2022/11/13,Planned,"Facebook,Twitter",No,,,,, let expected = format!(
AE,$5,2022/12/25,Planned,Facebook,Yes,,"Sprint,Sprint some more,Rest",,, r#"Name,Price,Time,Status,Platform,is urgent,link,TODO,Last Modified,Created At,Related
CB,,,,,,,,,, A,$1,2022/03/14,,"Google,Facebook",Yes,AppFlowy website - https://www.appflowy.io,First thing,{},{},
"#; ,$2,2022/03/14,,"Google,Twitter",Yes,,"Have breakfast,Have lunch,Take a nap,Have dinner,Shower and head to bed",{},{},
C,$3,2022/03/14,Completed,"Facebook,Google,Twitter",No,,,{},{},
DA,$14,2022/11/17,Completed,,No,,Task 1,{},{},
AE,,2022/11/13,Planned,"Facebook,Twitter",No,,,{},{},
AE,$5,2022/12/25,Planned,Facebook,Yes,,"Sprint,Sprint some more,Rest",{},{},
CB,,,,,,,,{},{},
"#,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
date_string,
);
println!("{}", s); println!("{}", s);
assert_eq!(s, expected); assert_eq!(s, expected);
} }