Commit ae8c6d67 by dongshufeng

refactor(all): add iesplan example

parent 7c0246df
......@@ -2421,7 +2421,7 @@ dependencies = [
[[package]]
name = "yew-bulma"
version = "0.1.0"
source = "git+https://github.com/shufengdong/yew-bulma.git#2bd9d5ddc5c77c9797e427f488b3baa329abde66"
source = "git+https://github.com/shufengdong/yew-bulma.git#5afd8092ec2ea27ba7f552a2cdbd325a68733dbf"
dependencies = [
"bytes",
"chrono",
......
use std::collections::HashMap;
use std::io::{Cursor, Write};
use std::path::Path;
use calamine::ReaderRef;
use calamine::{open_workbook_auto_from_rs, Data, Reader, Sheets, Xlsx, open_workbook_from_rs};
pub fn excel_to_csv_bytes<P: AsRef<Path>>(path: P) -> Option<Vec<Vec<u8>>> {
......@@ -9,7 +8,9 @@ pub fn excel_to_csv_bytes<P: AsRef<Path>>(path: P) -> Option<Vec<Vec<u8>>> {
excel_bytes_to_csv_bytes(bytes.as_slice())
}
pub fn get_first_sheet_merged_cells(bytes: Vec<u8>) -> Option<(u32, u32, HashMap<(u32,u32), (u32, u32)>)> {
// return: row count, col count, merged dimensions(key is start, value is end), values(key is i*n_j)
pub fn get_first_sheet_merged_cells(bytes: Vec<u8>)
-> Option<(u32, u32, HashMap<(u32,u32), (u32, u32)>, HashMap<usize, String>)> {
let c = Cursor::new(bytes);
let mut excel: Xlsx<_> = open_workbook_from_rs(c).ok()?;
excel.load_merged_regions().ok()?;
......@@ -24,10 +25,28 @@ pub fn get_first_sheet_merged_cells(bytes: Vec<u8>) -> Option<(u32, u32, HashMap
max_col = c.end.1;
}
}
let range = excel.worksheet_range_ref(&sheet_names[0]).ok()?;
let range = excel.worksheet_range(&sheet_names[0]).ok()?;
let max_col = max_col as usize;
let (m, w) = range.get_size();
let n = if w as u32 > max_col + 1 { w as u32 } else { max_col + 1 };
return Some((m as u32, n, merged_cells));
let n = if w > max_col + 1 { w } else { max_col + 1 };
let mut values = HashMap::with_capacity(m * n);
for (i, r) in range.rows().enumerate() {
for (j, c) in r.iter().enumerate() {
let key = i * n + j;
let value = match *c {
Data::Empty => String::new(),
Data::String(ref s) => format!("{s}"),
Data::Float(ref f) => format!("{f}"),
Data::DateTime(ref data) => format!("{data}"),
Data::DurationIso(ref s) | Data::DateTimeIso(ref s) => format!("{s}"),
Data::Int(ref i) => format!("{i}"),
Data::Error(ref e) => format!("{:?}", e),
Data::Bool(ref b) => format!("{b}"),
};
values.insert(key, value);
}
}
return Some((m as u32, n as u32, merged_cells, values));
}
None
}
......
0,car1,500
1,car1,500
100001,name1,Checkbox
100002,name2,Checkbox
100003,name3,Checkbox
100004,name4,Checkbox
100005,name5,Checkbox
......@@ -27,7 +27,6 @@ pub enum ParaType {
pub struct Parameters {
id: usize,
name: String,
height: usize,
labels: Vec<String>,
points: Vec<u64>,
para_types: Vec<ParaType>,
......@@ -41,7 +40,6 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
let record = records.next().unwrap().unwrap();
let id = csv_usize(&record, 0).unwrap();
let name = csv_string(&record, 1).unwrap();
let height = csv_usize(&record, 2).unwrap();
let mut labels = Vec::new();
let mut points = Vec::new();
let mut para_types = Vec::new();
......@@ -51,12 +49,13 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
labels.push(csv_string(&row, 1).unwrap());
para_types.push(ParaType::Checkbox);
}
Parameters { id, name, height, labels, points, para_types }
Parameters { id, name, labels, points, para_types }
}
pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> {
let (m, n, merge_map) = get_first_sheet_merged_cells(xlsx_bytes)?;
let mut class_vec = Vec::new();
let (m, n, merge_map, values) = get_first_sheet_merged_cells(xlsx_bytes)?;
let mut class_str = Vec::new();
let mut style_str = Vec::new();
let mut is_dealt = vec![false; (m * n) as usize];
for i in 0..m {
for j in 0..n {
......@@ -64,18 +63,18 @@ pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> {
if is_dealt[index] {
continue;
}
let mut class_str = "cell".to_string();
let mut class_s = "cell".to_string();
let coordinate = (i, j);
if let Some((end_row, end_col)) = merge_map.get(&coordinate) {
let row_span = *end_row - i + 1;
let col_span = *end_col - j + 1;
if row_span > 1 {
class_str.push_str(&format!(" is-row-span-{row_span}"))
class_s.push_str(&format!(" is-row-span-{row_span}"))
}
if col_span > 1 {
class_str.push_str(&format!(" is-col-span-{col_span}"))
class_s.push_str(&format!(" is-col-span-{col_span}"))
}
class_vec.push(class_str);
class_str.push(class_s);
for row in i..=*end_row {
for col in j..=*end_col {
let pos = (row * n + col) as usize;
......@@ -83,10 +82,13 @@ pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> {
}
}
} else {
class_vec.push(class_str);
class_str.push(class_s);
}
let h = values.get(&index).cloned().unwrap_or("100".to_string());
let s = format!("height:{h}px");
style_str.push(s);
}
}
let tiles = Tiles { id: "".to_string(), class_str: class_vec, with_box: true };
let tiles = Tiles { id: "".to_string(), class_str, style_str, with_box: true };
Some(tiles)
}
\ No newline at end of file
......@@ -30,9 +30,8 @@ impl Component for ParaCard {
let input_html = (0..paras.points.len()).map(|i| {
self.create_input(ctx, i)
}).collect::<Html>();
let height = format!("{}px", paras.height);
html! {
<Card height={height}>
<Card>
<CardHeader>
<p class="card-header-title">
{paras.name.clone()}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论