add more functionality for printing cells

pull/16/head
Thomas Fussell 2015-10-13 14:19:46 -04:00
parent 301e81d698
commit c7c5eec2ab
4 changed files with 71 additions and 32 deletions

View File

@ -79,9 +79,7 @@ public:
bool has_value() const;
template<typename T>
T get_value();
template<typename T>
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

View File

@ -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;

View File

@ -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<std::int8_t>(d_->value_numeric_);
}
template<>
std::int16_t cell::get_value()
std::int16_t cell::get_value() const
{
return static_cast<std::int16_t>(d_->value_numeric_);
}
template<>
std::int32_t cell::get_value()
std::int32_t cell::get_value() const
{
return static_cast<std::int32_t>(d_->value_numeric_);
}
template<>
std::int64_t cell::get_value()
std::int64_t cell::get_value() const
{
return static_cast<std::int64_t>(d_->value_numeric_);
}
template<>
std::uint8_t cell::get_value()
std::uint8_t cell::get_value() const
{
return static_cast<std::uint8_t>(d_->value_numeric_);
}
template<>
std::uint16_t cell::get_value()
std::uint16_t cell::get_value() const
{
return static_cast<std::uint16_t>(d_->value_numeric_);
}
template<>
std::uint32_t cell::get_value()
std::uint32_t cell::get_value() const
{
return static_cast<std::uint32_t>(d_->value_numeric_);
}
template<>
std::uint64_t cell::get_value()
std::uint64_t cell::get_value() const
{
return static_cast<std::uint64_t>(d_->value_numeric_);
}
template<>
float cell::get_value()
float cell::get_value() const
{
return static_cast<float>(d_->value_numeric_);
}
template<>
double cell::get_value()
double cell::get_value() const
{
return static_cast<double>(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<std::string>();
}
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<std::string>();
case type::numeric:
if(is_date())
{
return stream << get_value<datetime>().to_string(get_parent().get_parent().get_properties().excel_base_date);
}
else
{
return stream << get_value<long double>();
}
case type::error:
return stream << get_value<std::string>();
case type::formula:
return stream << d_->formula_;
default:
return stream;
}
}
}
} // namespace xlnt

View File

@ -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);