mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
implement consumption of vt:lpwstr custom properties
This commit is contained in:
parent
42c9233ad9
commit
afbab819c4
|
@ -320,6 +320,23 @@ public:
|
|||
template <typename T = std::string>
|
||||
void extended_property(const std::string &property_name, const T value);
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the workbook has the custom property with the given name.
|
||||
/// </summary>
|
||||
bool has_custom_property(const std::string &property_name) const;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
template <typename T = std::string>
|
||||
T custom_property(const std::string &property_name) const;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
template <typename T = std::string>
|
||||
void custom_property(const std::string &property_name, const T value);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
|
|
@ -118,7 +118,7 @@ const std::unordered_map<std::string, std::string> &constants::namespaces()
|
|||
{"workbook", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"},
|
||||
{"core-properties", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"},
|
||||
{"extended-properties", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"},
|
||||
{"custom-properties", "http://schemas.openxmlformats.org/officeDocument/2006/customProperties"},
|
||||
{"custom-properties", "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"},
|
||||
|
||||
{"encryption", "http://schemas.microsoft.com/office/2006/encryption"},
|
||||
{"encryption-password", "http://schemas.microsoft.com/office/2006/keyEncryptor/password"},
|
||||
|
|
|
@ -61,7 +61,7 @@ std::string to_string(relationship_type t)
|
|||
case relationship_type::unknown:
|
||||
return "unknown";
|
||||
case relationship_type::custom_properties:
|
||||
return "custom-properties";
|
||||
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties";
|
||||
case relationship_type::printer_settings:
|
||||
return "printer-settings";
|
||||
case relationship_type::connections:
|
||||
|
|
|
@ -812,6 +812,8 @@ void xlsx_consumer::read_content_types()
|
|||
|
||||
void xlsx_consumer::read_properties(const path &part, const xml::qname &root)
|
||||
{
|
||||
static const auto xmlns_vt = constants::namespace_("vt");
|
||||
|
||||
auto content_type = manifest().content_type(part);
|
||||
|
||||
std::unordered_map<std::string, std::string> properties;
|
||||
|
@ -821,16 +823,36 @@ void xlsx_consumer::read_properties(const path &part, const xml::qname &root)
|
|||
{
|
||||
auto property_element = expect_start_element(xml::content::mixed);
|
||||
|
||||
if (property_element.name() != "Property")
|
||||
if (property_element.name() != "property")
|
||||
{
|
||||
properties[property_element.name()] = read_text();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto property_name = parser().attribute("name");
|
||||
auto variant_child_element = expect_start_element(xml::content::simple);
|
||||
|
||||
if (variant_child_element == xml::qname(xmlns_vt, "lpwstr"))
|
||||
{
|
||||
properties[property_name] = read_text();
|
||||
}
|
||||
|
||||
expect_end_element(variant_child_element);
|
||||
}
|
||||
|
||||
skip_remaining_content(property_element);
|
||||
expect_end_element(property_element);
|
||||
}
|
||||
|
||||
expect_end_element(root);
|
||||
|
||||
if (content_type == "application/vnd.openxmlformats-officedocument.custom-properties+xml")
|
||||
{
|
||||
for (const auto &prop : properties)
|
||||
{
|
||||
target_.custom_property(prop.first, prop.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xlsx_consumer::read_office_document(const std::string &content_type) // CT_Workbook
|
||||
|
|
|
@ -104,4 +104,12 @@ public:
|
|||
TS_ASSERT(ws.header_footer().has_footer(xlnt::header_footer::location::right));
|
||||
TS_ASSERT_EQUALS(ws.header_footer().footer(xlnt::header_footer::location::right).plain_text(), "right footer");
|
||||
}
|
||||
|
||||
void test_read_custom_properties()
|
||||
{
|
||||
xlnt::workbook wb;
|
||||
wb.load("data/21_custom_properties.xlsx");
|
||||
TS_ASSERT(wb.has_custom_property("Client"));
|
||||
TS_ASSERT_EQUALS(wb.custom_property("Client"), "me!");
|
||||
}
|
||||
};
|
||||
|
|
|
@ -165,6 +165,30 @@ std::string workbook::extended_property(const std::string &property_name) const
|
|||
return d_->extended_properties_.at(property_name);
|
||||
}
|
||||
|
||||
|
||||
bool workbook::has_custom_property(const std::string &property_name) const
|
||||
{
|
||||
return d_->custom_properties_.count(property_name) > 0;
|
||||
}
|
||||
|
||||
template <>
|
||||
void workbook::custom_property(const std::string &property_name, const std::string value)
|
||||
{
|
||||
d_->custom_properties_[property_name] = value;
|
||||
}
|
||||
|
||||
template <>
|
||||
void workbook::custom_property(const std::string &property_name, const char *value)
|
||||
{
|
||||
d_->custom_properties_[property_name] = value;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string workbook::custom_property(const std::string &property_name) const
|
||||
{
|
||||
return d_->custom_properties_.at(property_name);
|
||||
}
|
||||
|
||||
workbook workbook::empty()
|
||||
{
|
||||
auto impl = new detail::workbook_impl();
|
||||
|
|
Loading…
Reference in New Issue
Block a user