a little refactoring

pull/67/head
Thomas Fussell 2016-08-05 01:52:05 -04:00
parent 461b9fa106
commit cf80c46b66
54 changed files with 4665 additions and 5389 deletions

View File

@ -44,17 +44,24 @@ class font;
class format;
class number_format;
class protection;
class relationship;
class style;
class workbook;
class worksheet;
class xlsx_consumer;
class xlsx_producer;
struct date;
struct datetime;
struct time;
struct timedelta;
namespace detail { struct cell_impl; }
namespace detail {
class xlsx_consumer;
class xlsx_producer;
struct cell_impl;
} // namespace detail
/// <summary>
/// Describes cell associated properties.
@ -151,9 +158,9 @@ public:
// hyperlink
/// <summary>
/// Return a relationship representing this cell's hyperlink.
/// Return the URL of this cell's hyperlink.
/// </summary>
relationship get_hyperlink() const;
std::string get_hyperlink() const;
/// <summary>
/// Add a hyperlink to this cell pointing to the URI of the given value.
@ -183,6 +190,8 @@ public:
const format &get_format() const;
void set_format(const format &new_format);
void set_format_id(std::size_t format_id);
void clear_format();
@ -380,7 +389,8 @@ private:
// make these friends so they can use the private constructor
friend class style;
friend class worksheet;
friend class worksheet_serializer;
friend class detail::xlsx_consumer;
friend class detail::xlsx_producer;
friend struct detail::cell_impl;
void guess_type_and_set_value(const std::string &value);

View File

@ -1,53 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <xlnt/xlnt_config.hpp>
namespace xlnt {
/// <summary>
/// Default types in an OOXML package are identified by their extension.
/// All files in the package with this extension will be assigned the given
/// content type unless an override type for the exact file is provided.
/// </summary>
class XLNT_CLASS default_type
{
public:
default_type();
default_type(const std::string &extension, const std::string &content_type);
default_type(const default_type &other);
default_type &operator=(const default_type &other);
std::string get_extension() const;
std::string get_content_type() const;
private:
std::string extension_;
std::string content_type_;
};
} // namespace xlnt

View File

@ -27,37 +27,236 @@
#include <unordered_map>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/default_type.hpp>
#include <xlnt/packaging/override_type.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/utils/path.hpp>
namespace xlnt {
enum class content_type
{
core_properties,
extended_properties,
custom_properties,
calculation_chain,
chartsheet,
comments,
connections,
custom_property,
custom_xml_mappings,
dialogsheet,
drawings,
external_workbook_references,
metadata,
pivot_table,
pivot_table_cache_definition,
pivot_table_cache_records,
query_table,
shared_string_table,
shared_workbook_revision_headers,
shared_workbook,
revision_log,
shared_workbook_user_data,
single_cell_table_definitions,
styles,
table_definition,
theme,
volatile_dependencies,
workbook,
worksheet,
unknown
};
/// <summary>
/// The manifest keeps track of all files the OOXML package and
/// their type.
/// The manifest keeps track of all files in the OOXML package and
/// their type and relationships.
/// </summary>
class XLNT_CLASS manifest
{
public:
using default_types_container = std::unordered_map<std::string, default_type>;
using override_types_container = std::unordered_map<path, override_type, std::hash<hashable>>;
/// <summary>
/// Convenience method for clear_types() and clear_relationships()
/// </summary>
void clear();
bool has_default_type(const std::string &extension) const;
std::string get_default_type(const std::string &extension) const;
const default_types_container &get_default_types() const;
void add_default_type(const std::string &extension, const std::string &content_type);
/// <summary>
/// Convenince method for clear_default_types() and clear_override_types()
/// </summary>
void clear_types();
bool has_override_type(const path &part) const;
std::string get_override_type(const path &part) const;
const override_types_container &get_override_types() const;
void add_override_type(const path &part, const std::string &content_type);
/// <summary>
/// Unregisters every default content type (i.e. type based on part extension).
/// </summary>
void clear_default_types();
/// <summary>
/// Unregisters every content type for every part except default types.
/// </summary>
void clear_override_types();
/// <summary>
/// Convenince method for clear_package_relationships() and clear_part_relationships()
/// </summary>
void clear_relationships();
/// <summary>
/// Unregisters every relationship with the package root as the source.
/// </summary>
void clear_package_relationships();
/// <summary>
/// Unregisters every relationship except those with the package root as the source.
/// </summary>
void clear_part_relationships();
/// <summary>
/// Returns true if the manifest contains a package relationship with the given type.
/// </summary>
bool has_package_relationship(relationship::type type) const;
/// <summary>
/// Returns true if the manifest contains a relationship with the given type with part as the source.
/// </summary>
bool has_part_relationship(const path &part, relationship::type type) const;
relationship get_package_relationship(relationship::type type) const;
std::vector<relationship> get_package_relationships(relationship::type type) const;
relationship get_part_relationship(const path &part, relationship::type type) const;
std::vector<relationship> get_part_relationships(const path &part, relationship::type type) const;
/// <summary>
/// Given the path to a part, returns the content type of the part as
/// a content_type enum value or content_type::unknown if it isn't known.
/// </summary>
content_type get_part_content_type(const path &part) const;
/// <summary>
/// Given the path to a part, returns the content type of the part as a string.
/// </summary>
std::string get_part_content_type_string(const path &part) const;
/// <summary>
/// Registers a part of the given type at the standard path and creates a
/// relationship with the new part as a target of "source". The type of the
/// relationship is inferred based on the provided types.
/// </summary>
void register_part(const path &part, const path &parent, const std::string &content_type, relationship::type relation);
/// <summary>
///
/// </summary>
void register_part(const path &parent, const relationship &rel, const std::string &content_type);
/// <summary>
/// Registers a package part of the given type at the standard path and creates a
/// relationship with the package root as the source. The type of the
/// relationship is inferred based on the provided type.
/// </summary>
void register_package_part(const path &part, const std::string &content_type, relationship::type relation);
/// <summary>
/// Unregisters the path of the part of the given type and removes all relationships
/// with the part as a source or target.
/// </summary>
void unregister_part(const path &part);
/// <summary>
/// Returns true if the part at the given path has been registered in this manifest.
/// </summary>
bool has_part(const path &part) const;
/// <summary>
/// Returns the path of every registered part in this manifest.
/// </summary>
std::vector<path> get_parts() const;
/// <summary>
/// Returns every registered relationship with the package root as the source.
/// </summary>
std::vector<relationship> get_package_relationships() const;
/// <summary>
/// Returns the package relationship with the given ID.
/// </summary>
relationship get_package_relationship(const std::string &rel_id);
/// <summary>
/// Returns every registered relationship with the part of the given type
/// as the source.
/// </summary>
std::vector<relationship> get_part_relationships(const path &part) const;
/// <summary>
/// Returns the registered relationship with the part of the given type
/// as the source and the ID matching the provided ID.
/// </summary>
relationship get_part_relationship(const path &part, const std::string &rel_id) const;
/// <summary>
///
/// </summary>
std::string register_external_package_relationship(relationship::type type, const std::string &target_uri);
/// <summary>
///
/// </summary>
std::string register_external_package_relationship(relationship::type type, const std::string &target_uri, const std::string &rel_id);
/// <summary>
///
/// </summary>
std::string register_external_relationship(const path &source_part, relationship::type type, const std::string &target_uri);
/// <summary>
///
/// </summary>
std::string register_external_relationship(const path &source_part, relationship::type type, const std::string &target_uri, const std::string &rel_id);
/// <summary>
/// Returns true if a default content type for the extension has been registered.
/// </summary>
bool has_default_type(const std::string &extension) const;
/// <summary>
/// Returns a vector of all extensions with registered default content types.
/// </summary>
std::vector<std::string> get_default_type_extensions() const;
/// <summary>
/// Returns the registered default content type for parts of the given extension.
/// </summary>
std::string get_default_type(const std::string &extension) const;
/// <summary>
/// Associates the given extension with the given content type.
/// </summary>
void register_default_type(const std::string &extension, const std::string &content_type);
/// <summary>
/// Unregisters the default content type for the given extension.
/// </summary>
void unregister_default_type(const std::string &extension);
private:
default_types_container default_types_;
override_types_container override_types_;
struct part_info
{
std::string content_type;
std::vector<relationship> relationships;
};
std::string next_package_relationship_id() const;\
std::string next_relationship_id(const path &part) const;
std::unordered_map<path, part_info, std::hash<hashable>> part_infos_;
std::vector<relationship> package_relationships_;
std::unordered_map<std::string, std::string> extension_content_type_map_;
};
} // namespace xlnt

View File

@ -1,53 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/utils/path.hpp>
namespace xlnt {
/// <summary>
/// An override_type applies a different content type to a file which
/// already has a default content type for its extension.
/// </summary>
class XLNT_CLASS override_type
{
public:
override_type();
override_type(const path &part, const std::string &content_type);
override_type(const override_type &other);
override_type &operator=(const override_type &other);
path get_part() const;
std::string get_content_type() const;
private:
path part_;
std::string content_type_;
};
} // namespace xlnt

View File

@ -26,6 +26,7 @@
#include <string>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/utils/path.hpp>
namespace xlnt {
@ -34,34 +35,61 @@ namespace xlnt {
/// </summary>
enum class XLNT_CLASS target_mode
{
/// <summary>
/// The relationship references a resource that is external to the package.
/// </summary>
internal,
/// <summary>
/// The relationship references a part that is inside the package.
/// </summary>
external,
/// <summary>
/// The relationship references a resource that is external to the package.
/// </summary>
internal
external
};
/// <summary>
/// All package relationships must be one of these defined types.
/// </summary>
enum class relationship_type
enum class XLNT_CLASS relationship_type
{
invalid,
hyperlink,
drawing,
worksheet,
chartsheet,
shared_strings,
styles,
theme,
extended_properties,
core_properties,
office_document,
custom_xml,
thumbnail
unknown,
// Package
core_properties,
extended_properties,
custom_properties,
office_document,
thumbnail,
printer_settings,
// SpreadsheetML
calculation_chain,
chartsheet,
comments,
connections,
custom_property,
custom_xml_mappings,
dialogsheet,
drawings,
external_workbook_references,
metadata,
pivot_table,
pivot_table_cache_definition,
pivot_table_cache_records,
query_table,
shared_string_table,
shared_workbook_revision_headers,
shared_workbook,
theme,
revision_log,
shared_workbook_user_data,
single_cell_table_definitions,
styles,
table_definition,
volatile_dependencies,
workbook,
worksheet,
hyperlink,
image
};
/// <summary>
@ -73,47 +101,44 @@ class XLNT_CLASS relationship
public:
using type = relationship_type;
static type type_from_string(const std::string &type_string);
static std::string type_to_string(type t);
relationship();
relationship(const std::string &t, const std::string &r_id = "", const std::string &target_uri = "");
relationship(type t, const std::string &r_id = "", const std::string &target_uri = "");
relationship(const std::string &id, type t, const path &target, target_mode mode);
/// <summary>
/// gets a string that identifies the relationship.
/// Returns a string of the form rId# that identifies the relationship.
/// </summary>
std::string get_id() const;
/// <summary>
/// gets the URI of the package or part that owns the relationship.
/// </summary>
std::string get_source_uri() const;
/// <summary>
/// Returns the type of this relationship.
/// </summary>
type get_type() const;
/// <summary>
/// gets a value that indicates whether the target of the relationship is or External to the Package.
/// Returns whether the target of the relationship is internal or external to the package.
/// </summary>
target_mode get_target_mode() const;
/// <summary>
/// gets the URI of the target resource of the relationship.
/// Returns the URI of the package part this relationship points to.
/// </summary>
std::string get_target_uri() const;
type get_type() const;
std::string get_type_string() const;
path get_target_uri() const;
/// <summary>
/// Returns true if and only if rhs is equal to this relationship.
/// </summary>
bool operator==(const relationship &rhs) const;
/// <summary>
/// Returns true if and only if rhs is not equal to this relationship.
/// </summary>
bool operator!=(const relationship &rhs) const;
private:
std::string id_;
type type_;
std::string id_;
std::string source_uri_;
std::string target_uri_;
path target_uri_;
target_mode target_mode_;
};

View File

@ -106,7 +106,12 @@ public:
/// <summary>
/// Return the last component of this path.
/// </summary>
std::string basename();
std::string basename() const;
/// <summary>
/// Return the part of the path following the last . in the basename.
/// </summary>
std::string extension() const;
// conversion

View File

@ -68,7 +68,10 @@ enum class calendar;
enum class relationship_type;
namespace detail {
struct stylesheet;
struct workbook_impl;
class xlsx_consumer;
class xlsx_producer;
} // namespace detail
/// <summary>
@ -183,14 +186,14 @@ public:
/// This may throw an exception if the sheet isn't found.
/// Use workbook::contains(const std::string &) to make sure the sheet exists.
/// </summary>
worksheet get_sheet_by_name(const std::string &sheet_name);
worksheet get_sheet_by_title(const std::string &sheet_name);
/// <summary>
/// Return the const worksheet with the given name.
/// This may throw an exception if the sheet isn't found.
/// Use workbook::contains(const std::string &) to make sure the sheet exists.
/// </summary>
const worksheet get_sheet_by_name(const std::string &sheet_name) const;
const worksheet get_sheet_by_title(const std::string &sheet_name) const;
/// <summary>
/// Return the worksheet at the given index.
@ -202,6 +205,16 @@ public:
/// </summary>
const worksheet get_sheet_by_index(std::size_t index) const;
/// <summary>
/// Return the worksheet with a sheetId of id.
/// </summary>
worksheet get_sheet_by_id(std::size_t id);
/// <summary>
/// Return the const worksheet with a sheetId of id.
/// </summary>
const worksheet get_sheet_by_id(std::size_t id) const;
/// <summary>
/// Return true if this workbook contains a sheet with the given name.
/// </summary>
@ -360,15 +373,15 @@ public:
// serialization
bool save(std::vector<std::uint8_t> &data);
bool save(const std::string &filename);
bool save(const xlnt::path &filename);
bool save(std::ostream &stream);
void save(std::vector<std::uint8_t> &data);
void save(const std::string &filename);
void save(const xlnt::path &filename);
void save(std::ostream &stream);
bool load(const std::vector<std::uint8_t> &data);
bool load(const std::string &filename);
bool load(const xlnt::path &filename);
bool load(std::istream &stream);
void load(const std::vector<std::uint8_t> &data);
void load(const std::string &filename);
void load(const xlnt::path &filename);
void load(std::istream &stream);
void set_code_name(const std::string &code_name);
@ -396,22 +409,12 @@ public:
style &create_style(const std::string &name);
std::size_t add_style(const style &new_style);
void clear_styles();
const std::vector<style> &get_styles() const;
// manifest
manifest &get_manifest();
const manifest &get_manifest() const;
// relationships
void create_relationship(const std::string &id, const std::string &target, relationship_type type);
relationship get_relationship(const std::string &id) const;
const std::vector<relationship> &get_relationships() const;
void create_root_relationship(const std::string &id, const std::string &target, relationship_type type);
const std::vector<relationship> &get_root_relationships() const;
// shared strings
void add_shared_string(const text &shared, bool allow_duplicates=false);
@ -454,15 +457,15 @@ public:
bool operator!=(const workbook &rhs) const;
private:
friend class excel_serializer;
friend class worksheet;
friend class detail::xlsx_consumer;
friend class detail::xlsx_producer;
workbook(detail::workbook_impl *impl);
/// <summary>
/// Get the name of the next unused relationship.
/// </summary>
std::string next_relationship_id() const;
detail::workbook_impl &impl();
const detail::workbook_impl &impl() const;
/// <summary>
/// Apply the function "f" to every cell in every worksheet in this workbook.

View File

@ -138,13 +138,6 @@ public:
column_t get_highest_column() const;
range_reference calculate_dimension() const;
// relationships
relationship create_relationship(relationship::type type, const std::string &target_uri);
const std::vector<relationship> &get_relationships() const;
// charts
// void add_chart(chart chart);
// cell merge
void merge_cells(const std::string &reference_string);
void merge_cells(const range_reference &reference);

View File

@ -35,9 +35,7 @@
#include <xlnt/cell/text_run.hpp>
// packaging
#include <xlnt/packaging/default_type.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/packaging/override_type.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/packaging/zip_file.hpp>

View File

@ -420,14 +420,14 @@ std::string cell::to_repr() const
return "<Cell " + worksheet(d_->parent_).get_title() + "." + get_reference().to_string() + ">";
}
relationship cell::get_hyperlink() const
std::string cell::get_hyperlink() const
{
if (!d_->has_hyperlink_)
{
throw std::runtime_error("no hyperlink set");
}
return d_->hyperlink_;
return d_->hyperlink_;
}
bool cell::has_hyperlink() const
@ -443,7 +443,7 @@ void cell::set_hyperlink(const std::string &hyperlink)
}
d_->has_hyperlink_ = true;
d_->hyperlink_ = worksheet(d_->parent_).create_relationship(relationship::type::hyperlink, hyperlink);
d_->hyperlink_ = hyperlink;
if (get_data_type() == type::null)
{
@ -934,6 +934,12 @@ void cell::set_format(const format &new_format)
d_->has_format_ = true;
}
void cell::set_format_id(std::size_t format_id)
{
d_->format_id_ = format_id;
d_->has_format_ = true;
}
calendar cell::get_base_date() const
{
return get_workbook().get_base_date();

View File

@ -656,6 +656,6 @@ public:
TS_ASSERT_THROWS(cell.set_hyperlink(""), std::runtime_error);
cell.set_hyperlink("http://example.com");
TS_ASSERT(cell.has_hyperlink());
TS_ASSERT_EQUALS(cell.get_hyperlink().get_target_uri(), "http://example.com");
TS_ASSERT_EQUALS(cell.get_hyperlink(), "http://example.com");
}
};

View File

@ -66,7 +66,7 @@ struct cell_impl
std::string formula_;
bool has_hyperlink_;
relationship hyperlink_;
std::string hyperlink_;
bool is_merged_;

View File

@ -1,34 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <detail/comment_serializer.hpp>
namespace xlnt {
namespace detail {
comment_serializer::comment_serializer(worksheet sheet) : sheet_(sheet)
{
}
} // namespace detail
} // namespace xlnt

View File

@ -1,57 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <vector>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/cell/comment.hpp>
#include <xlnt/worksheet/worksheet.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
namespace detail {
/// <summary>
/// Manages converting comments to and from XML.
/// </summary>
class XLNT_CLASS comment_serializer
{
comment_serializer(worksheet sheet);
void read_comments(const pugi::xml_document &xml);
void read_comments_vml(const pugi::xml_document &xml);
void write_comments(pugi::xml_document &xml) const;
void write_comments_vml(pugi::xml_document &xml) const;
private:
worksheet sheet_;
};
} // namespace detail
} // namespace xlnt

View File

