From 6e2e98da3c03a23e60502b52e7f3ee156fa60d83 Mon Sep 17 00:00:00 2001 From: nikvoid <77514082+nikvoid@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:58:28 +0300 Subject: [PATCH] allow to change `codeName` attribute in `workbookPr` (#179) --- src/structs/spreadsheet.rs | 22 ++++++++++++++++++++++ src/writer/xlsx/workbook.rs | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/structs/spreadsheet.rs b/src/structs/spreadsheet.rs index 70360cf7..36286bf5 100644 --- a/src/structs/spreadsheet.rs +++ b/src/structs/spreadsheet.rs @@ -22,6 +22,7 @@ pub struct Spreadsheet { properties: Properties, work_sheet_collection: Vec, macros_code: Option>, + code_name: Option, ribbon_xml_data: Option, theme: Theme, stylesheet: Stylesheet, @@ -272,6 +273,27 @@ impl Spreadsheet { self.macros_code.is_some() } + /// Set codeName property of workbook + /// + /// May be useful when importing VBA/macros code from another workbook + /// and only used when writing book with macros code + /// + /// Default one is `ThisWorkbook`. + /// + /// Excel often uses `Workbook________` (8 underscores). + pub fn set_code_name(&mut self, codename: S) -> &mut Self { + self.code_name = Some(codename.to_string()); + self + } + + /// Get codeName property of workbook + /// + /// Must to be the same in workbook with VBA/macros code from this workbook + /// for that code in Workbook object to work out of the box without adjustments + pub fn get_code_name(&self) -> Option<&str> { + self.code_name.as_deref() + } + /// (This method is crate only.) /// Get Stylesheet. pub(crate) fn get_stylesheet(&self) -> &Stylesheet { diff --git a/src/writer/xlsx/workbook.rs b/src/writer/xlsx/workbook.rs index c78bb663..645e5597 100644 --- a/src/writer/xlsx/workbook.rs +++ b/src/writer/xlsx/workbook.rs @@ -47,7 +47,7 @@ pub(crate) fn write( attributes.push(("filterPrivacy", "1")); //attributes.push(("defaultThemeVersion", "124226")); if spreadsheet.get_has_macros() { - attributes.push(("codeName", "ThisWorkbook")); + attributes.push(("codeName", &spreadsheet.get_code_name().unwrap_or("ThisWorkbook"))); } write_start_tag(&mut writer, "workbookPr", attributes, true);