-
Notifications
You must be signed in to change notification settings - Fork 250
feat: parse workbook properties (core + extended) #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f8e9e39
54719b6
ff64e2a
af315b4
75b06a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
| // Copyright 2016-2026, Johann Tuffe. | ||
|
|
||
| //! Demonstrates reading workbook properties from an XLSX or XLSB file. | ||
| //! | ||
| //! This example reads the core and extended document properties (such as | ||
| //! creator, application, and company) from a workbook. | ||
|
Comment on lines
+7
to
+8
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit the phrase "core and extended" here and on any public docs. They won't mean anything to most end users. |
||
| //! | ||
| //! Run the example like this: | ||
| //! | ||
| //! ```text | ||
| //! $ cargo run -q --example read_properties -- tests/issues.xlsx | ||
| //! | ||
| //! Core / Extended properties: | ||
| //! creator: Some("Johann Tuffe") | ||
| //! last_modified_by: Some("Johann Tuffe") | ||
| //! application: Some("Microsoft Excel") | ||
| //! company: Some("SOCIETE GENERALE") | ||
| //! ``` | ||
|
|
||
| use calamine::open_workbook_auto; | ||
| use std::env; | ||
| use std::process::exit; | ||
|
|
||
| fn main() { | ||
| let args: Vec<String> = env::args().collect(); | ||
| if args.len() < 2 { | ||
| eprintln!("Usage: {} <xlsx/xlsb path>", args[0]); | ||
| exit(1); | ||
| } | ||
|
|
||
| let path = &args[1]; | ||
| let mut excel = match open_workbook_auto(path) { | ||
| Ok(excel) => excel, | ||
| Err(e) => { | ||
| eprintln!("Cannot open {path}: {e}"); | ||
| exit(1); | ||
| } | ||
| }; | ||
|
|
||
| let props = match excel.workbook_properties() { | ||
| Ok(props) => props, | ||
| Err(e) => { | ||
| eprintln!("Cannot read workbook properties from {path}: {e}"); | ||
| exit(1); | ||
| } | ||
| }; | ||
|
|
||
| println!("Core / Extended properties:"); | ||
| println!(" creator: {:?}", props.creator); | ||
| println!(" last_modified_by: {:?}", props.last_modified_by); | ||
| println!(" created: {:?}", props.created); | ||
| println!(" modified: {:?}", props.modified); | ||
| println!(" title: {:?}", props.title); | ||
| println!(" application: {:?}", props.application); | ||
| println!(" app_version: {:?}", props.app_version); | ||
| println!(" company: {:?}", props.company); | ||
| println!(" template: {:?}", props.template); | ||
| println!(" manager: {:?}", props.manager); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,10 @@ use crate::utils::{ | |
| read_usize, | ||
| }; | ||
| use crate::vba::VbaProject; | ||
| use crate::xlsx::{read_app_properties, read_core_properties}; | ||
| use crate::{ | ||
| Cell, Data, HeaderRow, Metadata, Range, Reader, ReaderRef, Sheet, SheetType, SheetVisible, | ||
| WorkbookProperties, | ||
| }; | ||
|
|
||
| /// A Xlsb specific error | ||
|
|
@@ -160,6 +162,8 @@ pub struct Xlsb<RS> { | |
| formats: Vec<CellFormat>, | ||
| is_1904: bool, | ||
| metadata: Metadata, | ||
| /// Workbook document properties, parsed lazily on first access. | ||
| workbook_properties: Option<Box<WorkbookProperties>>, | ||
| #[cfg(feature = "picture")] | ||
| pictures: Option<Vec<(String, Vec<u8>)>>, | ||
| options: XlsbOptions, | ||
|
|
@@ -438,6 +442,24 @@ impl<RS: Read + Seek> Xlsb<RS> { | |
| ) | ||
| } | ||
|
|
||
| /// Get workbook document properties. | ||
| /// | ||
| /// Missing fields are returned as `None`. | ||
| pub fn workbook_properties(&mut self) -> Result<&WorkbookProperties, XlsbError> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this again, it is probably better to return
|
||
| if self.workbook_properties.is_none() { | ||
| let mut props = WorkbookProperties::default(); | ||
| read_core_properties(&mut self.zip, &mut props, &self.zip_path_cache) | ||
| .map_err(xlsx_error_to_xlsb)?; | ||
| read_app_properties(&mut self.zip, &mut props, &self.zip_path_cache) | ||
| .map_err(xlsx_error_to_xlsb)?; | ||
| self.workbook_properties = Some(Box::new(props)); | ||
| } | ||
| Ok(self | ||
| .workbook_properties | ||
| .as_deref() | ||
| .expect("workbook properties are initialized above")) | ||
| } | ||
|
|
||
| #[cfg(feature = "picture")] | ||
| fn read_pictures(&mut self) -> Result<(), XlsbError> { | ||
| let mut pics = Vec::new(); | ||
|
|
@@ -483,6 +505,7 @@ impl<RS: Read + Seek> Reader<RS> for Xlsb<RS> { | |
| formats: Vec::new(), | ||
| is_1904: false, | ||
| metadata: Metadata::default(), | ||
| workbook_properties: None, | ||
| #[cfg(feature = "picture")] | ||
| pictures: None, | ||
| options: XlsbOptions::default(), | ||
|
|
@@ -1053,3 +1076,16 @@ fn check_for_password_protected<RS: Read + Seek>(reader: &mut RS) -> Result<(), | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn xlsx_error_to_xlsb(e: crate::xlsx::XlsxError) -> XlsbError { | ||
| use crate::xlsx::XlsxError; | ||
| match e { | ||
| XlsxError::Io(e) => XlsbError::Io(e), | ||
| XlsxError::Zip(e) => XlsbError::Zip(e), | ||
| XlsxError::Vba(e) => XlsbError::Vba(e), | ||
| XlsxError::Xml(e) => XlsbError::Xml(e), | ||
| XlsxError::XmlAttr(e) => XlsbError::XmlAttr(e), | ||
| XlsxError::Encoding(e) => XlsbError::Xml(e.into()), | ||
| e => XlsbError::FileNotFound(e.to_string()), | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omit the phrase "core and extended" here.