Skip to content

Commit 21d862e

Browse files
author
Matéo Fernandez
committed
refactor(lib): update to an optional worksheet name
1 parent 8ec90c6 commit 21d862e

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

avro_schema.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"fields": [
1313
{
1414
"name": "name",
15-
"type": "string"
15+
"type": ["string", "null"],
16+
"default": null
1617
},
1718
{
1819
"name": "autofit",
@@ -46,10 +47,10 @@
4647
"type": "record",
4748
"name": "Format",
4849
"fields": [
49-
{"name": "fontName", "type": ["null", "string"], "default": null},
50-
{"name": "fontSize", "type": ["null", "int"], "default": null},
51-
{"name": "bold", "type": ["null", "boolean"], "default": null},
52-
{"name": "numFormat", "type": ["null", "string"], "default": null}
50+
{"name": "fontName", "type": ["string", "null"], "default": null},
51+
{"name": "fontSize", "type": ["int", "null"], "default": null},
52+
{"name": "bold", "type": ["boolean", "null"], "default": null},
53+
{"name": "numFormat", "type": ["string", "null"], "default": null}
5354
]
5455
}
5556
],

src/excel/workbook.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ impl WorkbookDto {
1616
let mut workbook = rust_xlsxwriter::Workbook::new();
1717

1818
for worksheet_data in self.worksheets.iter() {
19+
let worksheet = workbook.add_worksheet().set_formula_result_default("");
20+
21+
// set custom name if present
22+
let worksheet = match &worksheet_data.name {
23+
Some(custom_name) => worksheet.set_name(custom_name)?,
24+
None => worksheet,
25+
};
26+
1927
info!(
2028
"Add worksheet \"{}\" with {} cells to workbook",
21-
worksheet_data.name,
29+
worksheet.name(),
2230
worksheet_data.cells.len(),
2331
);
2432

25-
let worksheet = workbook
26-
.add_worksheet()
27-
.set_name(worksheet_data.name.clone())?
28-
.set_formula_result_default("");
29-
3033
for cell in worksheet_data.cells.iter() {
3134
if let Err(error) = cell.write_cell(worksheet) {
3235
error!("Could not write in cell, found: {:?}", error);
@@ -45,7 +48,7 @@ impl WorkbookDto {
4548
#[derive(Debug, Serialize, Deserialize, Builder)]
4649
pub struct WorksheetDto {
4750
#[builder(into)]
48-
pub name: String,
51+
pub name: Option<String>,
4952
pub autofit: bool,
5053
#[builder(default)]
5154
cells: Vec<Cell>,

src/parser/html.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl HtmlParser {
1818

1919
let mut worksheets: Vec<WorksheetDto> = Vec::new();
2020

21-
for (table_index, table) in document.select(&selectors.table).enumerate() {
21+
for table in document.select(&selectors.table) {
2222
let mut cells: Vec<Cell> = Vec::new();
2323

2424
for (row_index, row) in table.select(&selectors.row).enumerate() {
@@ -49,11 +49,7 @@ impl HtmlParser {
4949
}
5050
}
5151

52-
let worksheet = WorksheetDto::builder()
53-
.name(format!("Feuille {}", table_index + 1))
54-
.autofit(true)
55-
.cells(cells)
56-
.build();
52+
let worksheet = WorksheetDto::builder().autofit(true).cells(cells).build();
5753

5854
worksheets.push(worksheet);
5955
}

0 commit comments

Comments
 (0)