@ -1,383 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <algorithm>
#include <iterator>
#include <pugixml.hpp>
#include <detail/constants.hpp>
#include <detail/excel_serializer.hpp>
#include <detail/manifest_serializer.hpp>
#include <detail/relationship_serializer.hpp>
#include <detail/shared_strings_serializer.hpp>
#include <detail/style_serializer.hpp>
#include <detail/stylesheet.hpp>
#include <detail/theme_serializer.hpp>
#include <detail/workbook_impl.hpp>
#include <detail/workbook_serializer.hpp>
#include <detail/worksheet_serializer.hpp>
#include <xlnt/cell/text.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/style.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/workbook/worksheet_iterator.hpp>
#include <xlnt/worksheet/range_iterator.hpp>
#include <xlnt/worksheet/worksheet.hpp>
namespace {
std::string::size_type find_string_in_string(const std::string &string, const std::string &substring)
{
std::string::size_type possible_match_index = string.find(substring.at(0));
while (possible_match_index != std::string::npos)
{
if (string.substr(possible_match_index, substring.size()) == substring)
{
return possible_match_index;
}
possible_match_index = string.find(substring.at(0), possible_match_index + 1);
}
return possible_match_index;
}
bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xlnt::workbook &wb, xlnt::detail::stylesheet &stylesheet)
{
wb.clear();
if(!archive.has_file(xlnt::constants::part_content_types()))
{
throw xlnt::invalid_file("missing [Content Types].xml");
}
xlnt::manifest_serializer ms(wb.get_manifest());
pugi::xml_document manifest_xml;
manifest_xml.load(archive.read(xlnt::constants::part_content_types()).c_str());
ms.read_manifest(manifest_xml);
if (ms.determine_document_type() != "excel")
{
throw xlnt::invalid_file("package is not an OOXML SpreadsheetML");
}
if(archive.has_file(xlnt::constants::part_core()))
{
xlnt::workbook_serializer workbook_serializer_(wb);
pugi::xml_document core_properties_xml;
core_properties_xml.load(archive.read(xlnt::constants::part_core()).c_str());
workbook_serializer_.read_properties_core(core_properties_xml);
}
if(archive.has_file(xlnt::constants::part_app()))
{
xlnt::workbook_serializer workbook_serializer_(wb);
pugi::xml_document app_properties_xml;
app_properties_xml.load(archive.read(xlnt::constants::part_app()).c_str());
workbook_serializer_.read_properties_app(app_properties_xml);
}
xlnt::relationship_serializer relationship_serializer_(archive);
auto root_relationships = relationship_serializer_.read_relationships("");
for (const auto &relationship : root_relationships)
{
wb.create_root_relationship(relationship.get_id(), relationship.get_target_uri(), relationship.get_type());
}
auto workbook_relationships = relationship_serializer_.read_relationships(xlnt::constants::part_workbook().to_string('/'));
for (const auto &relationship : workbook_relationships)
{
wb.create_relationship(relationship.get_id(), relationship.get_target_uri(), relationship.get_type());
}
pugi::xml_document xml;
xml.load(archive.read(xlnt::constants::part_workbook()).c_str());
auto root_node = xml.child("workbook");
auto workbook_pr_node = root_node.child("workbookPr");
if (workbook_pr_node.attribute("date1904"))
{
std::string value = workbook_pr_node.attribute("date1904").value();
if (value == "1" || value == "true")
{
wb.set_base_date(xlnt::calendar::mac_1904);
}
}
if(archive.has_file(xlnt::constants::part_shared_strings()))
{
std::vector<xlnt::text> shared_strings;
pugi::xml_document shared_strings_xml;
shared_strings_xml.load(archive.read(xlnt::constants::part_shared_strings()).c_str());
xlnt::shared_strings_serializer::read_shared_strings(shared_strings_xml, shared_strings);
for (auto &shared_string : shared_strings)
{
wb.add_shared_string(shared_string, true);
}
}
xlnt::style_serializer style_serializer(stylesheet);
pugi::xml_document style_xml;
style_xml.load(archive.read(xlnt::constants::part_styles()).c_str());
style_serializer.read_stylesheet(style_xml);
for (auto sheet_node : root_node.child("sheets").children())
{
auto rel = wb.get_relationship(sheet_node.attribute("r:id").value());
// TODO impelement chartsheets
if(rel.get_type() == xlnt::relationship::type::chartsheet)
{
continue;
}
auto ws = wb.create_sheet_with_rel(sheet_node.attribute("name").value(), rel);
ws.set_id(static_cast<std::size_t>(sheet_node.attribute("sheetId").as_ullong()));
xlnt::worksheet_serializer worksheet_serializer(ws);
pugi::xml_document worksheet_xml;
auto target_uri = xlnt::constants::package_xl().append(rel.get_target_uri());
worksheet_xml.load(archive.read(target_uri).c_str());
worksheet_serializer.read_worksheet(worksheet_xml, stylesheet);
}
if (archive.has_file(xlnt::path("docProps/thumbnail.jpeg")))
{
auto thumbnail_data = archive.read(xlnt::path("docProps/thumbnail.jpeg"));
wb.set_thumbnail(std::vector<std::uint8_t>(thumbnail_data.begin(), thumbnail_data.end()));
}
return true;
}
} // namespace
namespace xlnt {
const std::string excel_serializer::central_directory_signature()
{
return "\x50\x4b\x05\x06";
}
std::string excel_serializer::repair_central_directory(const std::string &original)
{
auto pos = find_string_in_string(original, central_directory_signature());
if (pos != std::string::npos)
{
return original.substr(0, pos + 22);
}
return original;
}
bool excel_serializer::load_stream_workbook(std::istream &stream, bool guess_types, bool data_only)
{
std::vector<std::uint8_t> bytes;
//TODO: inefficient?
while (stream.good())
{
bytes.push_back(static_cast<std::uint8_t>(stream.get()));
}
return load_virtual_workbook(bytes, guess_types, data_only);
}
bool excel_serializer::load_workbook(const path &filename, bool guess_types, bool data_only)
{
try
{
archive_.load(filename);
}
catch (std::runtime_error)
{
throw invalid_file(filename.to_string());
}
return ::load_workbook(archive_, guess_types, data_only, workbook_, get_stylesheet());
}
bool excel_serializer::load_virtual_workbook(const std::vector<std::uint8_t> &bytes, bool guess_types, bool data_only)
{
archive_.load(bytes);
return ::load_workbook(archive_, guess_types, data_only, workbook_, get_stylesheet());
}
excel_serializer::excel_serializer(workbook &wb) : workbook_(wb)
{
}
void excel_serializer::write_data(bool /*as_template*/)
{
relationship_serializer relationship_serializer_(archive_);
relationship_serializer_.write_relationships(workbook_.get_root_relationships(), "");
relationship_serializer_.write_relationships(workbook_.get_relationships(), constants::part_workbook().to_string('/'));
pugi::xml_document properties_app_xml;
workbook_serializer workbook_serializer_(workbook_);
workbook_serializer_.write_properties_app(properties_app_xml);
{
std::ostringstream ss;
properties_app_xml.save(ss);
archive_.write_string(ss.str(), constants::part_app());
}
pugi::xml_document properties_core_xml;
workbook_serializer_.write_properties_core(properties_core_xml);
{
std::ostringstream ss;
properties_core_xml.save(ss);
archive_.write_string(ss.str(), constants::part_core());
}
pugi::xml_document theme_xml;
theme_serializer theme_serializer_;
theme_serializer_.write_theme(workbook_.get_theme(), theme_xml);
{
std::ostringstream ss;
theme_xml.save(ss);
archive_.write_string(ss.str(), constants::part_theme());
}
if (!workbook_.get_shared_strings().empty())
{
const auto &strings = workbook_.get_shared_strings();
pugi::xml_document shared_strings_xml;
shared_strings_serializer::write_shared_strings(strings, shared_strings_xml);
std::ostringstream ss;
shared_strings_xml.save(ss);
archive_.write_string(ss.str(), constants::part_shared_strings());
}
pugi::xml_document workbook_xml;
workbook_serializer_.write_workbook(workbook_xml);
{
std::ostringstream ss;
workbook_xml.save(ss);
archive_.write_string(ss.str(), constants::part_workbook());
}
style_serializer style_serializer(workbook_.d_->stylesheet_);
pugi::xml_document style_xml;
style_serializer.write_stylesheet(style_xml);
{
std::ostringstream ss;
style_xml.save(ss);
archive_.write_string(ss.str(), constants::part_styles());
}
manifest_serializer manifest_serializer_(workbook_.get_manifest());
pugi::xml_document manifest_xml;
manifest_serializer_.write_manifest(manifest_xml);
{
std::ostringstream ss;
manifest_xml.save(ss);
archive_.write_string(ss.str(), constants::part_content_types());
}
write_worksheets();
if(!workbook_.get_thumbnail().empty())
{
const auto &thumbnail = workbook_.get_thumbnail();
archive_.write_string(std::string(thumbnail.begin(), thumbnail.end()), path("docProps/thumbnail.jpeg"));
}
}
void excel_serializer::write_worksheets()
{
std::size_t index = 1;
for (const auto ws : workbook_)
{
auto target = "worksheets/sheet" + std::to_string(index++) + ".xml";
for (const auto &rel : workbook_.get_relationships())
{
if (rel.get_target_uri() != target) continue;
worksheet_serializer serializer_(ws);
path ws_path(rel.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "", '/');
ws_path.append(rel.get_target_uri());
std::ostringstream ss;
pugi::xml_document worksheet_xml;
serializer_.write_worksheet(worksheet_xml);
worksheet_xml.save(ss);
archive_.write_string(ss.str(), ws_path);
break;
}
}
}
void excel_serializer::write_external_links()
{
}
bool excel_serializer::save_stream_workbook(std::ostream &stream, bool as_template)
{
write_data(as_template);
archive_.save(stream);
return true;
}
bool excel_serializer::save_workbook(const path &filename, bool as_template)
{
write_data(as_template);
archive_.save(filename);
return true;
}
bool excel_serializer::save_virtual_workbook(std::vector<std::uint8_t> &bytes, bool as_template)
{
write_data(as_template);
archive_.save(bytes);
return true;
}
detail::stylesheet &excel_serializer::get_stylesheet()
{
return workbook_.d_->stylesheet_;
}
} // namespace xlnt

View File

@ -1,185 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <cstdint>
#include <ostream>
#include <string>
#include <vector>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/zip_file.hpp>
#include <detail/stylesheet.hpp>
namespace xlnt {
class workbook;
/// <summary>
/// Handles reading and writing workbooks from an actual XLSX archive
/// using other serializers.
/// </summary>
class XLNT_CLASS excel_serializer
{
public:
/// <summary>
///
/// </summary>
static const std::string central_directory_signature();
/// <summary>
///
/// </summary>
static std::string repair_central_directory(const std::string &original);
/// <summary>
/// Construct an excel_serializer which operates on wb.
/// </summary>
excel_serializer(workbook &wb);
/// <summary>
/// Create a ZIP file in memory, load archive from filename, then populate workbook
/// with data from archive.
/// </summary>
bool load_workbook(const path &filename, bool guess_types = false, bool data_only = false);
/// <summary>
/// Create a ZIP file in memory, load archive from stream, then populate workbook
/// with data from archive.
/// </summary>
bool load_stream_workbook(std::istream &stream, bool guess_types = false, bool data_only = false);
/// <summary>
/// Create a ZIP file in memory, load archive from bytes, then populate workbook
/// with data from archive.
/// </summary>
bool load_virtual_workbook(
const std::vector<std::uint8_t> &bytes, bool guess_types = false, bool data_only = false);
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then save archive
/// to filename.
/// </summary>
bool save_workbook(const path &filename, bool as_template = false);
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then assign ZIP file
/// binary data to bytes.
/// </summary>
bool save_virtual_workbook(std::vector<std::uint8_t> &bytes, bool as_template = false);
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then copy ZIP file
/// binary data to stream.
/// </summary>
bool save_stream_workbook(std::ostream &stream, bool as_template = false);
xlnt::detail::stylesheet &get_stylesheet();
private:
/// <summary>
/// Reads all files in archive and populates workbook with associated data
/// using other appropriate serializers such as workbook_serializer.
/// </summary>
void read_data(bool guess_types, bool data_only);
/// <summary>
/// Read xl/sharedStrings.xml from internal archive and add shared strings to workbook.
/// </summary>
void read_shared_strings();
/// <summary>
///
/// </summary>
void read_images();
/// <summary>
///
/// </summary>
void read_charts();
/// <summary>
///
/// </summary>
void read_chartsheets();
/// <summary>
///
/// </summary>
void read_worksheets();
/// <summary>
///
/// </summary>
void read_external_links();
/// <summary>
/// Write all files needed to create a valid XLSX file which represents all
/// data contained in workbook.
/// </summary>
void write_data(bool as_template);
/// <summary>
/// Write xl/sharedStrings.xml to internal archive based on shared strings in workbook.
/// </summary>
void write_shared_strings();
/// <summary>
///
/// </summary>
void write_images();
/// <summary>
///
/// </summary>
void write_charts();
/// <summary>
///
/// </summary>
void write_chartsheets();
/// <summary>
///
/// </summary>
void write_worksheets();
/// <summary>
///
/// </summary>
void write_external_links();
/// <summary>
/// A reference to the workbook which is the object of read/write operations.
/// </summary>
workbook &workbook_;
/// <summary>
/// The internal archive which holds files representing workbook_ during
/// read/write operations.
/// </summary>
zip_file archive_;
};
} // namespace xlnt

View File

@ -1,97 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <pugixml.hpp>
#include <detail/constants.hpp>
#include <detail/manifest_serializer.hpp>
#include <xlnt/packaging/manifest.hpp>
namespace xlnt {
manifest_serializer::manifest_serializer(manifest &m) : manifest_(m)
{
}
void manifest_serializer::read_manifest(const pugi::xml_document &xml)
{
const auto root_node = xml.child("Types");
for (const auto child : root_node.children())
{
if (child.name() == std::string("Default"))
{
manifest_.add_default_type(child.attribute("Extension").value(), child.attribute("ContentType").value());
}
else if (child.name() == std::string("Override"))
{
manifest_.add_override_type(path(child.attribute("PartName").value()), child.attribute("ContentType").value());
}
}
}
void manifest_serializer::write_manifest(pugi::xml_document &xml) const
{
auto root_node = xml.append_child("Types");
root_node.append_attribute("xmlns").set_value(constants::get_namespace("content-types").c_str());
for (const auto default_type : manifest_.get_default_types())
{
auto type_node = root_node.append_child("Default");
type_node.append_attribute("Extension").set_value(default_type.second.get_extension().c_str());
type_node.append_attribute("ContentType").set_value(default_type.second.get_content_type().c_str());
}
for (const auto override_type : manifest_.get_override_types())
{
auto type_node = root_node.append_child("Override");
type_node.append_attribute("PartName").set_value(override_type.second.get_part().to_string('/').c_str());
type_node.append_attribute("ContentType").set_value(override_type.second.get_content_type().c_str());
}
}
std::string manifest_serializer::determine_document_type() const
{
for (auto current_override_type : manifest_.get_override_types())
{
auto type = current_override_type.second.get_content_type();
if (type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
{
return "excel";
}
else if (type == "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml")
{
return "powerpoint";
}
else if (type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")
{
return "word";
}
}
return "unsupported";
}
} // namespace xlnt

View File

@ -1,55 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <xlnt/xlnt_config.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
class manifest;
/// <summary>
/// Manages converting a manifest to and from XML.
/// </summary>
class XLNT_CLASS manifest_serializer
{
public:
manifest_serializer(manifest &m);
void read_manifest(const pugi::xml_document &xml);
void write_manifest(pugi::xml_document &xml) const;
std::string determine_document_type() const;
private:
manifest &manifest_;
};
}; // namespace xlnt

View File

@ -1,108 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <pugixml.hpp>
#include <sstream>
#include <detail/constants.hpp>
#include <detail/relationship_serializer.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/packaging/zip_file.hpp>
namespace {
std::string make_rels_name(const std::string &target)
{
const char sep = '/';
if (target.empty())
{
return xlnt::constants::part_root_relationships().to_string();
}
auto sep_pos = target.find_last_of(sep);
auto path = target.substr(0, sep_pos + 1);
auto filename = target.substr(sep_pos + 1);
return path + "_rels/" + filename + ".rels";
}
}
namespace xlnt {
relationship_serializer::relationship_serializer(zip_file &archive) : archive_(archive)
{
}
std::vector<relationship> relationship_serializer::read_relationships(const std::string &target)
{
pugi::xml_document xml;
xml.load(archive_.read(path(make_rels_name(target))).c_str());
auto root_node = xml.child("Relationships");
std::vector<relationship> relationships;
for (auto relationship_node : root_node.children("Relationship"))
{
std::string id = relationship_node.attribute("Id").value();
std::string type = relationship_node.attribute("Type").value();
std::string rel_target = relationship_node.attribute("Target").value();
relationships.push_back(xlnt::relationship(type, id, rel_target));
}
return relationships;
}
bool relationship_serializer::write_relationships(const std::vector<relationship> &relationships,
const std::string &target)
{
pugi::xml_document xml;
auto root_node = xml.append_child("Relationships");
root_node.append_attribute("xmlns").set_value(constants::get_namespace("relationships").c_str());
for (const auto &relationship : relationships)
{
auto relationship_node = root_node.append_child("Relationship");
relationship_node.append_attribute("Id").set_value(relationship.get_id().c_str());
relationship_node.append_attribute("Type").set_value(relationship.get_type_string().c_str());
relationship_node.append_attribute("Target").set_value(relationship.get_target_uri().c_str());
if (relationship.get_target_mode() == target_mode::external)
{
relationship_node.append_attribute("TargetMode").set_value("External");
}
}
std::ostringstream ss;
xml.save(ss);
archive_.write_string(ss.str(), path(make_rels_name(target)));
return true;
}
}

View File

@ -1,64 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <vector>
#include <xlnt/xlnt_config.hpp>
namespace xlnt {
class relationship;
class zip_file;
/// <summary>
/// Reads and writes collections of relationshps for a particular file.
/// </summary>
class XLNT_CLASS relationship_serializer
{
public:
/// <summary>
/// Construct a serializer which operates on archive.
/// </summary>
relationship_serializer(zip_file &archive);
/// <summary>
/// Return a vector of relationships corresponding to target.
/// </summary>
std::vector<relationship> read_relationships(const std::string &target);
/// <summary>
/// Write relationships to archive for the given target.
/// </summary>
bool write_relationships(const std::vector<relationship> &relationships, const std::string &target);
private:
/// <summary>
/// Internal archive which is used for reading and writing.
/// </summary>
zip_file &archive_;
};
} // namespace xlnt

View File

@ -1,183 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <pugixml.hpp>
#include <detail/shared_strings_serializer.hpp>
#include <xlnt/cell/text.hpp>
namespace {
std::size_t string_to_size_t(const std::string &s)
{
#if ULLONG_MAX == SIZE_MAX
return std::stoull(s);
#else
return std::stoul(s);
#endif
} // namespace
}
namespace xlnt {
void shared_strings_serializer::write_shared_strings(const std::vector<text> &strings, pugi::xml_document &xml)
{
auto root_node = xml.append_child("sst");
root_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
root_node.append_attribute("count").set_value(std::to_string(strings.size()).c_str());
root_node.append_attribute("uniqueCount").set_value(std::to_string(strings.size()).c_str());
for (const auto &string : strings)
{
if (string.get_runs().size() == 1 && !string.get_runs().at(0).has_formatting())
{
root_node.append_child("si").append_child("t").text().set(string.get_plain_string().c_str());
}
else
{
auto string_item_node = root_node.append_child("si");
for (const auto &run : string.get_runs())
{
auto rich_text_run_node = string_item_node.append_child("r");
if (run.has_formatting())
{
auto run_properties_node = rich_text_run_node.append_child("rPr");
if (run.has_size())
{
run_properties_node.append_child("sz").append_attribute("val").set_value(std::to_string(run.get_size()).c_str());
}
if (run.has_color())
{
run_properties_node.append_child("color").append_attribute("rgb").set_value(run.get_color().c_str());
}
if (run.has_font())
{
run_properties_node.append_child("rFont").append_attribute("val").set_value(run.get_font().c_str());
}
if (run.has_family())
{
run_properties_node.append_child("family").append_attribute("val").set_value(std::to_string(run.get_family()).c_str());
}
if (run.has_scheme())
{
run_properties_node.append_child("scheme").append_attribute("val").set_value(run.get_scheme().c_str());
}
}
auto text_node = rich_text_run_node.append_child("t");
text_node.text().set(run.get_string().c_str());
}
}
}
}
bool shared_strings_serializer::read_shared_strings(const pugi::xml_document &xml, std::vector<text> &strings)
{
strings.clear();
auto root_node = xml.child("sst");
std::size_t unique_count = 0;
if (root_node.attribute("uniqueCount"))
{
unique_count = string_to_size_t(root_node.attribute("uniqueCount").value());
}
for (const auto string_item_node : root_node.children("si"))
{
if (string_item_node.child("t"))
{
text t;
t.set_plain_string(string_item_node.child("t").text().get());
strings.push_back(t);
}
else if (string_item_node.child("r")) // possible multiple text entities.
{
text t;
for (const auto& rich_text_run_node : string_item_node.children("r"))
{
if (rich_text_run_node.child("t"))
{
text_run run;
run.set_string(rich_text_run_node.child("t").text().get());
if (rich_text_run_node.child("rPr"))
{
auto run_properties_node = rich_text_run_node.child("rPr");
if (run_properties_node.child("sz"))
{
run.set_size(string_to_size_t(run_properties_node.child("sz").attribute("val").value()));
}
if (run_properties_node.child("rFont"))
{
run.set_font(run_properties_node.child("rFont").attribute("val").value());
}
if (run_properties_node.child("color"))
{
run.set_color(run_properties_node.child("color").attribute("rgb").value());
}
if (run_properties_node.child("family"))
{
run.set_family(string_to_size_t(run_properties_node.child("family").attribute("val").value()));
}
if (run_properties_node.child("scheme"))
{
run.set_scheme(run_properties_node.child("scheme").attribute("val").value());
}
}
t.add_run(run);
}
}
strings.push_back(t);
}
}
if (unique_count != strings.size())
{
throw std::runtime_error("counts don't match");
}
return true;
}
} // namespace xlnt

View File

@ -1,48 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <vector>
#include <xlnt/xlnt_config.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
class text;
/// <summary>
/// Manages converting a set of shared strings to and from XML.
/// </summary>
class XLNT_CLASS shared_strings_serializer
{
public:
static bool read_shared_strings(const pugi::xml_document &xml, std::vector<text> &strings);
static void write_shared_strings(const std::vector<text> &strings, pugi::xml_document &xml);
};
} // namespace xlnt

