From 5946eec32ef5b19131c4daa9d85a63512e86e167 Mon Sep 17 00:00:00 2001 From: "Wu, Ganhao" Date: Sun, 29 Aug 2021 02:56:57 +0800 Subject: [PATCH 1/4] Fix - Print Settings (Page Size, margins, etc) not saved when loading a file and save it. --- include/xlnt/worksheet/page_setup.hpp | 34 ++++++++++++- source/detail/serialization/xlsx_consumer.cpp | 20 ++++++-- source/detail/serialization/xlsx_producer.cpp | 51 ++++++++++++------- source/worksheet/page_setup.cpp | 33 ++++++++++-- 4 files changed, 111 insertions(+), 27 deletions(-) diff --git a/include/xlnt/worksheet/page_setup.hpp b/include/xlnt/worksheet/page_setup.hpp index 6fc1761e..7b57297f 100644 --- a/include/xlnt/worksheet/page_setup.hpp +++ b/include/xlnt/worksheet/page_setup.hpp @@ -119,6 +119,11 @@ public: /// void paper_size(xlnt::paper_size paper_size); + /// + /// Check if current paper setting has paper size setting + /// + bool has_paper_size() const; + /// /// Returns true if this worksheet should be scaled to fit on a single page during printing. /// @@ -159,6 +164,26 @@ public: /// double scale() const; + /// + /// Check if current paper setting has scale setting + /// + bool has_scale() const; + + /// + /// Gets reference relationship Id + /// + const std::string& rel_id() const; + + /// + /// Sets reference relationship Id + /// + void rel_id(const std::string& val); + + /// + /// Check if current paper setting has a reference relationship + /// + bool has_rel_id() const; + /// /// The orientation /// @@ -175,6 +200,11 @@ public: bool operator==(const page_setup &rhs) const; private: + /// + /// Relationship Id + /// + std::string rel_id_; + /// /// The break /// @@ -188,7 +218,7 @@ private: /// /// The paper size /// - xlnt::paper_size paper_size_; + xlnt::optional paper_size_; /// /// Whether or not to fit to page @@ -208,7 +238,7 @@ private: /// /// The amount to scale the worksheet /// - double scale_; + xlnt::optional scale_; }; } // namespace xlnt diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index b3fe56c2..99249114 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -955,19 +955,19 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) } if (parser().attribute_present("gridLinesSet")) { - opts.print_grid_lines.set(parser().attribute("gridLinesSet")); + opts.grid_lines_set.set(parser().attribute("gridLinesSet")); } if (parser().attribute_present("headings")) { - opts.print_grid_lines.set(parser().attribute("headings")); + opts.print_headings.set(parser().attribute("headings")); } if (parser().attribute_present("horizontalCentered")) { - opts.print_grid_lines.set(parser().attribute("horizontalCentered")); + opts.horizontal_centered.set(parser().attribute("horizontalCentered")); } if (parser().attribute_present("verticalCentered")) { - opts.print_grid_lines.set(parser().attribute("verticalCentered")); + opts.vertical_centered.set(parser().attribute("verticalCentered")); } ws.d_->print_options_.set(opts); skip_remaining_content(current_worksheet_element); @@ -1000,6 +1000,18 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) { setup.vertical_dpi_.set(parser().attribute("verticalDpi")); } + if (parser().attribute_present("paperSize")) + { + setup.paper_size(static_cast(parser().attribute("paperSize"))); + } + if (parser().attribute_present("scale")) + { + setup.scale(parser().attribute("scale")); + } + if (parser().attribute_present(qn("r", "id"))) + { + setup.rel_id(parser().attribute(qn("r", "id"))); + } ws.page_setup(setup); skip_remaining_content(current_worksheet_element); } diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index 12b363ed..c96c5817 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -2782,6 +2782,22 @@ void xlsx_producer::write_worksheet(const relationship &rel) write_end_element(xmlns, "hyperlinks"); } + if (ws.has_phonetic_properties()) + { + write_start_element(xmlns, phonetic_pr::Serialised_ID()); + const auto &ph_props = ws.phonetic_properties(); + write_attribute("fontId", ph_props.font_id()); + if (ph_props.has_type()) + { + write_attribute("type", phonetic_pr::type_as_string(ph_props.type())); + } + if (ph_props.has_alignment()) + { + write_attribute("alignment", phonetic_pr::alignment_as_string(ph_props.alignment())); + } + write_end_element(xmlns, phonetic_pr::Serialised_ID()); + } + if (ws.d_->print_options_.is_set()) { auto &opts = ws.d_->print_options_.get(); @@ -2809,22 +2825,6 @@ void xlsx_producer::write_worksheet(const relationship &rel) write_end_element(xmlns, "printOptions"); } - if (ws.has_phonetic_properties()) - { - write_start_element(xmlns, phonetic_pr::Serialised_ID()); - const auto &ph_props = ws.phonetic_properties(); - write_attribute("fontId", ph_props.font_id()); - if (ph_props.has_type()) - { - write_attribute("type", phonetic_pr::type_as_string(ph_props.type())); - } - if (ph_props.has_alignment()) - { - write_attribute("alignment", phonetic_pr::alignment_as_string(ph_props.alignment())); - } - write_end_element(xmlns, phonetic_pr::Serialised_ID()); - } - if (ws.has_page_margins()) { write_start_element(xmlns, "pageMargins"); @@ -2841,6 +2841,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (ws.has_page_setup()) { + const xlnt::page_setup &ps = ws.page_setup(); write_start_element(xmlns, "pageSetup"); if (ws.page_setup().orientation_.is_set()) { @@ -2854,8 +2855,22 @@ void xlsx_producer::write_worksheet(const relationship &rel) { write_attribute("verticalDpi", ws.page_setup().vertical_dpi_.get()); } - /*write_attribute("paperSize", static_cast(ws.page_setup().paper_size())); - write_attribute("fitToHeight", write_bool(ws.page_setup().fit_to_height())); + + if (ps.has_paper_size()) + { + write_attribute("paperSize", static_cast(ps.paper_size())); + } + + if (ps.has_scale()) + { + write_attribute("scale", ps.scale()); + } + + if (ps.has_rel_id()) + { + write_attribute(xml::qname(xmlns_r, "id"), ps.rel_id()); + } + /*write_attribute("fitToHeight", write_bool(ws.page_setup().fit_to_height())); write_attribute("fitToWidth", write_bool(ws.page_setup().fit_to_width()));*/ write_end_element(xmlns, "pageSetup"); } diff --git a/source/worksheet/page_setup.cpp b/source/worksheet/page_setup.cpp index 19767e2f..747abf5e 100644 --- a/source/worksheet/page_setup.cpp +++ b/source/worksheet/page_setup.cpp @@ -59,7 +59,7 @@ void page_setup::sheet_state(xlnt::sheet_state sheet_state) paper_size page_setup::paper_size() const { - return paper_size_; + return paper_size_.get(); } void page_setup::paper_size(xlnt::paper_size paper_size) @@ -67,6 +67,11 @@ void page_setup::paper_size(xlnt::paper_size paper_size) paper_size_ = paper_size; } +bool page_setup::has_paper_size() const +{ + return this->paper_size_.is_set(); +} + bool page_setup::fit_to_page() const { return fit_to_page_; @@ -104,7 +109,27 @@ void page_setup::scale(double scale) double page_setup::scale() const { - return scale_; + return scale_.get(); +} + +bool page_setup::has_scale() const +{ + return this->scale_.is_set(); +} + +const std::string& page_setup::rel_id() const +{ + return this->rel_id_; +} + +void page_setup::rel_id(const std::string& val) +{ + this->rel_id_ = val; +} + +bool page_setup::has_rel_id() const +{ + return !rel_id_.empty(); } bool page_setup::operator==(const page_setup &rhs) const @@ -115,7 +140,9 @@ bool page_setup::operator==(const page_setup &rhs) const && fit_to_page_ == rhs.fit_to_page_ && fit_to_height_ == rhs.fit_to_height_ && fit_to_width_ == rhs.fit_to_width_ - && detail::float_equals(scale_, rhs.scale_); + && scale_ == rhs.scale_ + && paper_size_ == rhs.paper_size_ + && rel_id_ == rhs.rel_id_; } } // namespace xlnt From 1be73c40f8558a0f169ea7a5c9a172232a4ba663 Mon Sep 17 00:00:00 2001 From: "Wu, Ganhao" Date: Sat, 4 Sep 2021 01:18:38 +0800 Subject: [PATCH 2/4] Fix character set error when compiling under non-English Windows. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 9842ca68..41d9f2cb 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ python/xlntpyarrow.egg-info/ /x64/ .envrc .vscode +/.vs/ +/out/ +/CMakeSettings.json From b81d76a22311c1d8dce06dcd078635199c927623 Mon Sep 17 00:00:00 2001 From: "Wu, Ganhao" Date: Sat, 4 Sep 2021 02:15:19 +0800 Subject: [PATCH 3/4] Fix test case - page size now optional. --- .gitignore | 1 + source/worksheet/page_setup.cpp | 4 +--- tests/worksheet/page_setup_test_suite.cpp | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 41d9f2cb..41450d72 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ python/xlntpyarrow.egg-info/ /x64/ .envrc .vscode +/dist/ /.vs/ /out/ /CMakeSettings.json diff --git a/source/worksheet/page_setup.cpp b/source/worksheet/page_setup.cpp index 747abf5e..3cf7a6dc 100644 --- a/source/worksheet/page_setup.cpp +++ b/source/worksheet/page_setup.cpp @@ -29,11 +29,9 @@ namespace xlnt { page_setup::page_setup() : break_(xlnt::page_break::none), sheet_state_(xlnt::sheet_state::visible), - paper_size_(xlnt::paper_size::letter), fit_to_page_(false), fit_to_height_(false), - fit_to_width_(false), - scale_(1) + fit_to_width_(false) { } diff --git a/tests/worksheet/page_setup_test_suite.cpp b/tests/worksheet/page_setup_test_suite.cpp index 5e5914d0..f004ef3d 100644 --- a/tests/worksheet/page_setup_test_suite.cpp +++ b/tests/worksheet/page_setup_test_suite.cpp @@ -38,7 +38,7 @@ public: { xlnt::page_setup ps; - xlnt_assert_equals(ps.paper_size(), xlnt::paper_size::letter); + xlnt_assert(!ps.has_paper_size()); ps.paper_size(xlnt::paper_size::executive); xlnt_assert_equals(ps.paper_size(), xlnt::paper_size::executive); From c9d896c816baae8c2d4ec63ca53a1480bfc1e7d5 Mon Sep 17 00:00:00 2001 From: "Wu, Ganhao" Date: Sat, 4 Sep 2021 02:15:55 +0800 Subject: [PATCH 4/4] Fix character set error when compiling under non-English Windows. --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cf84a44..04be58e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.1) project(xlnt_all) +add_compile_options("$<$:/utf-8>") +add_compile_options("$<$:/utf-8>") + # CTest setup # include (CTest) # Add this for valgrind support; CTest works without it enable_testing()