reformat cell.cpp

This commit is contained in:
Thomas Fussell 2016-11-19 16:50:35 -05:00
parent dab1b0218d
commit d393343c2e

View File

@ -21,6 +21,7 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <sstream> #include <sstream>
@ -43,9 +44,9 @@
#include <xlnt/styles/style.hpp> #include <xlnt/styles/style.hpp>
#include <xlnt/utils/date.hpp> #include <xlnt/utils/date.hpp>
#include <xlnt/utils/datetime.hpp> #include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/utils/time.hpp> #include <xlnt/utils/time.hpp>
#include <xlnt/utils/timedelta.hpp> #include <xlnt/utils/timedelta.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp> #include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/column_properties.hpp> #include <xlnt/worksheet/column_properties.hpp>
#include <xlnt/worksheet/row_properties.hpp> #include <xlnt/worksheet/row_properties.hpp>
@ -55,90 +56,90 @@ namespace {
std::pair<bool, long double> cast_numeric(const std::string &s) std::pair<bool, long double> cast_numeric(const std::string &s)
{ {
const char *str = s.c_str(); const char *str = s.c_str();
char *str_end = nullptr; char *str_end = nullptr;
auto result = std::strtold(str, &str_end); auto result = std::strtold(str, &str_end);
if (str_end != str + s.size()) return{ false, 0 }; if (str_end != str + s.size()) return {false, 0};
return{ true, result }; return {true, result};
} }
std::pair<bool, long double> cast_percentage(const std::string &s) std::pair<bool, long double> cast_percentage(const std::string &s)
{ {
if (s.back() == '%') if (s.back() == '%')
{ {
auto number = cast_numeric(s.substr(0, s.size() - 1)); auto number = cast_numeric(s.substr(0, s.size() - 1));
if (number.first) if (number.first)
{ {
return{ true, number.second / 100 }; return {true, number.second / 100};
} }
} }
return{ false, 0 }; return {false, 0};
} }
std::pair<bool, xlnt::time> cast_time(const std::string &s) std::pair<bool, xlnt::time> cast_time(const std::string &s)
{ {
xlnt::time result; xlnt::time result;
std::vector<std::string> time_components; std::vector<std::string> time_components;
std::size_t prev = 0; std::size_t prev = 0;
auto colon_index = s.find(':'); auto colon_index = s.find(':');
while (colon_index != std::string::npos) while (colon_index != std::string::npos)
{ {
time_components.push_back(s.substr(prev, colon_index - prev)); time_components.push_back(s.substr(prev, colon_index - prev));
prev = colon_index + 1; prev = colon_index + 1;
colon_index = s.find(':', colon_index + 1); colon_index = s.find(':', colon_index + 1);
} }
time_components.push_back(s.substr(prev, colon_index - prev)); time_components.push_back(s.substr(prev, colon_index - prev));
if (time_components.size() < 2 || time_components.size() > 3) if (time_components.size() < 2 || time_components.size() > 3)
{ {
return{ false, result }; return {false, result};
} }
std::vector<double> numeric_components; std::vector<double> numeric_components;
for (auto component : time_components) for (auto component : time_components)
{ {
if (component.empty() || (component.substr(0, component.find('.')).size() > 2)) if (component.empty() || (component.substr(0, component.find('.')).size() > 2))
{ {
return{ false, result }; return {false, result};
} }
for (auto d : component) for (auto d : component)
{ {
if (!(d >= '0' && d <= '9') && d != '.') if (!(d >= '0' && d <= '9') && d != '.')
{ {
return{ false, result }; return {false, result};
} }
} }
auto without_leading_zero = component.front() == '0' ? component.substr(1) : component; auto without_leading_zero = component.front() == '0' ? component.substr(1) : component;
auto numeric = std::stod(without_leading_zero); auto numeric = std::stod(without_leading_zero);
numeric_components.push_back(numeric); numeric_components.push_back(numeric);
} }
result.hour = static_cast<int>(numeric_components[0]); result.hour = static_cast<int>(numeric_components[0]);
result.minute = static_cast<int>(numeric_components[1]); result.minute = static_cast<int>(numeric_components[1]);
if (std::fabs(static_cast<double>(result.minute) - numeric_components[1]) > std::numeric_limits<double>::epsilon()) if (std::fabs(static_cast<double>(result.minute) - numeric_components[1]) > std::numeric_limits<double>::epsilon())
{ {
result.minute = result.hour; result.minute = result.hour;
result.hour = 0; result.hour = 0;
result.second = static_cast<int>(numeric_components[1]); result.second = static_cast<int>(numeric_components[1]);
result.microsecond = static_cast<int>((numeric_components[1] - result.second) * 1E6); result.microsecond = static_cast<int>((numeric_components[1] - result.second) * 1E6);
} }
else if (numeric_components.size() > 2) else if (numeric_components.size() > 2)
{ {
result.second = static_cast<int>(numeric_components[2]); result.second = static_cast<int>(numeric_components[2]);
result.microsecond = static_cast<int>((numeric_components[2] - result.second) * 1E6); result.microsecond = static_cast<int>((numeric_components[2] - result.second) * 1E6);
} }
return{ true, result }; return {true, result};
} }
} // namespace } // namespace
@ -147,16 +148,16 @@ namespace xlnt {
const std::unordered_map<std::string, int> &cell::error_codes() const std::unordered_map<std::string, int> &cell::error_codes()
{ {
static const std::unordered_map<std::string, int> *codes = static const auto *codes = new std::unordered_map<std::string, int>
new std::unordered_map<std::string, int>({ {
{ "#NULL!", 0 }, {"#NULL!", 0},
{ "#DIV/0!", 1 }, {"#DIV/0!", 1},
{ "#VALUE!", 2 }, {"#VALUE!", 2},
{ "#REF!", 3 }, {"#REF!", 3},
{ "#NAME?", 4 }, {"#NAME?", 4},
{ "#NUM!", 5 }, {"#NUM!", 5},
{ "#N/A!", 6 } {"#N/A!", 6}
}); };
return *codes; return *codes;
} }
@ -198,7 +199,7 @@ bool cell::garbage_collectible() const
template <> template <>
XLNT_API void cell::set_value(std::nullptr_t) XLNT_API void cell::set_value(std::nullptr_t)
{ {
d_->type_ = type::null; d_->type_ = type::null;
} }
template <> template <>
@ -313,32 +314,32 @@ XLNT_API void cell::set_value(long double d)
template <> template <>
XLNT_API void cell::set_value(std::string s) XLNT_API void cell::set_value(std::string s)
{ {
s = check_string(s); s = check_string(s);
if (s.size() > 1 && s.front() == '=') if (s.size() > 1 && s.front() == '=')
{ {
d_->type_ = type::formula; d_->type_ = type::formula;
set_formula(s); set_formula(s);
} }
else if (cell::error_codes().find(s) != cell::error_codes().end()) else if (cell::error_codes().find(s) != cell::error_codes().end())
{ {
set_error(s); set_error(s);
} }
else else
{ {
d_->type_ = type::string; d_->type_ = type::string;
d_->value_text_.plain_text(s); d_->value_text_.plain_text(s);
if (s.size() > 0) if (s.size() > 0)
{ {
get_workbook().add_shared_string(d_->value_text_); get_workbook().add_shared_string(d_->value_text_);
} }
} }
if (get_workbook().get_guess_types()) if (get_workbook().get_guess_types())
{ {
guess_type_and_set_value(s); guess_type_and_set_value(s);
} }
} }
template <> template <>
@ -427,14 +428,12 @@ bool cell::is_merged() const
bool cell::is_date() const bool cell::is_date() const
{ {
return get_data_type() == type::numeric return get_data_type() == type::numeric && has_format() && get_number_format().is_date_format();
&& has_format()
&& get_number_format().is_date_format();
} }
cell_reference cell::get_reference() const cell_reference cell::get_reference() const
{ {
return { d_->column_, d_->row_ }; return {d_->column_, d_->row_};
} }
bool cell::operator==(std::nullptr_t) const bool cell::operator==(std::nullptr_t) const
@ -449,16 +448,16 @@ bool cell::operator==(const cell &comparand) const
cell &cell::operator=(const cell &rhs) cell &cell::operator=(const cell &rhs)
{ {
d_->column_ = rhs.d_->column_; d_->column_ = rhs.d_->column_;
d_->format_ = rhs.d_->format_; d_->format_ = rhs.d_->format_;
d_->formula_ = rhs.d_->formula_; d_->formula_ = rhs.d_->formula_;
d_->hyperlink_ = rhs.d_->hyperlink_; d_->hyperlink_ = rhs.d_->hyperlink_;
d_->is_merged_ = rhs.d_->is_merged_; d_->is_merged_ = rhs.d_->is_merged_;
d_->parent_ = rhs.d_->parent_; d_->parent_ = rhs.d_->parent_;
d_->row_ = rhs.d_->row_; d_->row_ = rhs.d_->row_;
d_->type_ = rhs.d_->type_; d_->type_ = rhs.d_->type_;
d_->value_numeric_ = rhs.d_->value_numeric_; d_->value_numeric_ = rhs.d_->value_numeric_;
d_->value_text_ = rhs.d_->value_text_; d_->value_text_ = rhs.d_->value_text_;
return *this; return *this;
} }
@ -470,7 +469,7 @@ std::string cell::to_repr() const
std::string cell::get_hyperlink() const std::string cell::get_hyperlink() const
{ {
return d_->hyperlink_.get(); return d_->hyperlink_.get();
} }
void cell::set_hyperlink(const std::string &hyperlink) void cell::set_hyperlink(const std::string &hyperlink)
@ -480,7 +479,7 @@ void cell::set_hyperlink(const std::string &hyperlink)
throw invalid_parameter(); throw invalid_parameter();
} }
d_->hyperlink_ = hyperlink; d_->hyperlink_ = hyperlink;
if (get_data_type() == type::null) if (get_data_type() == type::null)
{ {
@ -507,7 +506,7 @@ void cell::set_formula(const std::string &formula)
bool cell::has_formula() const bool cell::has_formula() const
{ {
return d_->formula_; return d_->formula_;
} }
std::string cell::get_formula() const std::string cell::get_formula() const
@ -548,24 +547,22 @@ const worksheet cell::get_worksheet() const
workbook &cell::get_workbook() workbook &cell::get_workbook()
{ {
return get_worksheet().get_workbook(); return get_worksheet().get_workbook();
} }
const workbook &cell::get_workbook() const const workbook &cell::get_workbook() const
{ {
return get_worksheet().get_workbook(); return get_worksheet().get_workbook();
} }
//TODO: this shares a lot of code with worksheet::get_point_pos, try to reduce repetion // TODO: this shares a lot of code with worksheet::get_point_pos, try to reduce repetion
std::pair<int, int> cell::get_anchor() const std::pair<int, int> cell::get_anchor() const
{ {
static const auto DefaultColumnWidth = 51.85L; static const auto DefaultColumnWidth = 51.85L;
static const auto DefaultRowHeight = 15.0L; static const auto DefaultRowHeight = 15.0L;
auto points_to_pixels = [](long double value, long double dpi) auto points_to_pixels = [](
{ long double value, long double dpi) { return static_cast<int>(std::ceil(value * dpi / 72)); };
return static_cast<int>(std::ceil(value * dpi / 72));
};
auto left_columns = d_->column_ - 1; auto left_columns = d_->column_ - 1;
int left_anchor = 0; int left_anchor = 0;
@ -607,7 +604,7 @@ std::pair<int, int> cell::get_anchor() const
top_anchor += default_height; top_anchor += default_height;
} }
return { left_anchor, top_anchor }; return {left_anchor, top_anchor};
} }
cell::type cell::get_data_type() const cell::type cell::get_data_type() const
@ -622,32 +619,32 @@ void cell::set_data_type(type t)
number_format cell::computed_number_format() const number_format cell::computed_number_format() const
{ {
return xlnt::number_format(); return xlnt::number_format();
} }
font cell::computed_font() const font cell::computed_font() const
{ {
return xlnt::font(); return xlnt::font();
} }
fill cell::computed_fill() const fill cell::computed_fill() const
{ {
return xlnt::fill(); return xlnt::fill();
} }
border cell::computed_border() const border cell::computed_border() const
{ {
return xlnt::border(); return xlnt::border();
} }
alignment cell::computed_alignment() const alignment cell::computed_alignment() const
{ {
return xlnt::alignment(); return xlnt::alignment();
} }
protection cell::computed_protection() const protection cell::computed_protection() const
{ {
return xlnt::protection(); return xlnt::protection();
} }
void cell::clear_value() void cell::clear_value()
@ -823,7 +820,7 @@ bool cell::has_value() const
std::string cell::to_string() const std::string cell::to_string() const
{ {
auto nf = computed_number_format(); auto nf = computed_number_format();
switch (get_data_type()) switch (get_data_type())
{ {
@ -846,7 +843,7 @@ std::string cell::to_string() const
bool cell::has_format() const bool cell::has_format() const
{ {
return d_->format_.is_set(); return d_->format_.is_set();
} }
void cell::format(const class format new_format) void cell::format(const class format new_format)
@ -857,12 +854,12 @@ void cell::format(const class format new_format)
} }
++new_format.d_->references; ++new_format.d_->references;
d_->format_ = new_format.d_; d_->format_ = new_format.d_;
} }
calendar cell::get_base_date() const calendar cell::get_base_date() const
{ {
return get_workbook().get_base_date(); return get_workbook().get_base_date();
} }
std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
@ -872,41 +869,41 @@ std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
void cell::guess_type_and_set_value(const std::string &value) void cell::guess_type_and_set_value(const std::string &value)
{ {
auto percentage = cast_percentage(value); auto percentage = cast_percentage(value);
if (percentage.first) if (percentage.first)
{ {
d_->value_numeric_ = percentage.second; d_->value_numeric_ = percentage.second;
d_->type_ = cell::type::numeric; d_->type_ = cell::type::numeric;
set_number_format(xlnt::number_format::percentage()); set_number_format(xlnt::number_format::percentage());
} }
else else
{ {
auto time = cast_time(value); auto time = cast_time(value);
if (time.first) if (time.first)
{ {
d_->type_ = cell::type::numeric; d_->type_ = cell::type::numeric;
set_number_format(number_format::date_time6()); set_number_format(number_format::date_time6());
d_->value_numeric_ = time.second.to_number(); d_->value_numeric_ = time.second.to_number();
} }
else else
{ {
auto numeric = cast_numeric(value); auto numeric = cast_numeric(value);
if (numeric.first) if (numeric.first)
{ {
d_->value_numeric_ = numeric.second; d_->value_numeric_ = numeric.second;
d_->type_ = cell::type::numeric; d_->type_ = cell::type::numeric;
} }
} }
} }
} }
void cell::clear_format() void cell::clear_format()
{ {
format().d_->references -= format().d_->references > 0 ? 1 : 0; format().d_->references -= format().d_->references > 0 ? 1 : 0;
d_->format_.clear(); d_->format_.clear();
} }
void cell::clear_style() void cell::clear_style()
@ -932,7 +929,7 @@ const style cell::style() const
{ {
if (!has_format() || !format().has_style()) if (!has_format() || !format().has_style())
{ {
throw invalid_attribute(); throw invalid_attribute();
} }
return format().style(); return format().style();
@ -945,57 +942,57 @@ bool cell::has_style() const
format cell::modifiable_format() format cell::modifiable_format()
{ {
if (!d_->format_) if (!d_->format_)
{ {
throw invalid_attribute(); throw invalid_attribute();
} }
return xlnt::format(*d_->format_); return xlnt::format(*d_->format_);
} }
const format cell::format() const const format cell::format() const
{ {
if (!d_->format_) if (!d_->format_)
{ {
throw invalid_attribute(); throw invalid_attribute();
} }
return xlnt::format(*d_->format_); return xlnt::format(*d_->format_);
} }
alignment cell::get_alignment() const alignment cell::get_alignment() const
{ {
return format().alignment(); return format().alignment();
} }
border cell::get_border() const border cell::get_border() const
{ {
return format().border(); return format().border();
} }
fill cell::get_fill() const fill cell::get_fill() const
{ {
return format().fill(); return format().fill();
} }
font cell::get_font() const font cell::get_font() const
{ {
return format().font(); return format().font();
} }
number_format cell::get_number_format() const number_format cell::get_number_format() const
{ {
return format().number_format(); return format().number_format();
} }
protection cell::get_protection() const protection cell::get_protection() const
{ {
return format().protection(); return format().protection();
} }
bool cell::has_hyperlink() const bool cell::has_hyperlink() const
{ {
return d_->hyperlink_; return d_->hyperlink_;
} }
// comment // comment
@ -1014,7 +1011,7 @@ class comment cell::comment()
{ {
if (!has_comment()) if (!has_comment())
{ {
throw xlnt::exception("cell has no comment"); throw xlnt::exception("cell has no comment");
} }
return d_->comment_.get(); return d_->comment_.get();
@ -1055,7 +1052,7 @@ void cell::comment(const class comment &new_comment)
d_->comment_.get().position(cell_position.first, cell_position.second); d_->comment_.get().position(cell_position.first, cell_position.second);
d_->comment_.get().size(200, 100); d_->comment_.get().size(200, 100);
get_worksheet().register_comments_in_manifest(); get_worksheet().register_comments_in_manifest();
} }
} // namespace xlnt } // namespace xlnt