mirror of
				https://github.com/AppFlowy-IO/AppFlowy.git
				synced 2025-11-04 03:54:44 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									10a1571910
								
							
						
					
					
						commit
						1d73174b0c
					
				@ -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<_>>();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,5 @@
 | 
			
		||||
use chrono::{DateTime, Local, Offset};
 | 
			
		||||
use collab_database::database::timestamp;
 | 
			
		||||
use flowy_database2::entities::FieldType;
 | 
			
		||||
use flowy_database2::services::cell::stringify_cell;
 | 
			
		||||
use flowy_database2::services::field::CHECK;
 | 
			
		||||
@ -27,15 +29,36 @@ async fn export_csv_test() {
 | 
			
		||||
  let test = DatabaseEditorTest::new_grid().await;
 | 
			
		||||
  let database = test.editor.clone();
 | 
			
		||||
  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
 | 
			
		||||
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,,,,,,,,,,
 | 
			
		||||
"#;
 | 
			
		||||
  let format = "%Y/%m/%d %R";
 | 
			
		||||
  let naive = chrono::NaiveDateTime::from_timestamp_opt(timestamp(), 0).unwrap();
 | 
			
		||||
  let offset = Local::now().offset().fix();
 | 
			
		||||
  let date_time = DateTime::<Local>::from_naive_utc_and_offset(naive, offset);
 | 
			
		||||
  let date_string = format!("{}", date_time.format(format));
 | 
			
		||||
  let expected = format!(
 | 
			
		||||
    r#"Name,Price,Time,Status,Platform,is urgent,link,TODO,Last Modified,Created At,Related
 | 
			
		||||
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);
 | 
			
		||||
  assert_eq!(s, expected);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user