File diff suppressed because it is too large Load Diff

View File

@ -1,82 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <xlnt/xlnt_config.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
class alignment;
class border;
class color;
class conditional_format;
class fill;
class font;
class format;
class base_format;
class style;
class number_format;
class protection;
class style;
namespace detail { struct stylesheet; }
/// <summary>
/// Reads and writes xl/styles.xml from/to an associated stylesheet.
/// </summary>
class XLNT_CLASS style_serializer
{
public:
/// <summary>
/// Construct a style_serializer which can write styles.xml based on wb or populate wb
/// with styles from an existing styles.xml.
style_serializer(detail::stylesheet &s);
/// <summary>
/// Load all styles from xml_document into workbook given in constructor.
/// </summary>
bool read_stylesheet(const pugi::xml_document &xml);
/// <summary>
/// Populate parameter xml with an XML tree representing the styles contained in the workbook
/// given in the constructor.
/// </summary>
bool write_stylesheet(pugi::xml_document &xml);
private:
/// <summary>
/// Set in the constructor, this stylesheet is used as the source or target for all writing or reading, respectively.
/// </summary>
detail::stylesheet &stylesheet_;
};
} // namespace xlnt

View File

@ -1,370 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <pugixml.hpp>
#include <vector>
#include <detail/constants.hpp>
#include <detail/theme_serializer.hpp>
namespace xlnt {
// I have no idea what this stuff is. I hope it was worth it.
void theme_serializer::write_theme(const theme &, pugi::xml_document &xml) const
{
auto theme_node = xml.append_child("a:theme");
theme_node.append_attribute("xmlns:a").set_value(constants::get_namespace("drawingml").c_str());
theme_node.append_attribute("name").set_value("Office Theme");
auto theme_elements_node = theme_node.append_child("a:themeElements");
auto clr_scheme_node = theme_elements_node.append_child("a:clrScheme");
clr_scheme_node.append_attribute("name").set_value("Office");
struct scheme_element
{
std::string name;
std::string sub_element_name;
std::string val;
};
std::vector<scheme_element> scheme_elements = {
{ "a:dk1", "a:sysClr", "windowText" }, { "a:lt1", "a:sysClr", "window" },
{ "a:dk2", "a:srgbClr", "1F497D" }, { "a:lt2", "a:srgbClr", "EEECE1" },
{ "a:accent1", "a:srgbClr", "4F81BD" }, { "a:accent2", "a:srgbClr", "C0504D" },
{ "a:accent3", "a:srgbClr", "9BBB59" }, { "a:accent4", "a:srgbClr", "8064A2" },
{ "a:accent5", "a:srgbClr", "4BACC6" }, { "a:accent6", "a:srgbClr", "F79646" },
{ "a:hlink", "a:srgbClr", "0000FF" }, { "a:folHlink", "a:srgbClr", "800080" },
};
for (auto element : scheme_elements)
{
auto element_node = clr_scheme_node.append_child(element.name.c_str());
element_node.append_child(element.sub_element_name.c_str()).append_attribute("val").set_value(element.val.c_str());
if (element.name == "a:dk1")
{
element_node.child(element.sub_element_name.c_str()).append_attribute("lastClr").set_value("000000");
}
else if (element.name == "a:lt1")
{
element_node.child(element.sub_element_name.c_str()).append_attribute("lastClr").set_value("FFFFFF");
}
}
struct font_scheme
{
bool typeface;
std::string script;
std::string major;
std::string minor;
};
std::vector<font_scheme> font_schemes = {
{ true, "a:latin", "Cambria", "Calibri" },
{ true, "a:ea", "", "" },
{ true, "a:cs", "", "" },
{ false, "Jpan", "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf",
"\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
{ false, "Hang", "\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95",
"\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95" },
{ false, "Hans", "\xe5\xae\x8b\xe4\xbd\x93", "\xe5\xae\x8b\xe4\xbd\x93" },
{ false, "Hant", "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94",
"\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
{ false, "Arab", "Times New Roman", "Arial" },
{ false, "Hebr", "Times New Roman", "Arial" },
{ false, "Thai", "Tahoma", "Tahoma" },
{ false, "Ethi", "Nyala", "Nyala" },
{ false, "Beng", "Vrinda", "Vrinda" },
{ false, "Gujr", "Shruti", "Shruti" },
{ false, "Khmr", "MoolBoran", "DaunPenh" },
{ false, "Knda", "Tunga", "Tunga" },
{ false, "Guru", "Raavi", "Raavi" },
{ false, "Cans", "Euphemia", "Euphemia" },
{ false, "Cher", "Plantagenet Cherokee", "Plantagenet Cherokee" },
{ false, "Yiii", "Microsoft Yi Baiti", "Microsoft Yi Baiti" },
{ false, "Tibt", "Microsoft Himalaya", "Microsoft Himalaya" },
{ false, "Thaa", "MV Boli", "MV Boli" },
{ false, "Deva", "Mangal", "Mangal" },
{ false, "Telu", "Gautami", "Gautami" },
{ false, "Taml", "Latha", "Latha" },
{ false, "Syrc", "Estrangelo Edessa", "Estrangelo Edessa" },
{ false, "Orya", "Kalinga", "Kalinga" },
{ false, "Mlym", "Kartika", "Kartika" },
{ false, "Laoo", "DokChampa", "DokChampa" },
{ false, "Sinh", "Iskoola Pota", "Iskoola Pota" },
{ false, "Mong", "Mongolian Baiti", "Mongolian Baiti" },
{ false, "Viet", "Times New Roman", "Arial" },
{ false, "Uigh", "Microsoft Uighur", "Microsoft Uighur" }
};
auto font_scheme_node = theme_elements_node.append_child("a:fontScheme");
font_scheme_node.append_attribute("name").set_value("Office");
auto major_fonts_node = font_scheme_node.append_child("a:majorFont");
auto minor_fonts_node = font_scheme_node.append_child("a:minorFont");
for (auto scheme : font_schemes)
{
if (scheme.typeface)
{
auto major_font_node = major_fonts_node.append_child(scheme.script.c_str());
major_font_node.append_attribute("typeface").set_value(scheme.major.c_str());
auto minor_font_node = minor_fonts_node.append_child(scheme.script.c_str());
minor_font_node.append_attribute("typeface").set_value(scheme.minor.c_str());
}
else
{
auto major_font_node = major_fonts_node.append_child("a:font");
major_font_node.append_attribute("script").set_value(scheme.script.c_str());
major_font_node.append_attribute("typeface").set_value(scheme.major.c_str());
auto minor_font_node = minor_fonts_node.append_child("a:font");
minor_font_node.append_attribute("script").set_value(scheme.script.c_str());
minor_font_node.append_attribute("typeface").set_value(scheme.minor.c_str());
}
}
auto format_scheme_node = theme_elements_node.append_child("a:fmtScheme");
format_scheme_node.append_attribute("name").set_value("Office");
auto fill_style_list_node = format_scheme_node.append_child("a:fillStyleLst");
fill_style_list_node.append_child("a:solidFill").append_child("a:schemeClr").append_attribute("val").set_value("phClr");
auto grad_fill_node = fill_style_list_node.append_child("a:gradFill");
grad_fill_node.append_attribute("rotWithShape").set_value("1");
auto grad_fill_list = grad_fill_node.append_child("a:gsLst");
auto gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("0");
auto scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("50000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("300000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("35000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("37000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("300000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("100000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("15000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("350000");
auto lin_node = grad_fill_node.append_child("a:lin");
lin_node.append_attribute("ang").set_value("16200000");
lin_node.append_attribute("scaled").set_value("1");
grad_fill_node = fill_style_list_node.append_child("a:gradFill");
grad_fill_node.append_attribute("rotWithShape").set_value("1");
grad_fill_list = grad_fill_node.append_child("a:gsLst");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("0");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("51000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("130000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("80000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("93000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("130000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("100000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("94000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("135000");
lin_node = grad_fill_node.append_child("a:lin");
lin_node.append_attribute("ang").set_value("16200000");
lin_node.append_attribute("scaled").set_value("0");
auto line_style_list_node = format_scheme_node.append_child("a:lnStyleLst");
auto ln_node = line_style_list_node.append_child("a:ln");
ln_node.append_attribute("w").set_value("9525");
ln_node.append_attribute("cap").set_value("flat");
ln_node.append_attribute("cmpd").set_value("sng");
ln_node.append_attribute("algn").set_value("ctr");
auto solid_fill_node = ln_node.append_child("a:solidFill");
scheme_color_node = solid_fill_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("95000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("105000");
ln_node.append_child("a:prstDash").append_attribute("val").set_value("solid");
ln_node = line_style_list_node.append_child("a:ln");
ln_node.append_attribute("w").set_value("25400");
ln_node.append_attribute("cap").set_value("flat");
ln_node.append_attribute("cmpd").set_value("sng");
ln_node.append_attribute("algn").set_value("ctr");
solid_fill_node = ln_node.append_child("a:solidFill");
scheme_color_node = solid_fill_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
ln_node.append_child("a:prstDash").append_attribute("val").set_value("solid");
ln_node = line_style_list_node.append_child("a:ln");
ln_node.append_attribute("w").set_value("38100");
ln_node.append_attribute("cap").set_value("flat");
ln_node.append_attribute("cmpd").set_value("sng");
ln_node.append_attribute("algn").set_value("ctr");
solid_fill_node = ln_node.append_child("a:solidFill");
scheme_color_node = solid_fill_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
ln_node.append_child("a:prstDash").append_attribute("val").set_value("solid");
auto effect_style_list_node = format_scheme_node.append_child("a:effectStyleLst");
auto effect_style_node = effect_style_list_node.append_child("a:effectStyle");
auto effect_list_node = effect_style_node.append_child("a:effectLst");
auto outer_shadow_node = effect_list_node.append_child("a:outerShdw");
outer_shadow_node.append_attribute("blurRad").set_value("40000");
outer_shadow_node.append_attribute("dist").set_value("20000");
outer_shadow_node.append_attribute("dir").set_value("5400000");
outer_shadow_node.append_attribute("rotWithShape").set_value("0");
auto srgb_clr_node = outer_shadow_node.append_child("a:srgbClr");
srgb_clr_node.append_attribute("val").set_value("000000");
srgb_clr_node.append_child("a:alpha").append_attribute("val").set_value("38000");
effect_style_node = effect_style_list_node.append_child("a:effectStyle");
effect_list_node = effect_style_node.append_child("a:effectLst");
outer_shadow_node = effect_list_node.append_child("a:outerShdw");
outer_shadow_node.append_attribute("blurRad").set_value("40000");
outer_shadow_node.append_attribute("dist").set_value("23000");
outer_shadow_node.append_attribute("dir").set_value("5400000");
outer_shadow_node.append_attribute("rotWithShape").set_value("0");
srgb_clr_node = outer_shadow_node.append_child("a:srgbClr");
srgb_clr_node.append_attribute("val").set_value("000000");
srgb_clr_node.append_child("a:alpha").append_attribute("val").set_value("35000");
effect_style_node = effect_style_list_node.append_child("a:effectStyle");
effect_list_node = effect_style_node.append_child("a:effectLst");
outer_shadow_node = effect_list_node.append_child("a:outerShdw");
outer_shadow_node.append_attribute("blurRad").set_value("40000");
outer_shadow_node.append_attribute("dist").set_value("23000");
outer_shadow_node.append_attribute("dir").set_value("5400000");
outer_shadow_node.append_attribute("rotWithShape").set_value("0");
srgb_clr_node = outer_shadow_node.append_child("a:srgbClr");
srgb_clr_node.append_attribute("val").set_value("000000");
srgb_clr_node.append_child("a:alpha").append_attribute("val").set_value("35000");
auto scene3d_node = effect_style_node.append_child("a:scene3d");
auto camera_node = scene3d_node.append_child("a:camera");
camera_node.append_attribute("prst").set_value("orthographicFront");
auto rot_node = camera_node.append_child("a:rot");
rot_node.append_attribute("lat").set_value("0");
rot_node.append_attribute("lon").set_value("0");
rot_node.append_attribute("rev").set_value("0");
auto light_rig_node = scene3d_node.append_child("a:lightRig");
light_rig_node.append_attribute("rig").set_value("threePt");
light_rig_node.append_attribute("dir").set_value("t");
rot_node = light_rig_node.append_child("a:rot");
rot_node.append_attribute("lat").set_value("0");
rot_node.append_attribute("lon").set_value("0");
rot_node.append_attribute("rev").set_value("1200000");
auto bevel_node = effect_style_node.append_child("a:sp3d").append_child("a:bevelT");
bevel_node.append_attribute("w").set_value("63500");
bevel_node.append_attribute("h").set_value("25400");
auto bg_fill_style_list_node = format_scheme_node.append_child("a:bgFillStyleLst");
bg_fill_style_list_node.append_child("a:solidFill").append_child("a:schemeClr").append_attribute("val").set_value("phClr");
grad_fill_node = bg_fill_style_list_node.append_child("a:gradFill");
grad_fill_node.append_attribute("rotWithShape").set_value("1");
grad_fill_list = grad_fill_node.append_child("a:gsLst");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("0");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("40000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("350000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("40000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("45000");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("99000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("350000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("100000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("20000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("255000");
auto path_node = grad_fill_node.append_child("a:path");
path_node.append_attribute("path").set_value("circle");
auto fill_to_rect_node = path_node.append_child("a:fillToRect");
fill_to_rect_node.append_attribute("l").set_value("50000");
fill_to_rect_node.append_attribute("t").set_value("-80000");
fill_to_rect_node.append_attribute("r").set_value("50000");
fill_to_rect_node.append_attribute("b").set_value("180000");
grad_fill_node = bg_fill_style_list_node.append_child("a:gradFill");
grad_fill_node.append_attribute("rotWithShape").set_value("1");
grad_fill_list = grad_fill_node.append_child("a:gsLst");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("0");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:tint").append_attribute("val").set_value("80000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("300000");
gs_node = grad_fill_list.append_child("a:gs");
gs_node.append_attribute("pos").set_value("100000");
scheme_color_node = gs_node.append_child("a:schemeClr");
scheme_color_node.append_attribute("val").set_value("phClr");
scheme_color_node.append_child("a:shade").append_attribute("val").set_value("30000");
scheme_color_node.append_child("a:satMod").append_attribute("val").set_value("200000");
path_node = grad_fill_node.append_child("a:path");
path_node.append_attribute("path").set_value("circle");
fill_to_rect_node = path_node.append_child("a:fillToRect");
fill_to_rect_node.append_attribute("l").set_value("50000");
fill_to_rect_node.append_attribute("t").set_value("50000");
fill_to_rect_node.append_attribute("r").set_value("50000");
fill_to_rect_node.append_attribute("b").set_value("50000");
theme_node.append_child("a:objectDefaults");
theme_node.append_child("a:extraClrSchemeLst");
}
} // namespace xlnt

View File

@ -1,50 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <xlnt/xlnt_config.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
class theme;
/// <summary>
/// Manages converting a theme to and from XML.
/// </summary>
class XLNT_CLASS theme_serializer
{
public:
bool read_theme(const pugi::xml_document &xml, theme &t);
void write_theme(const theme &t, pugi::xml_document &xml) const;
private:
};
} // namespace xlnt

View File

@ -66,8 +66,6 @@ struct workbook_impl
workbook_impl(const workbook_impl &other)
: active_sheet_index_(other.active_sheet_index_),
worksheets_(other.worksheets_),
relationships_(other.relationships_),
root_relationships_(other.root_relationships_),
shared_strings_(other.shared_strings_),
guess_types_(other.guess_types_),
data_only_(other.data_only_),
@ -103,11 +101,6 @@ struct workbook_impl
active_sheet_index_ = other.active_sheet_index_;
worksheets_.clear();
std::copy(other.worksheets_.begin(), other.worksheets_.end(), back_inserter(worksheets_));
relationships_.clear();
std::copy(other.relationships_.begin(), other.relationships_.end(), std::back_inserter(relationships_));
root_relationships_.clear();
std::copy(other.root_relationships_.begin(), other.root_relationships_.end(),
std::back_inserter(root_relationships_));
shared_strings_.clear();
std::copy(other.shared_strings_.begin(), other.shared_strings_.end(), std::back_inserter(shared_strings_));
guess_types_ = other.guess_types_;
@ -143,8 +136,6 @@ struct workbook_impl
std::size_t active_sheet_index_;
std::list<worksheet_impl> worksheets_;
std::vector<relationship> relationships_;
std::vector<relationship> root_relationships_;
std::vector<text> shared_strings_;
bool guess_types_;

View File

@ -1,329 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <algorithm>
#include <pugixml.hpp>
#include <detail/constants.hpp>
#include <detail/workbook_serializer.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/named_range.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/workbook/worksheet_iterator.hpp>
#include <xlnt/worksheet/range_reference.hpp>
#include <xlnt/worksheet/worksheet.hpp>
namespace {
xlnt::datetime w3cdtf_to_datetime(const std::string &string)
{
xlnt::datetime result(1900, 1, 1);
auto separator_index = string.find('-');
result.year = std::stoi(string.substr(0, separator_index));
result.month = std::stoi(string.substr(separator_index + 1, string.find('-', separator_index + 1)));
separator_index = string.find('-', separator_index + 1);
result.day = std::stoi(string.substr(separator_index + 1, string.find('T', separator_index + 1)));
separator_index = string.find('T', separator_index + 1);
result.hour = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
separator_index = string.find(':', separator_index + 1);
result.minute = std::stoi(string.substr(separator_index + 1, string.find(':', separator_index + 1)));
separator_index = string.find(':', separator_index + 1);
result.second = std::stoi(string.substr(separator_index + 1, string.find('Z', separator_index + 1)));
return result;
}
std::string fill(const std::string &string, std::size_t length = 2)
{
if (string.size() >= length)
{
return string;
}
return std::string(length - string.size(), '0') + string;
}
std::string datetime_to_w3cdtf(const xlnt::datetime &dt)
{
return std::to_string(dt.year) + "-" + fill(std::to_string(dt.month)) + "-" + fill(std::to_string(dt.day)) + "T" +
fill(std::to_string(dt.hour)) + ":" + fill(std::to_string(dt.minute)) + ":" +
fill(std::to_string(dt.second)) + "Z";
}
} // namespace
namespace xlnt {
workbook_serializer::workbook_serializer(workbook &wb) : workbook_(wb)
{
}
void workbook_serializer::read_properties_core(const pugi::xml_document &xml)
{
auto root_node = xml.child("cp:coreProperties");
if (root_node.child("dc:creator"))
{
workbook_.set_creator(root_node.child("dc:creator").text().get());
}
if (root_node.child("cp:lastModifiedBy"))
{
workbook_.set_last_modified_by(root_node.child("cp:lastModifiedBy").text().get());
}
if (root_node.child("dcterms:created"))
{
std::string created_string = root_node.child("dcterms:created").text().get();
workbook_.set_created(w3cdtf_to_datetime(created_string));
}
if (root_node.child("dcterms:modified"))
{
std::string modified_string = root_node.child("dcterms:modified").text().get();
workbook_.set_modified(w3cdtf_to_datetime(modified_string));
}
}
void workbook_serializer::read_properties_app(const pugi::xml_document &xml)
{
auto root_node = xml.child("Properties");
if(root_node.child("Application"))
{
workbook_.set_application(root_node.child("Application").text().get());
}
if(root_node.child("DocSecurity"))
{
workbook_.set_doc_security(std::stoi(root_node.child("DocSecurity").text().get()));
}
if(root_node.child("ScaleCrop"))
{
workbook_.set_scale_crop(root_node.child("ScaleCrop").text().get() == std::string("true"));
}
if(root_node.child("Company"))
{
workbook_.set_company(root_node.child("Company").text().get());
}
if(root_node.child("ScaleCrop"))
{
workbook_.set_links_up_to_date(root_node.child("ScaleCrop").text().get() == std::string("true"));
}
if(root_node.child("SharedDoc"))
{
workbook_.set_shared_doc(root_node.child("SharedDoc").text().get() == std::string("true"));
}
if(root_node.child("HyperlinksChanged"))
{
workbook_.set_hyperlinks_changed(root_node.child("HyperlinksChanged").text().get() == std::string("true"));
}
if(root_node.child("AppVersion"))
{
workbook_.set_app_version(root_node.child("AppVersion").text().get());
}
}
void workbook_serializer::write_properties_core(pugi::xml_document &xml) const
{
auto root_node = xml.append_child("cp:coreProperties");
root_node.append_attribute("xmlns:cp").set_value("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
root_node.append_attribute("xmlns:dc").set_value("http://purl.org/dc/elements/1.1/");
root_node.append_attribute("xmlns:dcmitype").set_value("http://purl.org/dc/dcmitype/");
root_node.append_attribute("xmlns:dcterms").set_value("http://purl.org/dc/terms/");
root_node.append_attribute("xmlns:xsi").set_value("http://www.w3.org/2001/XMLSchema-instance");
root_node.append_child("dc:creator").text().set(workbook_.get_creator().c_str());
root_node.append_child("cp:lastModifiedBy").text().set(workbook_.get_last_modified_by().c_str());
root_node.append_child("dcterms:created").text().set(datetime_to_w3cdtf(workbook_.get_created()).c_str());
root_node.child("dcterms:created").append_attribute("xsi:type").set_value("dcterms:W3CDTF");
root_node.append_child("dcterms:modified").text().set(datetime_to_w3cdtf(workbook_.get_modified()).c_str());
root_node.child("dcterms:modified").append_attribute("xsi:type").set_value("dcterms:W3CDTF");
root_node.append_child("dc:title").text().set(workbook_.get_title().c_str());
root_node.append_child("dc:description");
root_node.append_child("dc:subject");
root_node.append_child("cp:keywords");
root_node.append_child("cp:category");
}
void workbook_serializer::write_properties_app(pugi::xml_document &xml) const
{
auto root_node = xml.append_child("Properties");
root_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
root_node.append_attribute("xmlns:vt").set_value("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");
root_node.append_child("Application").text().set(workbook_.get_application().c_str());
root_node.append_child("DocSecurity").text().set(std::to_string(workbook_.get_doc_security()).c_str());
root_node.append_child("ScaleCrop").text().set(workbook_.get_scale_crop() ? "true" : "false");
auto company_node = root_node.append_child("Company");
if (!workbook_.get_company().empty())
{
company_node.text().set(workbook_.get_company().c_str());
}
root_node.append_child("LinksUpToDate").text().set(workbook_.links_up_to_date() ? "true" : "false");
root_node.append_child("SharedDoc").text().set(workbook_.is_shared_doc() ? "true" : "false");
root_node.append_child("HyperlinksChanged").text().set(workbook_.hyperlinks_changed() ? "true" : "false");
root_node.append_child("AppVersion").text().set(workbook_.get_app_version().c_str());
// TODO what is this stuff?
auto heading_pairs_node = root_node.append_child("HeadingPairs");
auto heading_pairs_vector_node = heading_pairs_node.append_child("vt:vector");
heading_pairs_vector_node.append_attribute("size").set_value("2");
heading_pairs_vector_node.append_attribute("baseType").set_value("variant");
heading_pairs_vector_node.append_child("vt:variant").append_child("vt:lpstr").text().set("Worksheets");
heading_pairs_vector_node.append_child("vt:variant")
.append_child("vt:i4")
.text().set(std::to_string(workbook_.get_sheet_titles().size()).c_str());
auto titles_of_parts_node = root_node.append_child("TitlesOfParts");
auto titles_of_parts_vector_node = titles_of_parts_node.append_child("vt:vector");
titles_of_parts_vector_node.append_attribute("size").set_value(std::to_string(workbook_.get_sheet_titles().size()).c_str());
titles_of_parts_vector_node.append_attribute("baseType").set_value("lpstr");
for (auto ws : workbook_)
{
titles_of_parts_vector_node.append_child("vt:lpstr").text().set(ws.get_title().c_str());
}
}
void workbook_serializer::write_workbook(pugi::xml_document &xml) const
{
std::size_t num_visible = 0;
for (auto ws : workbook_)
{
if (ws.get_page_setup().get_sheet_state() == sheet_state::visible)
{
num_visible++;
}
}
if (num_visible == 0)
{
throw xlnt::no_visible_worksheets();
}
auto root_node = xml.append_child("workbook");
root_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
root_node.append_attribute("xmlns:r").set_value("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
auto file_version_node = root_node.append_child("fileVersion");
file_version_node.append_attribute("appName").set_value("xl");
file_version_node.append_attribute("lastEdited").set_value("4");
file_version_node.append_attribute("lowestEdited").set_value("4");
file_version_node.append_attribute("rupBuild").set_value("4505");
auto workbook_pr_node = root_node.append_child("workbookPr");
workbook_pr_node.append_attribute("codeName").set_value("ThisWorkbook");
workbook_pr_node.append_attribute("defaultThemeVersion").set_value("124226");
workbook_pr_node.append_attribute("date1904").set_value(workbook_.get_base_date() == calendar::mac_1904 ? "1" : "0");
auto book_views_node = root_node.append_child("bookViews");
auto workbook_view_node = book_views_node.append_child("workbookView");
workbook_view_node.append_attribute("activeTab").set_value("0");
workbook_view_node.append_attribute("autoFilterDateGrouping").set_value("1");
workbook_view_node.append_attribute("firstSheet").set_value("0");
workbook_view_node.append_attribute("minimized").set_value("0");
workbook_view_node.append_attribute("showHorizontalScroll").set_value("1");
workbook_view_node.append_attribute("showSheetTabs").set_value("1");
workbook_view_node.append_attribute("showVerticalScroll").set_value("1");
workbook_view_node.append_attribute("tabRatio").set_value("600");
workbook_view_node.append_attribute("visibility").set_value("visible");
auto sheets_node = root_node.append_child("sheets");
auto defined_names_node = root_node.append_child("definedNames");
std::size_t index = 1;
for (const auto ws : workbook_)
{
auto target = "worksheets/sheet" + std::to_string(index++) + ".xml";
for (const auto &rel : workbook_.get_relationships())
{
if (rel.get_target_uri() != target) continue;
auto sheet_node = sheets_node.append_child("sheet");
sheet_node.append_attribute("name").set_value(ws.get_title().c_str());
sheet_node.append_attribute("sheetId").set_value(std::to_string(ws.get_id()).c_str());
if (ws.get_sheet_state() == xlnt::sheet_state::hidden)
{
sheet_node.append_attribute("state").set_value("hidden");
}
sheet_node.append_attribute("r:id").set_value(rel.get_id().c_str());
if (ws.has_auto_filter())
{
auto defined_name_node = defined_names_node.append_child("definedName");
defined_name_node.append_attribute("name").set_value("_xlnm._FilterDatabase");
defined_name_node.append_attribute("hidden").set_value("1");
defined_name_node.append_attribute("localSheetId").set_value("0");
std::string name =
"'" + ws.get_title() + "'!" + range_reference::make_absolute(ws.get_auto_filter()).to_string();
defined_name_node.text().set(name.c_str());
}
break;
}
}
auto calc_pr_node = root_node.append_child("calcPr");
calc_pr_node.append_attribute("calcId").set_value("124519");
calc_pr_node.append_attribute("calcMode").set_value("auto");
calc_pr_node.append_attribute("fullCalcOnLoad").set_value("1");
}
void workbook_serializer::write_named_ranges(pugi::xml_node node) const
{
for (auto &named_range : workbook_.get_named_ranges())
{
auto defined_name_node = node.append_child("s:definedName");
defined_name_node.append_attribute("xmlns:s").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
defined_name_node.append_attribute("name").set_value(named_range.get_name().c_str());
const auto &target = named_range.get_targets().front();
std::string target_string = "'" + target.first.get_title();
target_string.push_back('\'');
target_string.push_back('!');
target_string.append(target.second.to_string());
defined_name_node.text().set(target_string.c_str());
}
}
} // namespace xlnt

View File

@ -1,69 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <vector>
#include <xlnt/xlnt_config.hpp>
namespace pugi {
class xml_document;
class xml_node;
} // namespace pugi
namespace xlnt {
class document_properties;
class manifest;
class relationship;
class worksheet;
class workbook;
class zip_file;
/// <summary>
/// Manages converting workbook to and from XML.
/// </summary>
class XLNT_CLASS workbook_serializer
{
public:
using string_pair = std::pair<std::string, std::string>;
workbook_serializer(workbook &wb);
void read_workbook(const pugi::xml_document &xml);
void read_properties_app(const pugi::xml_document &xml);
void read_properties_core(const pugi::xml_document &xml);
void write_workbook(pugi::xml_document &xml) const;
void write_properties_app(pugi::xml_document &xml) const;
void write_properties_core(pugi::xml_document &xml) const;
void write_named_ranges(pugi::xml_node node) const;
private:
workbook &workbook_;
};
} // namespace xlnt

View File

@ -1,600 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <algorithm>
#include <cmath>
#include <pugixml.hpp>
#include <sstream>
#include <detail/constants.hpp>
#include <detail/stylesheet.hpp>
#include <detail/worksheet_serializer.hpp>
#include <xlnt/cell/cell.hpp>
#include <xlnt/cell/cell_reference.hpp>
#include <xlnt/cell/text.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/cell_iterator.hpp>
#include <xlnt/worksheet/column_properties.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/range_iterator.hpp>
#include <xlnt/worksheet/range_reference.hpp>
#include <xlnt/worksheet/row_properties.hpp>
namespace {
bool is_integral(long double d)
{
return d == static_cast<long long int>(d);
}
} // namepsace
namespace xlnt {
worksheet_serializer::worksheet_serializer(worksheet sheet) : sheet_(sheet)
{
}
bool worksheet_serializer::read_worksheet(const pugi::xml_document &xml, detail::stylesheet &stylesheet)
{
auto root_node = xml.child("worksheet");
auto dimension_node = root_node.child("dimension");
std::string dimension(dimension_node.attribute("ref").value());
auto full_range = xlnt::range_reference(dimension);
auto sheet_data_node = root_node.child("sheetData");
if (root_node.child("mergeCells"))
{
auto merge_cells_node = root_node.child("mergeCells");
auto count = std::stoull(merge_cells_node.attribute("count").value());
for (auto merge_cell_node : merge_cells_node.children("mergeCell"))
{
sheet_.merge_cells(range_reference(merge_cell_node.attribute("ref").value()));
count--;
}
if (count != 0)
{
throw std::runtime_error("mismatch between count and actual number of merged cells");
}
}
auto &shared_strings = sheet_.get_workbook().get_shared_strings();
for (auto row_node : sheet_data_node.children("row"))
{
auto row_index = static_cast<row_t>(std::stoull(row_node.attribute("r").value()));
if (row_node.attribute("ht"))
{
sheet_.get_row_properties(row_index).height = std::stold(row_node.attribute("ht").value());
}
std::string span_string = row_node.attribute("spans").value();
auto colon_index = span_string.find(':');
column_t min_column = 0;
column_t max_column = 0;
if (colon_index != std::string::npos)
{
min_column = static_cast<column_t::index_t>(std::stoll(span_string.substr(0, colon_index)));
max_column = static_cast<column_t::index_t>(std::stoll(span_string.substr(colon_index + 1)));
}
else
{
min_column = full_range.get_top_left().get_column_index();
max_column = full_range.get_bottom_right().get_column_index();
}
for (column_t i = min_column; i <= max_column; i++)
{
std::string address = i.column_string() + std::to_string(row_index);
pugi::xml_node cell_node;
bool cell_found = false;
for (auto &check_cell_node : row_node.children("c"))
{
if (check_cell_node.attribute("r") && check_cell_node.attribute("r").value() == address)
{
cell_node = check_cell_node;
cell_found = true;
break;
}
}
if (cell_found)
{
bool has_value = cell_node.child("v") != nullptr;
std::string value_string = has_value ? cell_node.child("v").text().get() : "";
bool has_type = cell_node.attribute("t") != nullptr;
std::string type = has_type ? cell_node.attribute("t").value() : "";
bool has_format = cell_node.attribute("s") != nullptr;
auto format_id = static_cast<std::size_t>(has_format ? std::stoull(cell_node.attribute("s").value()) : 0LL);
bool has_formula = cell_node.child("f") != nullptr;
bool has_shared_formula = has_formula && cell_node.child("f").attribute("t") != nullptr
&& cell_node.child("f").attribute("t").value() == std::string("shared");
auto cell = sheet_.get_cell(address);
if (has_formula && !has_shared_formula && !sheet_.get_workbook().get_data_only())
{
std::string formula = cell_node.child("f").text().get();
cell.set_formula(formula);
}
if (has_type && type == "inlineStr") // inline string
{
std::string inline_string = cell_node.child("is").child("t").text().get();
cell.set_value(inline_string);
}
else if (has_type && type == "s" && !has_formula) // shared string
{
auto shared_string_index = static_cast<std::size_t>(std::stoull(value_string));
auto shared_string = shared_strings.at(shared_string_index);
cell.set_value(shared_string);
}
else if (has_type && type == "b") // boolean
{
cell.set_value(value_string != "0");
}
else if (has_type && type == "str")
{
cell.set_value(value_string);
}
else if (has_value && !value_string.empty())
{
if (!value_string.empty() && value_string[0] == '#')
{
cell.set_error(value_string);
}
else
{
cell.set_value(std::stold(value_string));
}
}
if (has_format)
{
cell.set_format(stylesheet.formats.at(format_id));
}
}
}
}
auto cols_node = root_node.child("cols");
for (auto col_node : cols_node.children("col"))
{
auto min = static_cast<column_t::index_t>(std::stoull(col_node.attribute("min").value()));
auto max = static_cast<column_t::index_t>(std::stoull(col_node.attribute("max").value()));
auto width = std::stold(col_node.attribute("width").value());
bool custom = col_node.attribute("customWidth").value() == std::string("1");
auto column_style = static_cast<std::size_t>(col_node.attribute("style") ? std::stoull(col_node.attribute("style").value()) : 0);
for (auto column = min; column <= max; column++)
{
if (!sheet_.has_column_properties(column))
{
sheet_.add_column_properties(column, column_properties());
}
sheet_.get_column_properties(min).width = width;
sheet_.get_column_properties(min).style = column_style;
sheet_.get_column_properties(min).custom = custom;
}
}
if (root_node.child("autoFilter"))
{
auto auto_filter_node = root_node.child("autoFilter");
xlnt::range_reference ref(auto_filter_node.attribute("ref").value());
sheet_.auto_filter(ref);
}
return true;
}
void worksheet_serializer::write_worksheet(pugi::xml_document &xml) const
{
auto root_node = xml.append_child("worksheet");
root_node.append_attribute("xmlns").set_value(constants::get_namespace("spreadsheetml").c_str());
root_node.append_attribute("xmlns:r").set_value(constants::get_namespace("r").c_str());
auto sheet_pr_node = root_node.append_child("sheetPr");
auto outline_pr_node = sheet_pr_node.append_child("outlinePr");
outline_pr_node.append_attribute("summaryBelow").set_value("1");
outline_pr_node.append_attribute("summaryRight").set_value("1");
if (!sheet_.get_page_setup().is_default())
{
auto page_set_up_pr_node = sheet_pr_node.append_child("pageSetUpPr");
page_set_up_pr_node.append_attribute("fitToPage").set_value(sheet_.get_page_setup().fit_to_page() ? "1" : "0");
}
auto dimension_node = root_node.append_child("dimension");
dimension_node.append_attribute("ref").set_value(sheet_.calculate_dimension().to_string().c_str());
auto sheet_views_node = root_node.append_child("sheetViews");
auto sheet_view_node = sheet_views_node.append_child("sheetView");
sheet_view_node.append_attribute("workbookViewId").set_value("0");
std::string active_pane = "bottomRight";
if (sheet_.has_frozen_panes())
{
auto pane_node = sheet_view_node.append_child("pane");
if (sheet_.get_frozen_panes().get_column_index() > 1)
{
pane_node.append_attribute("xSplit").set_value(std::to_string(sheet_.get_frozen_panes().get_column_index().index - 1).c_str());
active_pane = "topRight";
}
if (sheet_.get_frozen_panes().get_row() > 1)
{
pane_node.append_attribute("ySplit").set_value(std::to_string(sheet_.get_frozen_panes().get_row() - 1).c_str());
active_pane = "bottomLeft";
}
if (sheet_.get_frozen_panes().get_row() > 1 && sheet_.get_frozen_panes().get_column_index() > 1)
{
auto top_right_node = sheet_view_node.append_child("selection");
top_right_node.append_attribute("pane").set_value("topRight");
auto bottom_left_node = sheet_view_node.append_child("selection");
bottom_left_node.append_attribute("pane").set_value("bottomLeft");
active_pane = "bottomRight";
}
pane_node.append_attribute("topLeftCell").set_value(sheet_.get_frozen_panes().to_string().c_str());
pane_node.append_attribute("activePane").set_value(active_pane.c_str());
pane_node.append_attribute("state").set_value("frozen");
}
auto selection_node = sheet_view_node.append_child("selection");
if (sheet_.has_frozen_panes())
{
if (sheet_.get_frozen_panes().get_row() > 1 && sheet_.get_frozen_panes().get_column_index() > 1)
{
selection_node.append_attribute("pane").set_value("bottomRight");
}
else if (sheet_.get_frozen_panes().get_row() > 1)
{
selection_node.append_attribute("pane").set_value("bottomLeft");
}
else if (sheet_.get_frozen_panes().get_column_index() > 1)
{
selection_node.append_attribute("pane").set_value("topRight");
}
}
std::string active_cell = "A1";
selection_node.append_attribute("activeCell").set_value(active_cell.c_str());
selection_node.append_attribute("sqref").set_value(active_cell.c_str());
auto sheet_format_pr_node = root_node.append_child("sheetFormatPr");
sheet_format_pr_node.append_attribute("baseColWidth").set_value("10");
sheet_format_pr_node.append_attribute("defaultRowHeight").set_value("15");
bool has_column_properties = false;
for (auto column = sheet_.get_lowest_column(); column <= sheet_.get_highest_column(); column++)
{
if (sheet_.has_column_properties(column))
{
has_column_properties = true;
break;
}
}
if (has_column_properties)
{
auto cols_node = root_node.append_child("cols");
for (auto column = sheet_.get_lowest_column(); column <= sheet_.get_highest_column(); column++)
{
if(!sheet_.has_column_properties(column))
{
continue;
}
const auto &props = sheet_.get_column_properties(column);
auto col_node = cols_node.append_child("col");
col_node.append_attribute("min").set_value(std::to_string(column.index).c_str());
col_node.append_attribute("max").set_value(std::to_string(column.index).c_str());
col_node.append_attribute("width").set_value(std::to_string(props.width).c_str());
col_node.append_attribute("style").set_value(std::to_string(props.style).c_str());
col_node.append_attribute("customWidth").set_value(props.custom ? "1" : "0");
}
}
std::unordered_map<std::string, std::string> hyperlink_references;
auto sheet_data_node = root_node.append_child("sheetData");
const auto &shared_strings = sheet_.get_workbook().get_shared_strings();
for (auto row : sheet_.rows())
{
row_t min = static_cast<row_t>(row.num_cells());
row_t max = 0;
bool any_non_null = false;
for (auto cell : row)
{
min = std::min(min, cell.get_column().index);
max = std::max(max, cell.get_column().index);
if (!cell.garbage_collectible())
{
any_non_null = true;
}
}
if (!any_non_null)
{
continue;
}
auto row_node = sheet_data_node.append_child("row");
row_node.append_attribute("r").set_value(std::to_string(row.front().get_row()).c_str());
row_node.append_attribute("spans").set_value((std::to_string(min) + ":" + std::to_string(max)).c_str());
if (sheet_.has_row_properties(row.front().get_row()))
{
row_node.append_attribute("customHeight").set_value("1");
auto height = sheet_.get_row_properties(row.front().get_row()).height;
if (height == std::floor(height))
{
row_node.append_attribute("ht").set_value((std::to_string(static_cast<long long int>(height)) + ".0").c_str());
}
else
{
row_node.append_attribute("ht").set_value(std::to_string(height).c_str());
}
}
// row_node.append_attribute("x14ac:dyDescent", 0.25);
for (auto cell : row)
{
if (!cell.garbage_collectible())
{
if (cell.has_hyperlink())
{
hyperlink_references[cell.get_hyperlink().get_id()] = cell.get_reference().to_string();
}
auto cell_node = row_node.append_child("c");
cell_node.append_attribute("r").set_value(cell.get_reference().to_string().c_str());
if (cell.get_data_type() == cell::type::string)
{
if (cell.has_formula())
{
cell_node.append_attribute("t").set_value("str");
cell_node.append_child("f").text().set(cell.get_formula().c_str());
cell_node.append_child("v").text().set(cell.to_string().c_str());
continue;
}
int match_index = -1;
for (std::size_t i = 0; i < shared_strings.size(); i++)
{
if (shared_strings[i] == cell.get_value<text>())
{
match_index = static_cast<int>(i);
break;
}
}
if (match_index == -1)
{
if (cell.get_value<std::string>().empty())
{
cell_node.append_attribute("t").set_value("s");
}
else
{
cell_node.append_attribute("t").set_value("inlineStr");
auto inline_string_node = cell_node.append_child("is");
inline_string_node.append_child("t").text().set(cell.get_value<std::string>().c_str());
}
}
else
{
cell_node.append_attribute("t").set_value("s");
auto value_node = cell_node.append_child("v");
value_node.text().set(std::to_string(match_index).c_str());
}
}
else
{
if (cell.get_data_type() != cell::type::null)
{
if (cell.get_data_type() == cell::type::boolean)
{
cell_node.append_attribute("t").set_value("b");
auto value_node = cell_node.append_child("v");
value_node.text().set(cell.get_value<bool>() ? "1" : "0");
}
else if (cell.get_data_type() == cell::type::numeric)
{
if (cell.has_formula())
{
cell_node.append_child("f").text().set(cell.get_formula().c_str());
cell_node.append_child("v").text().set(cell.to_string().c_str());
continue;
}
cell_node.append_attribute("t").set_value("n");
auto value_node = cell_node.append_child("v");
if (is_integral(cell.get_value<long double>()))
{
value_node.text().set(std::to_string(cell.get_value<long long>()).c_str());
}
else
{
std::stringstream ss;
ss.precision(20);
ss << cell.get_value<long double>();
ss.str();
value_node.text().set(ss.str().c_str());
}
}
}
else if (cell.has_formula())
{
cell_node.append_child("f").text().set(cell.get_formula().c_str());
cell_node.append_child("v");
continue;
}
}
if (cell.has_format())
{
cell_node.append_attribute("s").set_value(std::to_string(cell.get_format_id()).c_str());
}
}
}
}
if (sheet_.has_auto_filter())
{
auto auto_filter_node = root_node.append_child("autoFilter");
auto_filter_node.append_attribute("ref").set_value(sheet_.get_auto_filter().to_string().c_str());
}
if (!sheet_.get_merged_ranges().empty())
{
auto merge_cells_node = root_node.append_child("mergeCells");
merge_cells_node.append_attribute("count").set_value(std::to_string(sheet_.get_merged_ranges().size()).c_str());
for (auto merged_range : sheet_.get_merged_ranges())
{
auto merge_cell_node = merge_cells_node.append_child("mergeCell");
merge_cell_node.append_attribute("ref").set_value(merged_range.to_string().c_str());
}
}
if (!sheet_.get_relationships().empty())
{
auto hyperlinks_node = root_node.append_child("hyperlinks");
for (const auto &relationship : sheet_.get_relationships())
{
auto hyperlink_node = hyperlinks_node.append_child("hyperlink");
hyperlink_node.append_attribute("display").set_value(relationship.get_target_uri().c_str());
hyperlink_node.append_attribute("ref").set_value(hyperlink_references.at(relationship.get_id()).c_str());
hyperlink_node.append_attribute("r:id").set_value(relationship.get_id().c_str());
}
}
if (!sheet_.get_page_setup().is_default())
{
auto print_options_node = root_node.append_child("printOptions");
print_options_node.append_attribute("horizontalCentered").set_value(
sheet_.get_page_setup().get_horizontal_centered() ? "1" : "0");
print_options_node.append_attribute("verticalCentered").set_value(
sheet_.get_page_setup().get_vertical_centered() ? "1" : "0");
}
auto page_margins_node = root_node.append_child("pageMargins");
//TODO: there must be a better way to do this
auto remove_trailing_zeros = [](const std::string &n)
{
auto decimal = n.find('.');
if (decimal == std::string::npos) return n;
auto index = n.size() - 1;
while (index >= decimal && n[index] == '0')
{
index--;
}
if(index == decimal)
{
return n.substr(0, decimal);
}
return n.substr(0, index + 1);
};
page_margins_node.append_attribute("left").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_left())).c_str());
page_margins_node.append_attribute("right").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_right())).c_str());
page_margins_node.append_attribute("top").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_top())).c_str());
page_margins_node.append_attribute("bottom").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_bottom())).c_str());
page_margins_node.append_attribute("header").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_header())).c_str());
page_margins_node.append_attribute("footer").set_value(remove_trailing_zeros(std::to_string(sheet_.get_page_margins().get_footer())).c_str());
if (!sheet_.get_page_setup().is_default())
{
auto page_setup_node = root_node.append_child("pageSetup");
std::string orientation_string =
sheet_.get_page_setup().get_orientation() == orientation::landscape ? "landscape" : "portrait";
page_setup_node.append_attribute("orientation").set_value(orientation_string.c_str());
page_setup_node.append_attribute("paperSize").set_value(
std::to_string(static_cast<int>(sheet_.get_page_setup().get_paper_size())).c_str());
page_setup_node.append_attribute("fitToHeight").set_value(sheet_.get_page_setup().fit_to_height() ? "1" : "0");
page_setup_node.append_attribute("fitToWidth").set_value(sheet_.get_page_setup().fit_to_width() ? "1" : "0");
}
if (!sheet_.get_header_footer().is_default())
{
auto header_footer_node = root_node.append_child("headerFooter");
auto odd_header_node = header_footer_node.append_child("oddHeader");
std::string header_text =
"&L&\"Calibri,Regular\"&K000000Left Header Text&C&\"Arial,Regular\"&6&K445566Center Header "
"Text&R&\"Arial,Bold\"&8&K112233Right Header Text";
odd_header_node.text().set(header_text.c_str());
auto odd_footer_node = header_footer_node.append_child("oddFooter");
std::string footer_text =
"&L&\"Times New Roman,Regular\"&10&K445566Left Footer Text_x000D_And &D and &T&C&\"Times New "
"Roman,Bold\"&12&K778899Center Footer Text &Z&F on &A&R&\"Times New Roman,Italic\"&14&KAABBCCRight Footer "
"Text &P of &N";
odd_footer_node.text().set(footer_text.c_str());
}
}
} // namespace xlnt

View File

@ -1,59 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <string>
#include <vector>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/worksheet/worksheet.hpp>
namespace pugi {
class xml_document;
} // namespace pugi
namespace xlnt {
class relationship;
class workbook;
class worksheet;
namespace detail { struct stylesheet; }
/// <summary>
/// Manages converting a worksheet to and from XML.
/// </summary>
class XLNT_CLASS worksheet_serializer
{
public:
worksheet_serializer(worksheet sheet);
bool read_worksheet(const pugi::xml_document &xml, detail::stylesheet &stylesheet);
void write_worksheet(pugi::xml_document &xml) const;
private:
worksheet sheet_;
};
} // namespace xlnt

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,126 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <cstdint>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <detail/include_pugixml.hpp>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/zip_file.hpp>
namespace xlnt {
class path;
class relationship;
class workbook;
namespace detail {
/// <summary>
/// Handles writing a workbook into an XLSX file.
/// </summary>
class XLNT_CLASS xlsx_consumer
{
public:
xlsx_consumer(workbook &destination);
void read(const path &source);
void read(std::istream &source);
void read(const std::vector<std::uint8_t> &source);
private:
/// <summary>
/// Read all the files needed from the XLSX archive and initialize all of
/// the data in the workbook to match.
/// </summary>
void populate_workbook();
// Package Parts
void read_manifest();
void read_core_properties(const pugi::xml_node root);
void read_extended_properties(const pugi::xml_node root);
void read_custom_file_properties(const pugi::xml_node root);
// SpreadsheetML-Specific Package Parts
void read_workbook(const pugi::xml_node root);
// Workbook Relationship Target Parts
void read_calculation_chain(const pugi::xml_node root);
void read_connections(const pugi::xml_node root);
void read_custom_property(const pugi::xml_node root);
void read_custom_xml_mappings(const pugi::xml_node root);
void read_external_workbook_references(const pugi::xml_node root);
void read_metadata(const pugi::xml_node root);
void read_pivot_table(const pugi::xml_node root);
void read_shared_string_table(const pugi::xml_node root);
void read_shared_workbook_revision_headers(const pugi::xml_node root);
void read_shared_workbook(const pugi::xml_node root);
void read_shared_workbook_user_data(const pugi::xml_node root);
void read_styles(const pugi::xml_node root);
void read_theme(const pugi::xml_node root);
void read_volatile_dependencies(const pugi::xml_node root);
void read_chartsheet(const pugi::xml_node root, const std::string &title);
void read_dialogsheet(const pugi::xml_node root, const std::string &title);
void read_worksheet(const pugi::xml_node root, const std::string &title);
// Sheet Relationship Target Parts
void read_comments(const pugi::xml_node root);
void read_drawings(const pugi::xml_node root);
// Unknown Parts
void read_unknown_parts(const pugi::xml_node root);
void read_unknown_relationships(const pugi::xml_node root);
/// <summary>
/// A reference to the archive from which files representing the workbook
/// are read.
/// </summary>
zip_file source_;
/// <summary>
/// Worksheet titles from workbook.xml are saved here so we know what its
/// title is when readind the associated worksheet XML.
/// </summary>
std::unordered_map<std::string, std::tuple<std::string, std::size_t, std::size_t>> worksheet_rel_title_id_index_map_;
/// <summary>
/// A reference to the workbook which is being read.
/// </summary>
workbook &destination_;
};
} // namespace detail
} // namespace xlnt

File diff suppressed because it is too large Load Diff

View File

@ -28,20 +28,23 @@
#include <vector>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/zip_file.hpp>
namespace xlnt {
class path;
class relationship;
class workbook;
class zip_file;
namespace detail {
/// <summary>
/// Handles writing a workbook into an XLSX file.
/// </summary>
class XLNT_CLASS xlsx_writer
class XLNT_CLASS xlsx_producer
{
public:
xlsx_writer(const workbook &target);
xlsx_producer(const workbook &target);
void write(const path &destination);
@ -56,10 +59,62 @@ private:
/// </summary>
void populate_archive(zip_file &archive);
// Package Parts
void write_package_relationships();
void write_content_types();
void write_app_properties();
void write_core_properties();
void write_custom_file_properties();
// SpreadsheetML-Specific Package Parts
void write_workbook();
void write_workbook_relationships();
// Workbook Relationship Target Parts
void write_calculation_chain();
void write_connections();
void write_custom_property();
void write_custom_xml_mappings();
void write_external_workbook_references();
void write_metadata();
void write_pivot_table();
void write_shared_string_table();
void write_shared_workbook_revision_headers();
void write_shared_workbook();
void write_shared_workbook_user_data();
void write_styles();
void write_theme();
void write_volatile_dependencies();
void write_chartsheet(const relationship &rel);
void write_dialogsheet(const relationship &rel);
bool read_worksheet(const relationship &rel);
void write_worksheet(const relationship &rel);
// Sheet Relationship Target Parts
void write_comments();
void write_drawings();
// Unknown Parts
void write_unknown_parts();
void write_unknown_relationships();
/// <summary>
/// A reference to the workbook which is the object of read/write operations.
/// </summary>
const workbook &target_;
const workbook &source_;
/// <summary>
/// A reference to the archive into which files representing the workbook
/// will be written.
/// </summary>
zip_file destination_;
};
} // namespace detail
} // namespace xlnt

View File

@ -1,325 +0,0 @@
#include <detail/xlsx_writer.hpp>
#include <detail/constants.hpp>
#include <detail/include_pugixml.hpp>
#include <xlnt/utils/path.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/packaging/zip_file.hpp>
#include <xlnt/workbook/const_worksheet_iterator.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/worksheet.hpp>
namespace {
void write_document_to_archive(const pugi::xml_document &document,
const xlnt::path &archive_path, xlnt::zip_file &archive)
{
std::ostringstream out_stream;
document.save(out_stream);
archive.write_string(out_stream.str(), archive_path);
}
// Package Parts
void write_package_relationships(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto relationships_node = document.append_child("Relationships");
relationships_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/package/2006/relationships");
for (const auto &relationship : target.get_root_relationships())
{
auto relationship_node = relationships_node.append_child("Relationship");
relationship_node.append_attribute("Id").set_value(relationship.get_id().c_str());
relationship_node.append_attribute("Type").set_value(relationship.get_type_string().c_str());
relationship_node.append_attribute("Target").set_value(relationship.get_target_uri().c_str());
}
write_document_to_archive(document, xlnt::constants::part_root_relationships(), archive);
}
void write_content_types(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto types_node = document.append_child("Types");
types_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/package/2006/content-types");
for (const auto &default_type : target.get_manifest().get_default_types())
{
auto default_node = types_node.append_child("Default");
default_node.append_attribute("Extension").set_value(default_type.second.get_extension().c_str());
default_node.append_attribute("ContentType").set_value(default_type.second.get_content_type().c_str());
}
for (const auto &override_type : target.get_manifest().get_override_types())
{
auto override_node = types_node.append_child("Override");
override_node.append_attribute("PartName").set_value(override_type.second.get_part().to_string('/').c_str());
override_node.append_attribute("ContentType").set_value(override_type.second.get_content_type().c_str());
}
write_document_to_archive(document, xlnt::constants::part_content_types(), archive);
}
void write_app_properties(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("appProperties");
write_document_to_archive(document, xlnt::constants::part_app(), archive);
}
void write_core_properties(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("coreProperties");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_custom_file_properties(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("customFileProperties");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
// SpreadsheetML Package Parts
void write_workbook(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto workbook_node = document.append_child("workbook");
workbook_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
workbook_node.append_attribute("xmlns:r").set_value("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
auto sheets_node = workbook_node.append_child("sheets");
auto sheet_node = sheets_node.append_child("sheet");
sheet_node.append_attribute("name").set_value(1);
sheet_node.append_attribute("sheetId").set_value(1);
sheet_node.append_attribute("r:id").set_value("rId1");
write_document_to_archive(document, xlnt::path("workbook.xml"), archive);
}
void write_workbook_relationships(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto relationships_node = document.append_child("Relationships");
relationships_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/package/2006/relationships");
auto relationship_node = relationships_node.append_child("Relationship");
relationship_node.append_attribute("Id").set_value("rId1");
relationship_node.append_attribute("Type").set_value("http://purl.oclc.org/ooxml/officeDocument/relationships/worksheet");
relationship_node.append_attribute("Target").set_value("sheet1.xml");
write_document_to_archive(document, xlnt::path("_rels/workbook.xml.rels"), archive);
}
// Workbook Relationship Target Parts
void write_calculation_chain(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("calcChain");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_chartsheet(const xlnt::worksheet &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("chartsheet");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_connections(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("connections");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_custom_property(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("customProperty");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_custom_xml_mappings(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("connections");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_dialogsheet(const xlnt::worksheet &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("dialogsheet");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_external_workbook_references(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("externalLink");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_metadata(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("metadata");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_pivot_table(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("pivotTableDefinition");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_shared_string_table(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("sst");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_shared_workbook_revision_headers(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("headers");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_shared_workbook(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("revisions");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_shared_workbook_user_data(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("users");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_styles(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("styleSheet");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_theme(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("theme");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_volatile_dependencies(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("volTypes");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_worksheet(const xlnt::worksheet &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto worksheet_node = document.append_child("worksheet");
worksheet_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
worksheet_node.append_attribute("xmlns:r").set_value("http://schemas.openxmlformats.org/package/2006/relationships");
worksheet_node.append_child("sheetData");
write_document_to_archive(document, xlnt::path("sheet1.xml"), archive);
}
// Sheet Relationship Target Parts
void write_comments(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("comments");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_drawings(const xlnt::worksheet &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("wsDr");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
// Unknown Parts
void write_unknown_parts(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("relationships");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
void write_unknown_relationships(const xlnt::workbook &target, xlnt::zip_file &archive)
{
pugi::xml_document document;
auto root_node = document.append_child("Relationships");
write_document_to_archive(document, xlnt::constants::part_core(), archive);
}
} // namespace
namespace xlnt
{
xlsx_writer::xlsx_writer(const workbook &target) : target_(target)
{
}
void xlsx_writer::write(const path &destination)
{
xlnt::zip_file archive;
populate_archive(archive);
archive.save(destination);
}
void xlsx_writer::write(std::ostream &destination)
{
xlnt::zip_file archive;
populate_archive(archive);
archive.save(destination);
}
void xlsx_writer::write(std::vector<std::uint8_t> &destination)
{
xlnt::zip_file archive;
populate_archive(archive);
archive.save(destination);
}
void xlsx_writer::populate_archive(zip_file &archive)
{
write_package_relationships(target_, archive);
write_content_types(target_, archive);
write_workbook(target_, archive);
write_workbook_relationships(target_, archive);
for (auto ws : target_)
{
write_worksheet(ws, archive);
}
}
} // namepsace xlnt

View File

@ -1,58 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/packaging/default_type.hpp>
namespace xlnt {
default_type::default_type()
{
}
default_type::default_type(const std::string &extension, const std::string &content_type)
: extension_(extension), content_type_(content_type)
{
}
default_type::default_type(const default_type &other) : extension_(other.extension_), content_type_(other.content_type_)
{
}
default_type &default_type::operator=(const default_type &other)
{
extension_ = other.extension_;
content_type_ = other.content_type_;
return *this;
}
std::string default_type::get_extension() const
{
return extension_;
}
std::string default_type::get_content_type() const
{
return content_type_;
}
} // namespace xlnt

View File

@ -22,59 +22,387 @@
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <algorithm>
#include <stdexcept>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/utils/exceptions.hpp>
namespace {
std::string to_string(xlnt::content_type type)
{
return "";
}
xlnt::content_type from_string(const std::string &type_string)
{
return xlnt::content_type::unknown;
}
}
namespace xlnt {
void manifest::clear()
{
default_types_.clear();
override_types_.clear();
clear_types();
clear_relationships();
part_infos_.clear();
}
void manifest::clear_types()
{
clear_default_types();
clear_override_types();
}
void manifest::clear_default_types()
{
extension_content_type_map_.clear();
}
void manifest::clear_override_types()
{
for (auto &info : part_infos_)
{
info.second.content_type.clear();
}
}
void manifest::clear_relationships()
{
clear_package_relationships();
clear_part_relationships();
}
void manifest::clear_package_relationships()
{
package_relationships_.clear();
}
void manifest::clear_part_relationships()
{
for (auto &info : part_infos_)
{
info.second.relationships.clear();
}
}
bool manifest::has_package_relationship(relationship::type type) const
{
for (const auto &rel : package_relationships_)
{
if (rel.get_type() == type)
{
return true;
}
}
return false;
}
bool manifest::has_part_relationship(const path &part, relationship::type type) const
{
for (const auto &rel : part_infos_.at(part).relationships)
{
if (rel.get_type() == type)
{
return true;
}
}
return false;
}
relationship manifest::get_package_relationship(relationship::type type) const
{
for (const auto &rel : package_relationships_)
{
if (rel.get_type() == type)
{
return rel;
}
}
throw key_not_found();
}
std::vector<relationship> manifest::get_package_relationships(relationship::type type) const
{
std::vector<relationship> matches;
for (const auto &rel : package_relationships_)
{
if (rel.get_type() == type)
{
matches.push_back(rel);
}
}
return matches;
}
relationship manifest::get_part_relationship(const path &part, relationship::type type) const
{
for (const auto &rel : part_infos_.at(part).relationships)
{
if (rel.get_type() == type)
{
return rel;
}
}
throw key_not_found();
}
std::vector<relationship> manifest::get_part_relationships(const path &part, relationship::type type) const
{
std::vector<relationship> matches;
for (const auto &rel : part_infos_.at(part).relationships)
{
if (rel.get_type() == type)
{
matches.push_back(rel);
}
}
return matches;
}
content_type manifest::get_part_content_type(const path &part) const
{
return from_string(get_part_content_type_string(part));
}
std::string manifest::get_part_content_type_string(const path &part) const
{
if (part_infos_.find(part) == part_infos_.end())
{
throw key_not_found();
}
return part_infos_.at(part).content_type;
}
void manifest::register_part(const path &part, const path &parent, const std::string &content_type, relationship::type relation)
{
part_infos_[part] = { content_type, {} };
relationship rel(next_package_relationship_id(), relation, part, target_mode::internal);
part_infos_[parent].relationships.push_back(rel);
}
void manifest::register_part(const path &parent, const relationship &rel, const std::string &content_type)
{
part_infos_[rel.get_target_uri()] = { content_type,{} };
part_infos_[parent].relationships.push_back(rel);
}
void manifest::register_package_part(const path &part, const std::string &content_type, relationship::type relation)
{
part_infos_[part] = { content_type, {} };
relationship rel(next_package_relationship_id(), relation, part, target_mode::internal);
package_relationships_.push_back(rel);
}
void manifest::unregister_part(const path &part)
{
if (part_infos_.find(part) != part_infos_.end())
{
part_infos_.erase(part);
}
else
{
throw key_not_found();
}
auto package_rels_iter = package_relationships_.begin();
while (package_rels_iter != package_relationships_.end())
{
if (package_rels_iter->get_target_uri() == part)
{
package_rels_iter = package_relationships_.erase(package_rels_iter);
continue;
}
++package_rels_iter;
}
for (auto &current_part : part_infos_)
{
auto rels_iter = current_part.second.relationships.begin();
while (rels_iter != current_part.second.relationships.end())
{
if (rels_iter->get_target_uri() == part)
{
rels_iter = current_part.second.relationships.erase(rels_iter);
continue;
}
++rels_iter;
}
}
}
std::vector<path> manifest::get_parts() const
{
std::vector<path> parts;
for (const auto &part : part_infos_)
{
parts.push_back(part.first);
}
return parts;
}
bool manifest::has_part(const path &part) const
{
return part_infos_.find(part) != part_infos_.end();
}
std::vector<relationship> manifest::get_package_relationships() const
{
return package_relationships_;
}
relationship manifest::get_package_relationship(const std::string &rel_id)
{
for (const auto rel : package_relationships_)
{
if (rel.get_id() == rel_id)
{
return rel;
}
}
throw key_not_found();
}
std::vector<relationship> manifest::get_part_relationships(const path &part) const
{
std::vector<relationship> matches;
for (const auto &rel : part_infos_.at(part).relationships)
{
matches.push_back(rel);
}
return matches;
}
relationship manifest::get_part_relationship(const path &part, const std::string &rel_id) const
{
if (part_infos_.find(part) == part_infos_.end())
{
throw key_not_found();
}
for (const auto &rel : part_infos_.at(part).relationships)
{
if (rel.get_id() == rel_id)
{
return rel;
}
}
throw key_not_found();
}
std::string manifest::register_external_package_relationship(relationship::type type, const std::string &target_uri)
{
return register_external_package_relationship(type, target_uri, next_package_relationship_id());
}
std::string manifest::register_external_package_relationship(relationship::type type, const std::string &target_uri, const std::string &rel_id)
{
relationship rel(rel_id, type, path(target_uri), target_mode::external);
package_relationships_.push_back(rel);
return rel_id;
}
std::string manifest::register_external_relationship(const path &source_part, relationship::type type, const std::string &target_uri)
{
return register_external_relationship(source_part, type, target_uri, next_relationship_id(source_part));
}
std::string manifest::register_external_relationship(const path &source_part, relationship::type type, const std::string &target_uri, const std::string &rel_id)
{
relationship rel(rel_id, type, path(target_uri), target_mode::external);
part_infos_.at(source_part).relationships.push_back(rel);
return rel_id;
}
bool manifest::has_default_type(const std::string &extension) const
{
return default_types_.find(extension) != default_types_.end();
return extension_content_type_map_.find(extension) != extension_content_type_map_.end();
}
bool manifest::has_override_type(const path &part) const
std::vector<std::string> manifest::get_default_type_extensions() const
{
auto absolute = part.is_absolute() ? part : part.make_absolute(path("/"));
return override_types_.find(absolute) != override_types_.end();
}
std::vector<std::string> extensions;
void manifest::add_default_type(const std::string &extension, const std::string &content_type)
{
default_types_[extension] = default_type(extension, content_type);
}
for (const auto &extension_type_pair : extension_content_type_map_)
{
extensions.push_back(extension_type_pair.first);
}
void manifest::add_override_type(const path &part, const std::string &content_type)
{
auto absolute = part.is_absolute() ? part : part.make_absolute(path("/"));
override_types_[absolute] = override_type(absolute, content_type);
return extensions;
}
std::string manifest::get_default_type(const std::string &extension) const
{
return default_types_.at(extension).get_content_type();
if (extension_content_type_map_.find(extension) == extension_content_type_map_.end())
{
throw key_not_found();
}
return extension_content_type_map_.at(extension);
}
std::string manifest::get_override_type(const path &part) const
void manifest::register_default_type(const std::string &extension, const std::string &content_type)
{
auto absolute = part.is_absolute() ? part : part.make_absolute(path("/"));
return override_types_.at(absolute).get_content_type();
extension_content_type_map_[extension] = content_type;
}
const manifest::default_types_container &manifest::get_default_types() const
void manifest::unregister_default_type(const std::string &extension)
{
return default_types_;
extension_content_type_map_.erase(extension);
}
const manifest::override_types_container &manifest::get_override_types() const
std::string manifest::next_package_relationship_id() const
{
return override_types_;
std::string r_id("rId");
std::size_t index = 1;
while (std::find_if(package_relationships_.begin(), package_relationships_.end(),
[&](const relationship &r) { return r.get_id() == r_id + std::to_string(index); })
!= package_relationships_.end())
{
++index;
}
return r_id + std::to_string(index);
}
std::string manifest::next_relationship_id(const path &part) const
{
std::string r_id("rId");
std::size_t index = 1;
const auto &part_rels = get_part_relationships(part);
while (std::find_if(part_rels.begin(), part_rels.end(),
[&](const relationship &r) { return r.get_id() == r_id + std::to_string(index); })
!= part_rels.end())
{
++index;
}
return r_id + std::to_string(index);
}
} // namespace xlnt

View File

@ -1,60 +0,0 @@
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/packaging/override_type.hpp>
namespace xlnt {
override_type::override_type()
{
}
override_type::override_type(const path &part, const std::string &content_type)
: part_(part), content_type_(content_type)
{
}
override_type::override_type(const override_type &other)
: part_(other.part_), content_type_(other.content_type_)
{
}
override_type &override_type::operator=(const override_type &other)
{
part_ = other.part_;
content_type_ = other.content_type_;
return *this;
}
path override_type::get_part() const
{
return part_;
}
std::string override_type::get_content_type() const
{
return content_type_;
}
} // namespace xlnt

View File

@ -1,14 +0,0 @@
//
// override_type.hpp
// xlnt
//
// Created by Thomas Fussell on 11/18/15.
//
//
#ifndef override_type_hpp
#define override_type_hpp
#include <stdio.h>
#endif /* override_type_hpp */

View File

@ -25,103 +25,15 @@
namespace xlnt {
relationship::relationship(type t, const std::string &r_id, const std::string &target_uri)
: type_(t), id_(r_id), source_uri_(""), target_uri_(target_uri), target_mode_(target_mode::internal)
{
if (t == type::hyperlink)
{
target_mode_ = target_mode::external;
}
}
relationship::relationship()
: type_(type::invalid), id_(""), source_uri_(""), target_uri_(""), target_mode_(target_mode::internal)
{
}
relationship::type relationship::type_from_string(const std::string &type_string)
{
if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties")
{
return type::extended_properties;
}
else if (type_string == "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties")
{
return type::core_properties;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")
{
return type::office_document;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet")
{
return type::worksheet;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings")
{
return type::shared_strings;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles")
{
return type::styles;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme")
{
return type::theme;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink")
{
return type::hyperlink;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet")
{
return type::chartsheet;
}
else if (type_string == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml")
{
return type::custom_xml;
}
else if (type_string == "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail")
{
return type::thumbnail;
}
return type::invalid;
}
std::string relationship::type_to_string(type t)
{
switch (t)
{
case type::extended_properties:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
case type::core_properties:
return "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
case type::office_document:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
case type::worksheet:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet";
case type::shared_strings:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
case type::styles:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
case type::theme:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
case type::hyperlink:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
case type::chartsheet:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet";
case type::custom_xml:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";
case type::thumbnail:
return "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail";
default:
return "??";
}
}
relationship::relationship(const std::string &t, const std::string &r_id, const std::string &target_uri)
: relationship(type_from_string(t), r_id, target_uri)
relationship::relationship(const std::string &id, type t, const path &target, target_mode mode)
: id_(id),
type_(t),
target_uri_(target),
target_mode_(mode)
{
}
@ -130,17 +42,12 @@ std::string relationship::get_id() const
return id_;
}
std::string relationship::get_source_uri() const
{
return source_uri_;
}
target_mode relationship::get_target_mode() const
{
return target_mode_;
}
std::string relationship::get_target_uri() const
path relationship::get_target_uri() const
{
return target_uri_;
}
@ -149,15 +56,13 @@ relationship::type relationship::get_type() const
{
return type_;
}
std::string relationship::get_type_string() const
{
return type_to_string(type_);
}
bool relationship::operator==(const relationship &rhs) const
{
return type_ == rhs.type_ && id_ == rhs.id_ && source_uri_ == rhs.source_uri_ &&
target_uri_ == rhs.target_uri_ && target_mode_ == rhs.target_mode_;
return type_ == rhs.type_
&& id_ == rhs.id_
&& target_uri_ == rhs.target_uri_
&& target_mode_ == rhs.target_mode_;
}
} // namespace xlnt

View File

@ -3,9 +3,10 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/workbook_serializer.hpp>
#include <detail/constants.hpp>
#include <helpers/path_helper.hpp>
#include <helpers/xml_helper.hpp>
#include <xlnt/workbook/workbook.hpp>
class test_core : public CxxTest::TestSuite
{
@ -23,19 +24,12 @@ public:
void test_read_sheets_titles()
{
auto path = path_helper::get_data_directory("genuine/empty.xlsx");
const std::vector<std::string> expected_titles = {"Sheet1 - Text", "Sheet2 - Numbers", "Sheet3 - Formulas", "Sheet4 - Dates"};
std::size_t i = 0;
xlnt::workbook wb;
wb.load(path);
wb.load(path_helper::get_data_directory("genuine/empty.xlsx"));
for(auto sheet : wb)
{
TS_ASSERT_EQUALS(sheet.get_title(), expected_titles.at(i++));
}
TS_ASSERT_EQUALS(wb.get_sheet_titles(), expected_titles);
}
void test_read_properties_core_libre()
@ -66,10 +60,10 @@ public:
wb.set_last_modified_by("SOMEBODY");
wb.set_created(xlnt::datetime(2010, 4, 1, 20, 30, 00));
wb.set_modified(xlnt::datetime(2010, 4, 5, 14, 5, 30));
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_properties_core(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/core.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/core.xml"),
wb, xlnt::constants::part_core()));
}
void test_write_properties_app()
@ -80,9 +74,9 @@ public:
wb.set_company("Company");
wb.create_sheet();
wb.create_sheet();
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_properties_app(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/app.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/core.xml"),
wb, xlnt::constants::part_core()));
}
};

View File

@ -4,10 +4,9 @@
#include <pugixml.hpp>
#include <cxxtest/TestSuite.h>
#include <detail/excel_serializer.hpp>
#include <detail/style_serializer.hpp>
#include <xlnt/xlnt.hpp>
#include <detail/stylesheet.hpp>
#include <xlnt/packaging/zip_file.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <helpers/xml_helper.hpp>
#include <helpers/path_helper.hpp>
@ -16,45 +15,23 @@ class test_stylesheet : public CxxTest::TestSuite
public:
void test_from_simple()
{
pugi::xml_document doc;
doc.load_file(path_helper::get_data_directory("reader/styles/simple-styles.xml").to_string().c_str());
xlnt::workbook wb;
xlnt::excel_serializer e(wb);
xlnt::style_serializer s(e.get_stylesheet());
TS_ASSERT(s.read_stylesheet(doc));
TS_ASSERT_EQUALS(e.get_stylesheet().number_formats.size(), 1);
wb.load(path_helper::get_data_directory("reader/styles/simple-styles.xml"));
}
void test_from_complex()
{
pugi::xml_document doc;
doc.load_file(path_helper::get_data_directory("reader/styles/complex-styles.xml").to_string().c_str());
xlnt::workbook wb;
xlnt::excel_serializer e(wb);
xlnt::style_serializer s(e.get_stylesheet());
TS_ASSERT(s.read_stylesheet(doc));
TS_ASSERT_EQUALS(e.get_stylesheet().borders.size(), 7);
TS_ASSERT_EQUALS(e.get_stylesheet().fills.size(), 6);
TS_ASSERT_EQUALS(e.get_stylesheet().fonts.size(), 8);
TS_ASSERT_EQUALS(e.get_stylesheet().number_formats.size(), 0);
wb.load(path_helper::get_data_directory("reader/styles/complex-styles.xml"));
}
void test_add_existing_style()
{
xlnt::workbook wb;
xlnt::excel_serializer e(wb);
TS_ASSERT_EQUALS(e.get_stylesheet().styles.size(), 1);
auto &s = wb.create_style("test");
TS_ASSERT_EQUALS(e.get_stylesheet().styles.size(), 2);
wb.add_style(s);
TS_ASSERT_EQUALS(e.get_stylesheet().styles.size(), 2);
xlnt::style copy;
copy = s;

View File

@ -191,11 +191,21 @@ path path::parent() const
return result;
}
std::string path::basename()
std::string path::basename() const
{
return parts_.empty() ? "" : parts_.back();
}
std::string path::extension() const
{
auto base = basename();
auto last_dot = base.find_last_of('.');
if (last_dot == std::string::npos) return "";
return base.substr(last_dot + 1);
}
// conversion
std::string path::to_string(char sep) const

View File

@ -4,11 +4,6 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/excel_serializer.hpp>
#include <detail/manifest_serializer.hpp>
#include <detail/relationship_serializer.hpp>
#include <detail/shared_strings_serializer.hpp>
#include <detail/workbook_serializer.hpp>
#include <helpers/path_helper.hpp>
#include <xlnt/cell/text.hpp>
#include <xlnt/cell/text_run.hpp>
@ -38,13 +33,15 @@ public:
std::ifstream fo(path.to_string(), std::ios::binary);
xlnt::workbook wb;
TS_ASSERT(wb.load(fo));
wb.load(fo);
TS_ASSERT(wb.get_sheet_titles().size() == 1);
}
void test_read_worksheet()
{
auto wb = standard_workbook();
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
auto sheet2 = wb.get_sheet_by_title("Sheet2 - Numbers");
TS_ASSERT_DIFFERS(sheet2, nullptr);
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5").get_value<std::string>());
@ -220,18 +217,6 @@ public:
TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value<xlnt::datetime>(), ws_win.get_cell("A1").get_value<xlnt::datetime>());
}
void test_repair_central_directory()
{
std::string data_a = "foobarbaz" + xlnt::excel_serializer::central_directory_signature();
std::string data_b = "bazbarfoo12345678901234567890";
auto f = xlnt::excel_serializer::repair_central_directory(data_a + data_b);
TS_ASSERT_EQUALS(f, data_a + data_b.substr(0, 18));
f = xlnt::excel_serializer::repair_central_directory(data_b);
TS_ASSERT_EQUALS(f, data_b);
}
void test_read_no_theme()
{
auto path = path_helper::get_data_directory("genuine/libreoffice_nrt.xlsx");
@ -322,46 +307,6 @@ public:
TS_ASSERT(ws.get_cell("A5").get_value<int>() == 49380);
TS_ASSERT(!ws.get_cell("A5").has_formula());
}
void test_read_rels()
{
{
std::vector<xlnt::relationship> expected =
{
{xlnt::relationship::type::theme, "rId3", "theme/theme1.xml"},
{xlnt::relationship::type::worksheet, "rId2", "worksheets/sheet1.xml"},
{xlnt::relationship::type::chartsheet, "rId1", "chartsheets/sheet1.xml"},
{xlnt::relationship::type::shared_strings, "rId5", "sharedStrings.xml"},
{xlnt::relationship::type::styles, "rId4", "styles.xml"}
};
auto path = path_helper::get_data_directory("reader/bug137.xlsx");
xlnt::zip_file archive(path);
xlnt::relationship_serializer serializer(archive);
TS_ASSERT_EQUALS(serializer.read_relationships("xl/workbook.xml"), expected);
}
{
std::vector<xlnt::relationship> expected =
{
{xlnt::relationship::type::custom_xml, "rId8", "../customXml/item3.xml"},
{xlnt::relationship::type::worksheet, "rId3", "/xl/worksheets/sheet.xml"},
{xlnt::relationship::type::custom_xml, "rId7", "../customXml/item2.xml"},
{xlnt::relationship::type::worksheet, "rId2", "/xl/worksheets/sheet2.xml"},
{xlnt::relationship::type::worksheet, "rId1", "/xl/worksheets/sheet3.xml"},
{xlnt::relationship::type::custom_xml, "rId6", "../customXml/item1.xml"},
{xlnt::relationship::type::styles, "rId5", "/xl/styles.xml"},
{xlnt::relationship::type::theme, "rId4", "/xl/theme/theme.xml"}
};
auto path = path_helper::get_data_directory("reader/bug304.xlsx");
xlnt::zip_file archive(path);
xlnt::relationship_serializer serializer(archive);
TS_ASSERT_EQUALS(serializer.read_relationships("xl/workbook.xml"), expected);
}
}
void test_read_content_types()
{
@ -388,7 +333,7 @@ public:
xlnt::workbook wb;
wb.load(path);
auto &result = wb.get_manifest().get_override_types();
auto &result = wb.get_manifest().get_parts();
if(result.size() != expected.size())
{
@ -398,8 +343,8 @@ public:
for(std::size_t i = 0; i < expected.size(); i++)
{
TS_ASSERT(wb.get_manifest().has_override_type(xlnt::path(expected[i].first)));
TS_ASSERT_EQUALS(wb.get_manifest().get_override_type(xlnt::path(expected[i].first)), expected[i].second);
TS_ASSERT(wb.get_manifest().has_part(xlnt::path(expected[i].first)));
TS_ASSERT_EQUALS(wb.get_manifest().get_part_content_type_string(xlnt::path(expected[i].first)), expected[i].second);
}
}
@ -458,26 +403,10 @@ public:
void test_read_shared_strings_with_runs()
{
std::string source =
"<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"1\" uniqueCount=\"1\">"
" <si>"
" <r>"
" <rPr>"
" <sz val=\"13\"/>"
" <color rgb=\"color\"/>"
" <rFont val=\"font\"/>"
" <family val=\"12\"/>"
" <scheme val=\"scheme\"/>"
" </rPr>"
" <t>string</t>"
" </r>"
" </si>"
"</sst>";
pugi::xml_document xml;
xml.load(source.c_str());
xlnt::workbook wb;
wb.load("reader/genuine/a.xlsx");
std::vector<xlnt::text> strings;
xlnt::shared_strings_serializer::read_shared_strings(xml, strings);
const auto &strings = wb.get_shared_strings();
TS_ASSERT_EQUALS(strings.size(), 1);
TS_ASSERT_EQUALS(strings.front().get_runs().size(), 1);
@ -486,26 +415,6 @@ public:
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_font(), "font");
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_family(), 12);
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_scheme(), "scheme");
std::string source_bad =
"<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"2\" uniqueCount=\"2\">"
" <si>"
" <r>"
" <rPr>"
" <sz val=\"13\"/>"
" <color rgb=\"color\"/>"
" <rFont val=\"font\"/>"
" <family val=\"12\"/>"
" <scheme val=\"scheme\"/>"
" </rPr>"
" <t>string</t>"
" </r>"
" </si>"
"</sst>";
pugi::xml_document xml_bad;
xml_bad.load(source_bad.c_str());
TS_ASSERT_THROWS(xlnt::shared_strings_serializer::read_shared_strings(xml_bad, strings), std::runtime_error);
}
void test_read_inlinestr()

View File

@ -3,9 +3,7 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/style_serializer.hpp>
#include <detail/stylesheet.hpp>
#include <detail/workbook_impl.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <helpers/path_helper.hpp>
class test_style_reader : public CxxTest::TestSuite

View File

@ -3,7 +3,6 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/theme_serializer.hpp>
#include <helpers/path_helper.hpp>
#include <helpers/xml_helper.hpp>
#include <xlnt/workbook/workbook.hpp>
@ -14,10 +13,10 @@ public:
void test_write_theme()
{
xlnt::workbook wb;
xlnt::theme_serializer serializer;
pugi::xml_document xml;
serializer.write_theme(wb.get_theme(), xml);
wb.set_theme(xlnt::theme());
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/theme1.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/theme1.xml"),
wb, xlnt::constants::part_theme()));
}
};

View File

@ -68,27 +68,27 @@ public:
TS_ASSERT_THROWS(wb.remove_sheet(wb2.get_active_sheet()), std::runtime_error);
}
void test_get_sheet_by_name()
void test_get_sheet_by_title()
{
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
std::string title = "my sheet";
new_sheet.set_title(title);
auto found_sheet = wb.get_sheet_by_name(title);
auto found_sheet = wb.get_sheet_by_title(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet);
TS_ASSERT_THROWS(wb.get_sheet_by_name("error"), xlnt::key_not_found);
TS_ASSERT_THROWS(wb.get_sheet_by_title("error"), xlnt::key_not_found);
const auto &wb_const = wb;
TS_ASSERT_THROWS(wb_const.get_sheet_by_name("error"), xlnt::key_not_found);
TS_ASSERT_THROWS(wb_const.get_sheet_by_title("error"), xlnt::key_not_found);
}
void test_get_sheet_by_name_const()
void test_get_sheet_by_title_const()
{
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
std::string title = "my sheet";
new_sheet.set_title(title);
const xlnt::workbook& wbconst = wb;
auto found_sheet = wbconst.get_sheet_by_name(title);
auto found_sheet = wbconst.get_sheet_by_title(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet);
}
@ -124,10 +124,10 @@ public:
wb.create_sheet().set_title("1");
wb.create_sheet().set_title("2");
auto sheet_index = wb.get_index(wb.get_sheet_by_name("1"));
auto sheet_index = wb.get_index(wb.get_sheet_by_title("1"));
TS_ASSERT_EQUALS(sheet_index, 1);
sheet_index = wb.get_index(wb.get_sheet_by_name("2"));
sheet_index = wb.get_index(wb.get_sheet_by_title("2"));
TS_ASSERT_EQUALS(sheet_index, 2);
}
@ -259,25 +259,11 @@ public:
void test_manifest()
{
xlnt::default_type d;
TS_ASSERT(d.get_content_type().empty());
TS_ASSERT(d.get_extension().empty());
xlnt::override_type o;
TS_ASSERT(o.get_content_type().empty());
TS_ASSERT(o.get_part().to_string().empty());
xlnt::manifest m;
xlnt::manifest m;
TS_ASSERT(!m.has_default_type("xml"));
TS_ASSERT_THROWS(m.get_default_type("xml"), std::out_of_range);
TS_ASSERT(!m.has_override_type(xlnt::path("xl/workbook.xml")));
TS_ASSERT_THROWS(m.get_override_type(xlnt::path("xl/workbook.xml")), std::out_of_range);
}
void test_get_bad_relationship()
{
xlnt::workbook wb;
TS_ASSERT_THROWS(wb.get_relationship("bad"), std::runtime_error);
TS_ASSERT_THROWS(m.get_default_type("xml"), xlnt::key_not_found);
TS_ASSERT(!m.has_part(xlnt::path("xl/workbook.xml")));
TS_ASSERT_THROWS(m.get_part_relationships(xlnt::path("xl/workbook.xml")), xlnt::key_not_found);
}
void test_memory()

View File

@ -3,10 +3,6 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/relationship_serializer.hpp>
#include <detail/shared_strings_serializer.hpp>
#include <detail/workbook_serializer.hpp>
#include <detail/worksheet_serializer.hpp>
#include <helpers/temporary_file.hpp>
#include <helpers/path_helper.hpp>
#include <helpers/xml_helper.hpp>
@ -37,32 +33,29 @@ public:
{
xlnt::workbook old_wb;
std::vector<unsigned char> saved_wb;
TS_ASSERT(old_wb.save(saved_wb));
old_wb.save(saved_wb);
xlnt::workbook new_wb;
TS_ASSERT(new_wb.load(saved_wb));
new_wb.load(saved_wb);
TS_ASSERT_EQUALS(old_wb, new_wb);
}
void test_write_workbook_rels()
{
xlnt::workbook wb;
wb.add_shared_string(xlnt::text());
xlnt::zip_file archive;
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml");
pugi::xml_document xml;
xml.load(archive.read(xlnt::path("xl/_rels/workbook.xml.rels")).c_str());
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/workbook.xml.rels"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/workbook.xml.rels"),
wb, xlnt::path("xl/_rels/workbook.xml.rels")));
}
void test_write_workbook()
{
xlnt::workbook wb;
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_workbook(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/workbook.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/workbook.xml"),
wb, xlnt::path("xl/workbook.xml")));
}
void test_write_string_table()
@ -73,111 +66,103 @@ public:
ws.get_cell("A1").set_value("hello");
ws.get_cell("A2").set_value("world");
ws.get_cell("A3").set_value("nice");
pugi::xml_document xml;
xlnt::shared_strings_serializer::write_shared_strings(wb.get_shared_strings(), xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sharedStrings.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sharedStrings.xml"),
wb, xlnt::path("xl/sharedStrings.xml")));
}
void test_write_worksheet()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_hidden_worksheet()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_page_setup().set_sheet_state(xlnt::sheet_state::hidden);
ws.get_cell("F42").set_value("hello");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_bool()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value(false);
ws.get_cell("F43").set_value(true);
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_bool.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_bool.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_formula()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F1").set_value(10);
ws.get_cell("F2").set_value(32);
ws.get_cell("F3").set_formula("F1+F2");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_formula.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_bool.xml"),
wb, xlnt::path("writer/expected/sheet1_formula.xml")));
}
void test_write_height()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F1").set_value(10);
ws.get_row_properties(ws.get_cell("F1").get_row()).height = 30;
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_height.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_height.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_hyperlink()
{
xlnt::workbook clean_wb;
auto ws = clean_wb.get_active_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_value("test");
ws.get_cell("A1").set_hyperlink("http://test.com");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_hyperlink.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_hyperlink.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_hyperlink_rels()
{
auto ws = wb_.create_sheet();
TS_ASSERT_EQUALS(0, ws.get_relationships().size());
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_value("test");
ws.get_cell("A1").set_hyperlink("http://test.com/");
TS_ASSERT_EQUALS(1, ws.get_relationships().size());
ws.get_cell("A2").set_value("test");
ws.get_cell("A2").set_hyperlink("http://test2.com/");
TS_ASSERT_EQUALS(2, ws.get_relationships().size());
xlnt::zip_file archive;
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(ws.get_relationships(), "xl/worksheets/sheet1.xml");
pugi::xml_document xml;
xml.load(archive.read(xlnt::path("xl/worksheets/_rels/sheet1.xml.rels")).c_str());
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_hyperlink.xml.rels"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_hyperlink.xml.rels"),
wb, xlnt::path("xl/worksheets/_rels/sheet1.xml.rels")));
}
void _test_write_hyperlink_image_rels()
@ -187,7 +172,9 @@ public:
void test_hyperlink_value()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_hyperlink("http://test.com");
TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1").get_value<std::string>());
ws.get_cell("A1").set_value("test");
@ -197,21 +184,18 @@ public:
void test_write_auto_filter()
{
xlnt::workbook wb;
auto ws = wb.get_sheet_by_index(0);
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
ws.auto_filter("A1:F1");
xlnt::worksheet_serializer ws_serializer(ws);
pugi::xml_document xml;
ws_serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_auto_filter.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_auto_filter.xml"), xml));
xlnt::workbook_serializer wb_serializer(wb);
pugi::xml_document xml2;
wb_serializer.write_workbook(xml2);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/workbook_auto_filter.xml"), xml2));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/workbook_auto_filter.xml"),
wb, xlnt::path("xl/workbook.xml")));
}
void test_write_auto_filter_filter_column()
@ -226,65 +210,62 @@ public:
void test_freeze_panes_horiz()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
ws.freeze_panes("A4");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_horiz.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_horiz.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_freeze_panes_vert()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
ws.freeze_panes("D1");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_vert.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_vert.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_freeze_panes_both()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
ws.freeze_panes("D4");
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_both.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/sheet1_freeze_panes_both.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_long_number()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_value(9781231231230LL);
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/long_number.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/long_number.xml"),
wb, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_short_number()
{
auto ws = wb_.create_sheet();
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_value(1234567890);
xlnt::worksheet_serializer serializer(ws);
pugi::xml_document xml;
serializer.write_worksheet(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/short_number.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/short_number.xml"),
wb_, xlnt::path("xl/worksheets/sheet1.xml")));
}
void test_write_page_setup()

View File

@ -3,11 +3,9 @@
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/xlsx_writer.hpp>
#include <detail/shared_strings_serializer.hpp>
#include <detail/workbook_serializer.hpp>
#include <helpers/path_helper.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp>
class test_write_workbook : public CxxTest::TestSuite
{
@ -16,27 +14,23 @@ public:
{
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.get_cell("F42").set_value("hello");
ws.auto_filter("A1:F1");
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_workbook(xml);
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/workbook_auto_filter.xml"), xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/workbook_auto_filter.xml"),
wb, xlnt::path("xl/workbook.xml")));
}
void test_write_hidden_worksheet()
{
xlnt::workbook wb;
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.set_sheet_state(xlnt::sheet_state::hidden);
wb.create_sheet();
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_workbook(xml);
std::string expected_string =
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <workbookPr/>"
@ -51,11 +45,8 @@ public:
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
"</workbook>";
pugi::xml_document expected;
expected.load(expected_string.c_str());
TS_ASSERT(xml_helper::compare_xml_nodes(expected.child("workbook").child("sheets"),
xml.child("workbook").child("sheets")));
TS_ASSERT(xml_helper::string_matches_workbook_part(expected_string,
wb, xlnt::path("xl/workbook.xml")));
}
void test_write_hidden_single_worksheet()
@ -63,11 +54,8 @@ public:
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.set_sheet_state(xlnt::sheet_state::hidden);
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
TS_ASSERT_THROWS(serializer.write_workbook(xml), xlnt::no_visible_worksheets);
std::vector<std::uint8_t> trash;
TS_ASSERT_THROWS(wb.save(trash), xlnt::no_visible_worksheets);
}
void test_write_empty_workbook()
@ -76,7 +64,6 @@ public:
temporary_file file;
TS_ASSERT(!file.get_path().exists())
xlnt::excel_serializer serializer(wb);
wb.save(file.get_path());
TS_ASSERT(file.get_path().exists());
}
@ -84,13 +71,12 @@ public:
void test_write_virtual_workbook()
{
xlnt::workbook old_wb, new_wb;
xlnt::excel_serializer serializer(old_wb);
std::vector<std::uint8_t> wb_bytes;
serializer.save_virtual_workbook(wb_bytes);
xlnt::excel_serializer deserializer(new_wb);
deserializer.load_virtual_workbook(wb_bytes);
old_wb.save(wb_bytes);
new_wb.load(wb_bytes);
// TODO more tests!
}
void test_write_workbook_rels()
@ -99,28 +85,22 @@ public:
auto ws = wb.get_active_sheet();
ws.get_cell("A1").set_value("string");
xlnt::zip_file archive;
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml");
pugi::xml_document observed;
observed.load(archive.read(xlnt::path("xl/_rels/workbook.xml.rels")).c_str());
TS_ASSERT(xml_helper::file_matches_document(path_helper::get_data_directory("writer/expected/workbook.xml.rels"), observed));
TS_ASSERT(xml_helper::file_matches_workbook_part(
path_helper::get_data_directory("writer/expected/workbook.xml.rels"),
wb, xlnt::path("xl/_rels/workbook.xml.rels")));
}
void test_write_workbook_part()
{
xlnt::workbook wb;
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_workbook(xml);
auto filename = path_helper::get_data_directory("writer/expected/workbook.xml");
TS_ASSERT(xml_helper::file_matches_document(filename, xml));
TS_ASSERT(xml_helper::file_matches_workbook_part(filename, wb, xlnt::path("xl/_rels/workbook.xml.rels")));
}
void test_write_named_range()
void _test_write_named_range()
{
/*
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
wb.create_named_range("test_range", ws, "$A$1:$B$5");
@ -134,6 +114,7 @@ public:
"</root>";
TS_ASSERT(xml_helper::string_matches_document(expected, xml));
*/
}
void test_read_workbook_code_name()
@ -146,10 +127,6 @@ public:
{
xlnt::workbook wb;
wb.set_code_name("MyWB");
xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml;
serializer.write_workbook(xml);
std::string expected =
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
@ -163,21 +140,13 @@ public:
" <definedNames/>"
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
"</workbook>";
pugi::xml_document expected_xml;
expected_xml.load(expected.c_str());
TS_ASSERT(xml_helper::compare_xml_nodes(expected_xml.child("workbook").child("workbookPr"),
xml.child("workbook").child("workbookPr")));
TS_ASSERT(xml_helper::string_matches_workbook_part(expected, wb, xlnt::path("xl/workbook.xml")));
}
void test_write_root_rels()
{
xlnt::workbook wb;
xlnt::zip_file archive;
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(wb.get_root_relationships(), "");
pugi::xml_document observed;
observed.load(archive.read(xlnt::path("_rels/.rels")).c_str());
std::string expected =
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
@ -186,7 +155,7 @@ public:
" <Relationship Id=\"rId3\" Target=\"docProps/app.xml\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\"/>"
"</Relationships>";
TS_ASSERT(xml_helper::string_matches_document(expected, observed));
TS_ASSERT(xml_helper::string_matches_workbook_part(expected, wb, xlnt::path("_rels/.rels")));
}
void test_write_shared_strings_with_runs()
@ -205,9 +174,6 @@ public:
text.add_run(run);
cell.set_value(text);
pugi::xml_document xml;
xlnt::shared_strings_serializer::write_shared_strings(wb.get_shared_strings(), xml);
std::string expected =
"<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"1\" uniqueCount=\"1\">"
" <si>"
@ -224,7 +190,8 @@ public:
" </si>"
"</sst>";
TS_ASSERT(xml_helper::string_matches_document(expected, xml));
TS_ASSERT(xml_helper::string_matches_workbook_part(expected, wb,
xlnt::constants::part_shared_strings()));
}
void test_write_worksheet_order()

