From 5a9c18834d27df1fc8f09402dcf4736e812ba577 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Thu, 10 Mar 2016 17:12:51 +0800 Subject: [PATCH] work on round-tripping of a workbook with complex formatting --- .../xlnt/serialization/style_serializer.hpp | 11 + include/xlnt/styles/alignment.hpp | 6 +- include/xlnt/styles/format.hpp | 107 ++++++ include/xlnt/styles/style.hpp | 76 +--- include/xlnt/workbook/workbook.hpp | 12 + source/cell/cell.cpp | 4 +- source/detail/workbook_impl.hpp | 4 + source/serialization/excel_serializer.cpp | 12 + source/serialization/style_serializer.cpp | 362 +++++++++++++----- source/serialization/workbook_serializer.cpp | 3 + source/styles/alignment.cpp | 10 + source/styles/format.cpp | 237 ++++++++++++ source/styles/style.cpp | 187 ++------- source/workbook/workbook.cpp | 173 +++++---- source/worksheet/tests/test_worksheet.hpp | 7 + 15 files changed, 830 insertions(+), 381 deletions(-) create mode 100644 include/xlnt/styles/format.hpp create mode 100644 source/styles/format.cpp diff --git a/include/xlnt/serialization/style_serializer.hpp b/include/xlnt/serialization/style_serializer.hpp index 7f5ff8dd..446b754b 100644 --- a/include/xlnt/serialization/style_serializer.hpp +++ b/include/xlnt/serialization/style_serializer.hpp @@ -38,6 +38,7 @@ class color; class conditional_format; class fill; class font; +class format; class named_style; class number_format; class protection; @@ -219,6 +220,16 @@ public: /// bool read_number_formats(const xml_node &number_formats_node); + /// + /// Read a single format from the given node. In styles.xml, this is an "xf" element. + /// A format has an optional border id, fill id, font id, and number format id where + /// each id is an index in the corresponding list of borders, etc. A style also has + /// optional alignment and protection children. A style also defines whether each of + /// these is "applied". For example, a style with a defined font id, font=# but with + /// "applyFont=0" will not use the font in formatting. + /// + format read_format(const xml_node &format_node); + /// /// Read a single style from the given node. In styles.xml, this is an "xf" element. /// A style has an optional border id, fill id, font id, and number format id where diff --git a/include/xlnt/styles/alignment.hpp b/include/xlnt/styles/alignment.hpp index 3bb4b372..03289da3 100644 --- a/include/xlnt/styles/alignment.hpp +++ b/include/xlnt/styles/alignment.hpp @@ -32,7 +32,7 @@ namespace xlnt { /// -/// Alignment options for use in styles. +/// Alignment options for use in cell formats. /// class XLNT_CLASS alignment : public hashable { @@ -52,6 +52,10 @@ public: vertical_alignment get_vertical() const; void set_vertical(vertical_alignment vertical); + + void set_shrink_to_fit(bool shrink_to_fit); + + bool get_shrink_to_fit() const; protected: std::string to_hash_string() const override; diff --git a/include/xlnt/styles/format.hpp b/include/xlnt/styles/format.hpp new file mode 100644 index 00000000..05f5aff1 --- /dev/null +++ b/include/xlnt/styles/format.hpp @@ -0,0 +1,107 @@ +// 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 +#include +#include +#include +#include +#include +#include + +namespace xlnt { + +class workbook; + +/// +/// Describes the entirety of the formatting of a particular cell. +/// +class XLNT_CLASS format : public hashable +{ +public: + format(); + format(const format &other); + format &operator=(const format &other); + + std::size_t hash() const; + + const alignment get_alignment() const; + const border get_border() const; + const fill get_fill() const; + const font get_font() const; + const number_format get_number_format() const; + const protection get_protection() const; + bool pivot_button() const; + bool quote_prefix() const; + + std::size_t get_id() const; + std::size_t get_fill_id() const; + std::size_t get_font_id() const; + std::size_t get_border_id() const; + std::size_t get_number_format_id() const; + + void apply_alignment(bool apply); + void apply_border(bool apply); + void apply_fill(bool apply); + void apply_font(bool apply); + void apply_number_format(bool apply); + void apply_protection(bool apply); + +protected: + std::string to_hash_string() const override; + +private: + friend class style_serializer; + friend class workbook; + + std::size_t id_; + + bool alignment_apply_; + alignment alignment_; + + bool border_apply_; + std::size_t border_id_; + border border_; + + bool fill_apply_; + std::size_t fill_id_; + fill fill_; + + bool font_apply_; + std::size_t font_id_; + font font_; + + bool number_format_apply_; + std::size_t number_format_id_; + number_format number_format_; + + bool protection_apply_; + protection protection_; + + bool pivot_button_; + bool quote_prefix_; +}; + +} // namespace xlnt diff --git a/include/xlnt/styles/style.hpp b/include/xlnt/styles/style.hpp index 004f6f28..71fdc235 100644 --- a/include/xlnt/styles/style.hpp +++ b/include/xlnt/styles/style.hpp @@ -24,12 +24,7 @@ #pragma once #include -#include -#include -#include -#include -#include -#include +#include namespace xlnt { @@ -46,62 +41,29 @@ public: style &operator=(const style &other); std::size_t hash() const; - - const alignment get_alignment() const; - const border get_border() const; - const fill get_fill() const; - const font get_font() const; - const number_format get_number_format() const; - const protection get_protection() const; - bool pivot_button() const; - bool quote_prefix() const; - - std::size_t get_id() const; - std::size_t get_fill_id() const; - std::size_t get_font_id() const; - std::size_t get_border_id() const; - std::size_t get_number_format_id() const; - - void apply_alignment(bool apply); - void apply_border(bool apply); - void apply_fill(bool apply); - void apply_font(bool apply); - void apply_number_format(bool apply); - void apply_protection(bool apply); - + + std::string get_name() const; + void set_name(const std::string &name); + + std::size_t get_format_id() const; + void set_format_id(std::size_t format_id); + + std::size_t get_builtin_id() const; + void set_builtin_id(std::size_t builtin_id); + + void set_hidden(bool hidden); + bool get_hidden() const; + protected: std::string to_hash_string() const override; - + private: - friend class style_serializer; friend class workbook; - std::size_t id_; - - bool alignment_apply_; - alignment alignment_; - - bool border_apply_; - std::size_t border_id_; - border border_; - - bool fill_apply_; - std::size_t fill_id_; - fill fill_; - - bool font_apply_; - std::size_t font_id_; - font font_; - - bool number_format_apply_; - std::size_t number_format_id_; - number_format number_format_; - - bool protection_apply_; - protection protection_; - - bool pivot_button_; - bool quote_prefix_; + std::string name_; + std::size_t format_id_; + std::size_t builtin_id_; + bool hidden_; }; } // namespace xlnt diff --git a/include/xlnt/workbook/workbook.hpp b/include/xlnt/workbook/workbook.hpp index ad25bb55..feed3a54 100644 --- a/include/xlnt/workbook/workbook.hpp +++ b/include/xlnt/workbook/workbook.hpp @@ -43,6 +43,7 @@ class document_properties; class drawing; class fill; class font; +class format; class manifest; class named_range; class number_format; @@ -182,6 +183,8 @@ public: void add_number_format(const number_format &format); void add_protection(const protection &p); + const std::vector &get_cell_style_formats() const; + const std::vector &get_cell_formats() const; const std::vector