Skip to content

Commit

Permalink
allow to change codeName attribute in workbookPr (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvoid authored Feb 16, 2024
1 parent 0a22658 commit 6e2e98d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/structs/spreadsheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Spreadsheet {
properties: Properties,
work_sheet_collection: Vec<Worksheet>,
macros_code: Option<Vec<u8>>,
code_name: Option<String>,
ribbon_xml_data: Option<String>,
theme: Theme,
stylesheet: Stylesheet,
Expand Down Expand Up @@ -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<S: ToString>(&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 {
Expand Down
2 changes: 1 addition & 1 deletion src/writer/xlsx/workbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn write<W: io::Seek + io::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);

Expand Down

0 comments on commit 6e2e98d

Please sign in to comment.