From c7c5eec2abc439f4a36bdab16fc7c6481e8e67e1 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Tue, 13 Oct 2015 14:19:46 -0400 Subject: [PATCH] add more functionality for printing cells --- include/xlnt/cell/cell.hpp | 8 +-- include/xlnt/common/datetime.hpp | 1 + source/cell.cpp | 89 ++++++++++++++++++++++---------- source/datetime.cpp | 5 ++ 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index 361b8604..864993a7 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -79,9 +79,7 @@ public: bool has_value() const; template - T get_value(); - template - const T get_value() const; + T get_value() const; void clear_value(); @@ -154,6 +152,8 @@ 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; @@ -169,7 +169,7 @@ private: inline std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) { - return stream << cell.to_string(); + return cell.print(stream, true); } } // namespace xlnt diff --git a/include/xlnt/common/datetime.hpp b/include/xlnt/common/datetime.hpp index 0a1303ec..7d2d33b4 100644 --- a/include/xlnt/common/datetime.hpp +++ b/include/xlnt/common/datetime.hpp @@ -133,6 +133,7 @@ struct datetime { } + std::string to_string(calendar base_date) const; long double to_number(calendar base_date) const; bool operator==(const datetime &comparand) const; diff --git a/source/cell.cpp b/source/cell.cpp index 2d5de479..e5c87674 100644 --- a/source/cell.cpp +++ b/source/cell.cpp @@ -716,116 +716,149 @@ void cell::clear_value() } template<> -bool cell::get_value() +bool cell::get_value() const { return d_->value_numeric_ != 0; } template<> -std::int8_t cell::get_value() +std::int8_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::int16_t cell::get_value() +std::int16_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::int32_t cell::get_value() +std::int32_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::int64_t cell::get_value() +std::int64_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::uint8_t cell::get_value() +std::uint8_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::uint16_t cell::get_value() +std::uint16_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::uint32_t cell::get_value() +std::uint32_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -std::uint64_t cell::get_value() +std::uint64_t cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -float cell::get_value() +float cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -double cell::get_value() +double cell::get_value() const { return static_cast(d_->value_numeric_); } template<> -long double cell::get_value() +long double cell::get_value() const { return d_->value_numeric_; } template<> -time cell::get_value() +time cell::get_value() const { return time::from_number(d_->value_numeric_); } template<> -datetime cell::get_value() +datetime cell::get_value() const { return datetime::from_number(d_->value_numeric_, xlnt::calendar::windows_1900); } template<> -date cell::get_value() +date cell::get_value() const { return date::from_number(d_->value_numeric_, xlnt::calendar::windows_1900); } template<> -timedelta cell::get_value() +timedelta cell::get_value() const { return timedelta(0, 0); //return timedelta::from_number(d_->value_numeric_); } - - void cell::set_number_format(const std::string &format_string) + +void cell::set_number_format(const std::string &format_string) +{ + get_style().get_number_format().set_format_code_string(format_string); +} + +template<> +std::string cell::get_value() const +{ + return d_->value_string_; +} + +bool cell::has_value() const +{ + return d_->type_ != cell::type::null; +} + +std::ostream &cell::print(std::ostream &stream, bool convert) const +{ + if(!convert) { - get_style().get_number_format().set_format_code_string(format_string); + return stream << get_value(); } - - template<> - std::string cell::get_value() + else { - return d_->value_string_; - } - - bool cell::has_value() const - { - return d_->type_ != cell::type::null; + switch(get_data_type()) + { + case type::null: + return stream << ""; + case type::string: + return stream << get_value(); + case type::numeric: + if(is_date()) + { + return stream << get_value().to_string(get_parent().get_parent().get_properties().excel_base_date); + } + else + { + return stream << get_value(); + } + case type::error: + return stream << get_value(); + case type::formula: + return stream << d_->formula_; + default: + return stream; + } } +} } // namespace xlnt diff --git a/source/datetime.cpp b/source/datetime.cpp index 6035d5b1..74aa2e80 100644 --- a/source/datetime.cpp +++ b/source/datetime.cpp @@ -163,6 +163,11 @@ long double datetime::to_number(calendar base_date) const + time(hour, minute, second, microsecond).to_number(); } +std::string datetime::to_string(xlnt::calendar base_date) const +{ + return std::to_string(year) + "/" + std::to_string(month) + "/" + std::to_string(day) + " " +std::to_string(hour) + ":" + std::to_string(minute) + ":" + std::to_string(second) + ":" + std::to_string(microsecond); +} + date date::today() { std::time_t raw_time = std::time(0);