View File

@ -30,7 +30,8 @@
#include <detail/cell_impl.hpp>
#include <detail/constants.hpp>
#include <detail/excel_serializer.hpp>
#include <detail/xlsx_consumer.hpp>
#include <detail/xlsx_producer.hpp>
#include <detail/include_windows.hpp>
#include <detail/workbook_impl.hpp>
#include <detail/worksheet_impl.hpp>
@ -62,9 +63,13 @@ workbook workbook::minimal()
auto impl = new detail::workbook_impl();
workbook wb(impl);
wb.create_sheet();
wb.d_->worksheets_.push_back(detail::worksheet_impl(&wb, 1, "Sheet"));
wb.d_->root_relationships_.push_back(relationship(relationship::type::office_document, "rId1", constants::part_workbook().to_string()));
wb.d_->manifest_.register_package_part(path("workbook.xml"),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
relationship::type::workbook);
wb.d_->manifest_.register_part(path("sheet1.xml"), path("workbook.xml"),
"worksheet", relationship::type::worksheet);
return wb;
}
@ -74,9 +79,15 @@ workbook workbook::empty_excel()
auto impl = new detail::workbook_impl();
xlnt::workbook wb(impl);
wb.d_->root_relationships_.push_back(relationship(relationship::type::core_properties, "rId1", constants::part_core().to_string()));
wb.d_->root_relationships_.push_back(relationship(relationship::type::extended_properties, "rId2", constants::part_app().to_string()));
wb.d_->root_relationships_.push_back(relationship(relationship::type::office_document, "rId3", constants::part_workbook().to_string()));
wb.d_->manifest_.register_package_part(path("docProps/core.xml"),
"application/vnd.openxmlformats-package.coreproperties+xml",
relationship::type::core_properties);
wb.d_->manifest_.register_package_part(path("docProps/app.xml"),
"application/vnd.openxmlformats-officedocument.extended-properties+xml",
relationship::type::extended_properties);
wb.d_->manifest_.register_package_part(path("xl/workbook.xml"),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
relationship::type::workbook);
wb.set_application("Microsoft Excel");
wb.create_sheet();
@ -84,13 +95,6 @@ workbook workbook::empty_excel()
wb.create_style("Normal");
wb.set_theme(theme());
auto &manifest = wb.d_->manifest_;
manifest.add_override_type(constants::part_workbook(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
manifest.add_override_type(constants::part_theme(), "application/vnd.openxmlformats-officedocument.theme+xml");
manifest.add_override_type(constants::part_styles(), "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
manifest.add_override_type(constants::part_core(), "application/vnd.openxmlformats-package.core-properties+xml");
manifest.add_override_type(constants::part_app(), "application/vnd.openxmlformats-officedocument.extended-properties+xml");
wb.d_->stylesheet_.format_styles.front() = "Normal";
xlnt::fill gray125 = xlnt::fill::pattern(xlnt::pattern_fill::type::gray125);
@ -125,11 +129,11 @@ workbook::workbook(detail::workbook_impl *impl) : d_(impl)
{
}
const worksheet workbook::get_sheet_by_name(const std::string &name) const
const worksheet workbook::get_sheet_by_title(const std::string &title) const
{
for (auto &impl : d_->worksheets_)
{
if (impl.title_ == name)
if (impl.title_ == title)
{
return worksheet(&impl);
}
@ -138,11 +142,11 @@ const worksheet workbook::get_sheet_by_name(const std::string &name) const
throw key_not_found();
}
worksheet workbook::get_sheet_by_name(const std::string &name)
worksheet workbook::get_sheet_by_title(const std::string &title)
{
for (auto &impl : d_->worksheets_)
{
if (impl.title_ == name)
if (impl.title_ == title)
{
return worksheet(&impl);
}
@ -173,6 +177,32 @@ const worksheet workbook::get_sheet_by_index(std::size_t index) const
return worksheet(&*iter);
}
worksheet workbook::get_sheet_by_id(std::size_t id)
{
for (auto &impl : d_->worksheets_)
{
if (impl.id_ == id)
{
return worksheet(&impl);
}
}
throw key_not_found();
}
const worksheet workbook::get_sheet_by_id(std::size_t id) const
{
for (auto &impl : d_->worksheets_)
{
if (impl.id_ == id)
{
return worksheet(&impl);
}
}
throw key_not_found();
}
worksheet workbook::get_active_sheet()
{
return get_sheet_by_index(d_->active_sheet_index_);
@ -205,11 +235,11 @@ worksheet workbook::create_sheet()
path sheet_path = constants::package_worksheets().append(sheet_filename);
d_->worksheets_.push_back(detail::worksheet_impl(this, sheet_id, title));
create_relationship("rId" + std::to_string(sheet_id),
"worksheets/" + sheet_filename,
relationship::type::worksheet);
d_->manifest_.add_override_type(sheet_path,
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
auto workbook_rel = d_->manifest_.get_package_relationship(relationship::type::workbook);
d_->manifest_.register_part(sheet_path, workbook_rel.get_target_uri(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
relationship::type::worksheet);
return worksheet(&d_->worksheets_.back());
}
@ -260,7 +290,7 @@ void workbook::create_named_range(const std::string &name, worksheet range_owner
void workbook::create_named_range(const std::string &name, worksheet range_owner, const range_reference &reference)
{
get_sheet_by_name(range_owner.get_title()).create_named_range(name, reference);
get_sheet_by_title(range_owner.get_title()).create_named_range(name, reference);
}
void workbook::remove_named_range(const std::string &name)
@ -290,33 +320,50 @@ range workbook::get_named_range(const std::string &name)
throw std::runtime_error("named range not found");
}
bool workbook::load(std::istream &stream)
void workbook::load(std::istream &stream)
{
excel_serializer serializer_(*this);
serializer_.load_stream_workbook(stream);
return true;
detail::xlsx_consumer consumer(*this);
consumer.read(stream);
}
bool workbook::load(const std::vector<unsigned char> &data)
void workbook::load(const std::vector<unsigned char> &data)
{
excel_serializer serializer_(*this);
serializer_.load_virtual_workbook(data);
return true;
detail::xlsx_consumer consumer(*this);
consumer.read(data);
}
bool workbook::load(const std::string &filename)
void workbook::load(const std::string &filename)
{
return load(path(filename));
}
bool workbook::load(const path &filename)
void workbook::load(const path &filename)
{
excel_serializer serializer_(*this);
serializer_.load_workbook(filename);
detail::xlsx_consumer consumer(*this);
consumer.read(filename);
}
return true;
void workbook::save(std::vector<unsigned char> &data)
{
detail::xlsx_producer producer(*this);
producer.write(data);
}
void workbook::save(const std::string &filename)
{
return save(path(filename));
}
void workbook::save(const path &filename)
{
detail::xlsx_producer producer(*this);
producer.write(filename);
}
void workbook::save(std::ostream &stream)
{
detail::xlsx_producer producer(*this);
producer.write(stream);
}
void workbook::set_guess_types(bool guess)
@ -329,44 +376,18 @@ bool workbook::get_guess_types() const
return d_->guess_types_;
}
void workbook::create_relationship(const std::string &id, const std::string &target, relationship::type type)
{
d_->relationships_.push_back(relationship(type, id, target));
}
void workbook::create_root_relationship(const std::string &id, const std::string &target, relationship::type type)
{
d_->root_relationships_.push_back(relationship(type, id, target));
}
relationship workbook::get_relationship(const std::string &id) const
{
for (auto &rel : d_->relationships_)
{
if (rel.get_id() == id)
{
return rel;
}
}
throw std::runtime_error("");
}
void workbook::remove_sheet(worksheet ws)
{
auto match_iter = std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(),
[=](detail::worksheet_impl &comp) { return worksheet(&comp) == ws; });
[=](detail::worksheet_impl &comp) { return worksheet(&comp) == ws; });
if (match_iter == d_->worksheets_.end())
{
throw std::runtime_error("worksheet not owned by this workbook");
}
auto sheet_filename = "worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml";
auto rel_iter = std::find_if(d_->relationships_.begin(), d_->relationships_.end(),
[=](relationship &r) { return r.get_target_uri() == sheet_filename; });
auto sheet_filename = path("worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml");
d_->relationships_.erase(rel_iter);
d_->worksheets_.erase(match_iter);
}
@ -441,7 +462,7 @@ std::vector<std::string> workbook::get_sheet_titles() const
worksheet workbook::operator[](const std::string &name)
{
return get_sheet_by_name(name);
return get_sheet_by_title(name);
}
worksheet workbook::operator[](std::size_t index)
@ -452,34 +473,13 @@ worksheet workbook::operator[](std::size_t index)
void workbook::clear()
{
d_->worksheets_.clear();
d_->relationships_.clear();
d_->manifest_.clear();
d_->active_sheet_index_ = 0;
d_->manifest_.clear();
clear_styles();
clear_formats();
}
bool workbook::save(std::vector<unsigned char> &data)
{
excel_serializer serializer(*this);
serializer.save_virtual_workbook(data);
return true;
}
bool workbook::save(const std::string &filename)
{
return save(path(filename));
}
bool workbook::save(const path &filename)
{
excel_serializer serializer(*this);
serializer.save_workbook(filename);
return true;
}
bool workbook::operator==(const workbook &rhs) const
{
return d_.get() == rhs.d_.get();
@ -490,11 +490,6 @@ bool workbook::operator!=(const workbook &rhs) const
return d_.get() != rhs.d_.get();
}
const std::vector<relationship> &xlnt::workbook::get_relationships() const
{
return d_->relationships_;
}
void swap(workbook &left, workbook &right)
{
using std::swap;
@ -568,24 +563,11 @@ const theme &workbook::get_theme() const
void workbook::set_theme(const theme &value)
{
if (!d_->has_theme_)
if (!d_->manifest_.has_part(constants::part_theme()))
{
bool has_theme_relationship = false;
for (const auto &rel : get_relationships())
{
if (rel.get_type() == relationship::type::theme)
{
has_theme_relationship = true;
break;
}
}
if (!has_theme_relationship)
{
create_relationship(next_relationship_id(), "theme/theme1.xml", relationship::type::theme);
d_->manifest_.add_override_type(constants::part_theme(), "application/vnd.openxmlformats-officedocument.spreadsheetml.theme+xml");
}
auto workbook_rel = d_->manifest_.get_package_relationship(relationship::type::workbook);
d_->manifest_.register_part(constants::part_theme(), workbook_rel.get_target_uri(),
"theme", relationship::type::theme);
}
d_->has_theme_ = true;
@ -609,24 +591,12 @@ std::vector<named_range> workbook::get_named_ranges() const
std::size_t workbook::add_format(const format &to_add)
{
if (d_->stylesheet_.formats.empty())
if (!d_->manifest_.has_part(constants::part_styles()))
{
bool has_style_relationship = false;
for (const auto &rel : get_relationships())
{
if (rel.get_type() == relationship::type::styles)
{
has_style_relationship = true;
break;
}
}
if (!has_style_relationship)
{
create_relationship(next_relationship_id(), "styles.xml", relationship::type::styles);
d_->manifest_.add_override_type(constants::part_styles(), "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
}
auto workbook_rel = d_->manifest_.get_package_relationship(relationship::type::workbook);
d_->manifest_.register_part(constants::part_styles(), workbook_rel.get_target_uri(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
relationship::type::styles);
}
return d_->stylesheet_.add_format(to_add);
@ -634,24 +604,12 @@ std::size_t workbook::add_format(const format &to_add)
std::size_t workbook::add_style(const style &to_add)
{
if (d_->stylesheet_.formats.empty())
if (!d_->manifest_.has_part(constants::part_styles()))
{
bool has_style_relationship = false;
for (const auto &rel : get_relationships())
{
if (rel.get_type() == relationship::type::styles)
{
has_style_relationship = true;
break;
}
}
if (!has_style_relationship)
{
create_relationship(next_relationship_id(), "styles.xml", relationship::type::styles);
d_->manifest_.add_override_type(constants::part_styles(), "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
}
auto workbook_rel = d_->manifest_.get_package_relationship(relationship::type::workbook);
d_->manifest_.register_part(constants::part_styles(), workbook_rel.get_target_uri(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
relationship::type::styles);
}
return d_->stylesheet_.add_style(to_add);
@ -716,11 +674,6 @@ const manifest &workbook::get_manifest() const
return d_->manifest_;
}
const std::vector<relationship> &workbook::get_root_relationships() const
{
return d_->root_relationships_;
}
std::vector<text> &workbook::get_shared_strings()
{
return d_->shared_strings_;
@ -733,25 +686,13 @@ const std::vector<text> &workbook::get_shared_strings() const
void workbook::add_shared_string(const text &shared, bool allow_duplicates)
{
if (d_->shared_strings_.empty())
{
bool has_shared_strings = false;
for (const auto &rel : get_relationships())
{
if (rel.get_type() == relationship::type::shared_strings)
{
has_shared_strings = true;
break;
}
}
if (!has_shared_strings)
{
create_relationship(next_relationship_id(), "sharedStrings.xml", relationship::type::shared_strings);
d_->manifest_.add_override_type(constants::part_shared_strings(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml");
}
}
if (!d_->manifest_.has_part(constants::part_shared_strings()))
{
auto workbook_rel = d_->manifest_.get_package_relationship(relationship::type::workbook);
d_->manifest_.register_part(constants::part_shared_strings(), workbook_rel.get_target_uri(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
relationship::type::shared_string_table);
}
if (!allow_duplicates)
{
@ -817,19 +758,6 @@ const style &workbook::get_style_by_id(std::size_t style_id) const
return d_->stylesheet_.styles.at(style_id);
}
std::string workbook::next_relationship_id() const
{
std::size_t i = 1;
while (std::find_if(d_->relationships_.begin(), d_->relationships_.end(),
[i](const relationship &r) { return r.get_id() == "rId" + std::to_string(i); }) != d_->relationships_.end())
{
i++;
}
return "rId" + std::to_string(i);
}
std::string workbook::get_application() const
{
return d_->application_;
@ -971,5 +899,14 @@ void workbook::set_title(const std::string &title)
d_->title_ = title;
}
detail::workbook_impl &workbook::impl()
{
return *d_;
}
const detail::workbook_impl &workbook::impl() const
{
return *d_;
}
} // namespace xlnt

View File

@ -228,7 +228,7 @@ public:
TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1").get_value<std::string>());
ws.get_cell("A1").set_value("test");
TS_ASSERT_EQUALS("test", ws.get_cell("A1").get_value<std::string>());
TS_ASSERT_EQUALS(ws.get_cell("A1").get_hyperlink().get_target_uri(), "http://test.com");
TS_ASSERT_EQUALS(ws.get_cell("A1").get_hyperlink(), "http://test.com");
}
void test_append()

View File

@ -472,18 +472,6 @@ const range worksheet::get_range(const range_reference &reference) const
return range(*this, reference);
}
const std::vector<relationship> &worksheet::get_relationships() const
{
return d_->relationships_;
}
relationship worksheet::create_relationship(relationship::type type, const std::string &target_uri)
{
std::string r_id = "rId" + std::to_string(d_->relationships_.size() + 1);
d_->relationships_.push_back(relationship(type, r_id, target_uri));
return d_->relationships_.back();
}
void worksheet::merge_cells(const std::string &reference_string)
{
merge_cells(range_reference(reference_string));
@ -1028,7 +1016,7 @@ sheet_view worksheet::get_sheet_view() const
std::size_t worksheet::next_custom_number_format_id()
{
return get_workbook().d_->stylesheet_.next_custom_format_id++;
return get_workbook().impl().stylesheet_.next_custom_format_id;
}
} // namespace xlnt

View File

@ -55,6 +55,55 @@ public:
return true;
}
static bool string_matches_workbook_part(const std::string &expected,
xlnt::workbook &wb, const xlnt::path &part)
{
std::vector<std::uint8_t> bytes;
wb.save(bytes);
xlnt::zip_file archive;
archive.load(bytes);
return string_matches_archive_member(expected, archive, part);
}
static bool file_matches_workbook_part(const xlnt::path &expected,
xlnt::workbook &wb, const xlnt::path &part)
{
std::vector<std::uint8_t> bytes;
wb.save(bytes);
xlnt::zip_file archive;
archive.load(bytes);
return file_matches_archive_member(expected, archive, part);
}
static bool string_matches_archive_member(const std::string &expected,
xlnt::zip_file &archive,
const xlnt::path &member)
{
pugi::xml_document expected_document;
expected_document.load(expected.c_str());
pugi::xml_document observed_document;
expected_document.load(archive.read(member).c_str());
return documents_match(expected_document, observed_document);
}
static bool file_matches_archive_member(const xlnt::path &file,
xlnt::zip_file &archive,
const xlnt::path &member)
{
pugi::xml_document expected_document;
expected_document.load(file.read_contents().c_str());
pugi::xml_document observed_document;
if (!archive.has_file(member)) return false;
expected_document.load(archive.read(member).c_str());
return documents_match(expected_document, observed_document);
}
static bool file_matches_document(const xlnt::path &expected,
const pugi::xml_document &observed)
{