From a63984969e3e100a5d6d5b2308be2f896698f75d Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Fri, 16 Oct 2015 18:35:11 -0400 Subject: [PATCH] improve date handling and printing --- include/xlnt/cell/cell.hpp | 16 +- include/xlnt/reader/reader.hpp | 4 +- include/xlnt/styles/number_format.hpp | 40 ++- include/xlnt/writer/style_writer.hpp | 3 +- samples/sample1.xlsx | Bin 492148 -> 491964 bytes source/cell.cpp | 424 ++++++++++++++++++++++++-- source/detail/cell_impl.hpp | 3 +- source/number_format.cpp | 17 +- source/reader.cpp | 52 ++-- source/style_writer.cpp | 20 ++ source/workbook.cpp | 10 +- source/workbook_writer.cpp | 2 +- tests/test_cell.hpp | 7 +- tests/test_read.hpp | 2 +- tests/test_style.hpp | 319 ------------------- tests/test_style_writer.hpp | 284 +++++++++++++++++ tests/test_worksheet.hpp | 7 - 17 files changed, 813 insertions(+), 397 deletions(-) delete mode 100644 tests/test_style.hpp create mode 100644 tests/test_style_writer.hpp diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index 30f5f63e..82ddf70d 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -115,7 +115,7 @@ public: // style shortcuts std::string get_number_format(); std::string get_number_format() const; - void set_number_format(const std::string &format_code); + void set_number_format(const std::string &format_code, int index = -1); font &get_font(); const font &get_font() const; fill &get_fill(); @@ -144,6 +144,16 @@ public: bool has_formula() const; // printing + + /// + /// Returns a string describing this cell like . + /// + std::string to_repr() const; + + /// + /// Returns a string representing the value of this cell. If the data type is not a string, + /// it will be converted according to the number format. + /// std::string to_string() const; // merging @@ -161,8 +171,6 @@ public: // operators cell &operator=(const cell &rhs); - std::ostream &print(std::ostream &stream, bool convert) const; - bool operator==(const cell &comparand) const; bool operator==(std::nullptr_t) const; @@ -178,7 +186,7 @@ private: inline std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) { - return cell.print(stream, true); + return stream << cell.to_string(); } } // namespace xlnt diff --git a/include/xlnt/reader/reader.hpp b/include/xlnt/reader/reader.hpp index a94d527c..001dd855 100644 --- a/include/xlnt/reader/reader.hpp +++ b/include/xlnt/reader/reader.hpp @@ -46,8 +46,8 @@ public: static std::vector read_relationships(zip_file &content, const std::string &filename); static std::vector> read_content_types(zip_file &archive); static std::string determine_document_type(const std::vector> &override_types); - static worksheet read_worksheet(std::istream &handle, workbook &wb, const std::string &title, const std::vector &string_table); - static void read_worksheet(worksheet ws, const std::string &xml_string, const std::vector &string_table, const std::vector &number_format_ids); + static worksheet read_worksheet(std::istream &handle, workbook &wb, const std::string &title, const std::vector &string_table, const std::unordered_map &custom_number_formats); + static void read_worksheet(worksheet ws, const std::string &xml_string, const std::vector &string_table, const std::vector &number_format_ids, const std::unordered_map &custom_number_formats); static std::vector read_shared_string(const std::string &xml_string); static std::string read_dimension(const std::string &xml_string); static document_properties read_properties_core(const std::string &xml_string); diff --git a/include/xlnt/styles/number_format.hpp b/include/xlnt/styles/number_format.hpp index 08579886..dde0e70c 100644 --- a/include/xlnt/styles/number_format.hpp +++ b/include/xlnt/styles/number_format.hpp @@ -90,11 +90,45 @@ public: static bool is_builtin(const std::string &format); number_format() : format_code_(format::general), format_index_(0) {} - number_format(format code) : format_code_(code) {} + number_format(format code) : format_code_(code), format_index_(reversed_builtin_formats().at(format_strings().at(code))) {} format get_format_code() const { return format_code_; } - void set_format_code(format format_code) { format_code_ = format_code; } - void set_format_code_string(const std::string &format_code) { custom_format_code_ = format_code; format_code_ = format::unknown; } + + void set_format_code(format format_code, int index = -1) + { + format_code_ = format_code; + + if(format_code_ != format::unknown) + { + set_format_code_string(format_strings().at(format_code), index); + } + } + + void set_format_code_string(const std::string &format_code, int index) + { + custom_format_code_ = format_code; + format_index_ = index; + + const auto &reversed = reversed_builtin_formats(); + auto match = reversed.find(format_code); + + format_code_ = format::unknown; + + if(match != reversed.end()) + { + format_index_ = match->second; + + for(const auto &p : format_strings()) + { + if(p.second == format_code) + { + format_code_ = p.first; + break; + } + } + } + + } std::string get_format_code_string() const; private: diff --git a/include/xlnt/writer/style_writer.hpp b/include/xlnt/writer/style_writer.hpp index 716093ff..abd551b4 100644 --- a/include/xlnt/writer/style_writer.hpp +++ b/include/xlnt/writer/style_writer.hpp @@ -43,6 +43,8 @@ public: std::string write_table() const; std::vector