xlnt/source/cell.cpp

525 lines
11 KiB
C++
Raw Normal View History

2014-05-22 05:48:51 +08:00
#include <algorithm>
#include <locale>
2014-05-21 22:20:30 +08:00
#include <sstream>
2014-08-14 06:56:34 +08:00
#include <xlnt/cell/cell.hpp>
#include <xlnt/cell/cell_reference.hpp>
#include <xlnt/cell/comment.hpp>
#include <xlnt/cell/value.hpp>
#include <xlnt/common/datetime.hpp>
#include <xlnt/common/relationship.hpp>
#include <xlnt/worksheet/worksheet.hpp>
#include <xlnt/common/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/workbook/document_properties.hpp>
2014-06-06 04:19:31 +08:00
#include "detail/cell_impl.hpp"
2014-05-21 22:20:30 +08:00
2014-07-26 04:39:25 +08:00
namespace {
2014-05-21 22:20:30 +08:00
std::vector<std::string> split_string(const std::string &string, char delim = ' ')
{
std::stringstream ss(string);
std::string part;
std::vector<std::string> parts;
while(std::getline(ss, part, delim))
{
parts.push_back(part);
}
return parts;
}
2014-07-26 04:39:25 +08:00
xlnt::value::type data_type_for_value(const std::string &value)
2014-05-21 22:20:30 +08:00
{
if(value.empty())
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::null;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
2014-07-25 05:31:46 +08:00
if(value[0] == '0')
2014-05-21 22:20:30 +08:00
{
if(value.length() > 1)
{
if(value[1] == '.' || (value.length() > 2 && (value[1] == 'e' || value[1] == 'E')))
{
auto first_non_number = std::find_if(value.begin() + 2, value.end(),
2014-07-26 04:39:25 +08:00
[](char c) { return !std::isdigit(c, std::locale::classic()); });
2014-05-21 22:20:30 +08:00
if(first_non_number == value.end())
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::numeric;
2014-05-21 22:20:30 +08:00
}
}
auto split = split_string(value, ':');
if(split.size() == 2 || split.size() == 3)
{
for(auto part : split)
{
try
{
std::stoi(part);
}
catch(std::invalid_argument)
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::string;
2014-05-21 22:20:30 +08:00
}
}
2014-07-26 04:39:25 +08:00
return xlnt::value::type::numeric;
2014-05-21 22:20:30 +08:00
}
else
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::string;
2014-05-21 22:20:30 +08:00
}
}
2014-07-26 04:39:25 +08:00
return xlnt::value::type::numeric;
2014-05-21 22:20:30 +08:00
}
else if(value[0] == '#')
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::error;
2014-05-21 22:20:30 +08:00
}
else
{
char *p;
strtod(value.c_str(), &p);
if(*p != 0)
{
static const std::vector<std::string> possible_booleans = {"TRUE", "true", "FALSE", "false"};
if(std::find(possible_booleans.begin(), possible_booleans.end(), value) != possible_booleans.end())
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::boolean;
2014-05-21 22:20:30 +08:00
}
2014-06-11 06:36:31 +08:00
if(value.back() == '%')
{
strtod(value.substr(0, value.length() - 1).c_str(), &p);
if(*p == 0)
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::numeric;
2014-06-11 06:36:31 +08:00
}
}
2014-07-26 04:39:25 +08:00
return xlnt::value::type::string;
2014-05-21 22:20:30 +08:00
}
else
{
2014-07-26 04:39:25 +08:00
return xlnt::value::type::numeric;
2014-07-25 05:31:46 +08:00
}
2014-05-31 06:42:25 +08:00
}
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
} // namespace
2014-05-21 22:20:30 +08:00
2014-07-26 04:39:25 +08:00
namespace xlnt {
const xlnt::color xlnt::color::black(0);
const xlnt::color xlnt::color::white(1);
const std::unordered_map<std::string, int> cell::ErrorCodes =
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
{"#NULL!", 0},
{"#DIV/0!", 1},
{"#VALUE!", 2},
{"#REF!", 3},
{"#NAME?", 4},
{"#NUM!", 5},
{"#N/A!", 6}
};
2014-05-21 22:20:30 +08:00
2014-07-26 04:39:25 +08:00
cell::cell() : d_(nullptr)
2014-07-20 04:59:05 +08:00
{
}
2014-07-26 04:39:25 +08:00
cell::cell(detail::cell_impl *d) : d_(d)
2014-05-21 22:20:30 +08:00
{
}
2014-07-26 04:39:25 +08:00
cell::cell(worksheet worksheet, const cell_reference &reference) : d_(nullptr)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
cell self = worksheet.get_cell(reference);
d_ = self.d_;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
cell::cell(worksheet worksheet, const cell_reference &reference, const value &initial_value) : cell(worksheet, reference)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
set_value(initial_value);
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
bool cell::garbage_collectible() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return !(get_value().get_type() != value::type::null || is_merged() || has_comment() || has_formula());
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
value &cell::get_value()
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->value_;
2014-05-31 06:42:25 +08:00
}
2014-07-26 04:39:25 +08:00
const value &cell::get_value() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->value_;
2014-05-31 06:42:25 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const value &v)
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
d_->value_ = v;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const std::string &s)
2014-06-14 03:05:24 +08:00
{
2014-07-26 04:39:25 +08:00
if(!get_parent().get_parent().get_guess_types())
2014-06-14 03:05:24 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = false;
d_->value_ = value(s);
2014-06-14 03:05:24 +08:00
}
2014-07-26 04:39:25 +08:00
else
2014-06-14 03:05:24 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = false;
2014-06-14 03:05:24 +08:00
2014-07-26 04:39:25 +08:00
switch(data_type_for_value(s))
2014-06-14 03:05:24 +08:00
{
2014-07-26 04:39:25 +08:00
case value::type::numeric:
if(s.find(':') != std::string::npos)
{
d_->is_date_ = true;
d_->value_ = value(time(s).to_number());
}
else if(s.back() == '%')
{
d_->value_ = value(std::stod(s.substr(0, s.length() - 1)) / 100);
get_style().get_number_format().set_format_code(xlnt::number_format::format::percentage);
}
else
{
d_->value_ = value(std::stod(s));
}
break;
case value::type::boolean:
d_->value_ = value(s == "TRUE" || s == "true");
break;
case value::type::error:
d_->value_ = value(s);
break;
case value::type::string:
d_->value_ = value(s);
break;
case value::type::null:
d_->value_ = value::null();
break;
default: throw data_type_exception();
2014-06-14 03:05:24 +08:00
}
}
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const char *s)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
set_value(std::string(s));
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(bool b)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->value_ = value(b);
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(int i)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->value_ = value(i);
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(long long int i)
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
d_->value_ = value(i);
2014-05-31 06:42:25 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(double d)
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
d_->value_ = value(d);
2014-05-31 06:42:25 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const date &d)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = true;
2014-08-02 04:46:54 +08:00
auto date_format_code = xlnt::number_format::lookup_format(14);
auto number_format = xlnt::number_format(date_format_code);
get_style().set_number_format(number_format);
2014-07-26 04:39:25 +08:00
auto base_date = get_parent().get_parent().get_properties().excel_base_date;
set_value(d.to_number(base_date));
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const datetime &d)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = true;
2014-08-02 04:46:54 +08:00
auto date_format_code = xlnt::number_format::lookup_format(22);
auto number_format = xlnt::number_format(date_format_code);
get_style().set_number_format(number_format);
2014-07-26 04:39:25 +08:00
auto base_date = get_parent().get_parent().get_properties().excel_base_date;
set_value(d.to_number(base_date));
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const time &t)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = true;
set_value(t.to_number());
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_value(const timedelta &t)
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
d_->is_date_ = true;
set_value(t.to_number());
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
bool cell::has_style() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->style_ != nullptr;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
row_t cell::get_row() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->row_ + 1;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
std::string cell::get_column() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return cell_reference::column_string_from_index(d_->column_ + 1);
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
void cell::set_merged(bool merged)
2014-06-05 06:42:17 +08:00
{
2014-07-26 04:39:25 +08:00
d_->merged = merged;
2014-06-05 06:42:17 +08:00
}
2014-07-26 04:39:25 +08:00
bool cell::is_merged() const
2014-06-11 05:12:15 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->merged;
2014-06-11 05:12:15 +08:00
}
2014-07-26 04:39:25 +08:00
bool cell::is_date() const
2014-06-05 06:42:17 +08:00
{
2014-07-26 04:39:25 +08:00
return d_->is_date_ || (d_->style_ != nullptr && get_style().get_number_format().get_format_code() == number_format::format::date_xlsx14);
2014-06-05 06:42:17 +08:00
}
2014-07-26 04:39:25 +08:00
cell_reference cell::get_reference() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
return {d_->column_, d_->row_};
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
bool cell::operator==(std::nullptr_t) const
2014-07-25 05:31:46 +08:00
{
2014-07-26 04:39:25 +08:00
return d_ == nullptr;
2014-07-25 05:31:46 +08:00
}
2014-05-21 22:20:30 +08:00
2014-07-26 04:39:25 +08:00
bool cell::operator==(const cell &comparand) const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
if(comparand == nullptr)
2014-07-25 05:31:46 +08:00
{
2014-07-26 04:39:25 +08:00
return d_ == nullptr;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
return d_->value_ == comparand.d_->value_;
2014-05-21 22:20:30 +08:00
}
2014-07-26 04:39:25 +08:00
style &cell::get_style()
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
if(d_->style_ == nullptr)
{
d_->style_ = new style();
}
return *d_->style_;
2014-05-31 06:42:25 +08:00
}
2014-07-26 04:39:25 +08:00
const style &cell::get_style() const
2014-05-21 22:20:30 +08:00
{
2014-07-26 04:39:25 +08:00
if(d_->style_ == nullptr)
{
d_->style_ = new style();
}
return *d_->style_;
2014-05-31 06:42:25 +08:00
}
void cell::set_style(const xlnt::style &s)
{
get_style() = s;
}
2014-05-31 06:42:25 +08:00
2014-07-26 04:39:25 +08:00
cell &cell::operator=(const cell &rhs)
2014-05-31 06:42:25 +08:00
{
2014-07-26 04:39:25 +08:00
d_ = rhs.d_;
2014-05-21 22:20:30 +08:00
return *this;
}
2014-07-26 04:39:25 +08:00
bool operator<(cell left, cell right)
2014-07-20 04:59:05 +08:00
{
2014-07-26 04:39:25 +08:00
return left.get_reference() < right.get_reference();
2014-07-20 04:59:05 +08:00
}
2014-05-21 22:20:30 +08:00
std::string cell::to_string() const
{
2014-06-06 05:42:15 +08:00
return "<Cell " + worksheet(d_->parent_).get_title() + "." + get_reference().to_string() + ">";
2014-05-21 22:20:30 +08:00
}
2014-06-11 05:12:15 +08:00
relationship cell::get_hyperlink() const
{
if(!d_->has_hyperlink_)
{
throw std::runtime_error("no hyperlink set");
}
return d_->hyperlink_;
}
bool cell::has_hyperlink() const
{
return d_->has_hyperlink_;
}
2014-05-21 22:20:30 +08:00
2014-06-11 05:12:15 +08:00
void cell::set_hyperlink(const std::string &hyperlink)
{
2014-06-06 05:42:15 +08:00
if(hyperlink.length() == 0 || std::find(hyperlink.begin(), hyperlink.end(), ':') == hyperlink.end())
2014-06-11 05:12:15 +08:00
{
throw data_type_exception();
}
d_->has_hyperlink_ = true;
2014-07-20 02:43:48 +08:00
d_->hyperlink_ = worksheet(d_->parent_).create_relationship(relationship::type::hyperlink, hyperlink);
2014-06-11 05:12:15 +08:00
2014-07-26 04:39:25 +08:00
if(get_value().is(value::type::null))
2014-06-11 05:12:15 +08:00
{
2014-07-26 04:39:25 +08:00
set_value(hyperlink);
2014-06-11 05:12:15 +08:00
}
}
void cell::set_formula(const std::string &formula)
{
2014-07-25 05:31:46 +08:00
if(formula.length() == 0)
{
throw data_type_exception();
}
2014-07-26 04:39:25 +08:00
d_->formula_ = formula;
2014-07-25 05:31:46 +08:00
}
bool cell::has_formula() const
{
2014-07-26 04:39:25 +08:00
return !d_->formula_.empty();
2014-07-25 05:31:46 +08:00
}
std::string cell::get_formula() const
{
2014-07-26 04:39:25 +08:00
if(d_->formula_.empty())
2014-06-11 05:12:15 +08:00
{
throw data_type_exception();
}
2014-07-26 04:39:25 +08:00
return d_->formula_;
2014-07-25 05:31:46 +08:00
}
void cell::clear_formula()
{
2014-07-26 04:39:25 +08:00
d_->formula_.clear();
2014-06-11 05:12:15 +08:00
}
2014-05-31 06:42:25 +08:00
void cell::set_comment(const xlnt::comment &c)
2014-07-20 02:43:48 +08:00
{
if(!has_comment())
{
get_parent().increment_comments();
}
d_->comment_ = c;
2014-07-20 02:43:48 +08:00
}
void cell::clear_comment()
{
if(has_comment())
2014-07-20 02:43:48 +08:00
{
get_parent().decrement_comments();
2014-07-20 02:43:48 +08:00
}
d_->comment_ = comment();
2014-07-20 02:43:48 +08:00
}
2014-07-25 05:31:46 +08:00
bool cell::has_comment() const
{
return d_->comment_.get_text() != "";
2014-07-25 05:31:46 +08:00
}
2014-06-11 05:12:15 +08:00
void cell::set_error(const std::string &error)
{
2014-06-06 05:42:15 +08:00
if(error.length() == 0 || error[0] != '#')
2014-06-11 05:12:15 +08:00
{
throw data_type_exception();
}
2014-07-26 04:39:25 +08:00
set_value(value::error(error));
2014-06-11 05:12:15 +08:00
}
2014-05-31 06:42:25 +08:00
2014-07-20 02:43:48 +08:00
cell cell::offset(column_t column, row_t row)
{
2014-07-26 04:39:25 +08:00
return get_parent().get_cell(cell_reference(d_->column_ + column, d_->row_ + row));
2014-07-20 02:43:48 +08:00
}
worksheet cell::get_parent()
{
return worksheet(d_->parent_);
}
2014-07-26 04:39:25 +08:00
const worksheet cell::get_parent() const
{
return worksheet(d_->parent_);
}
2014-07-20 02:43:48 +08:00
comment cell::get_comment() const
{
return d_->comment_;
2014-07-20 02:43:48 +08:00
}
2014-07-26 04:39:25 +08:00
std::pair<int, int> cell::get_anchor() const
{
static const double DefaultColumnWidth = 51.85;
static const double DefaultRowHeight = 15.0;
auto points_to_pixels = [](double value, double dpi) { return (int)std::ceil(value * dpi / 72); };
auto left_columns = d_->column_;
auto &column_dimensions = get_parent().get_column_dimensions();
int left_anchor = 0;
auto default_width = points_to_pixels(DefaultColumnWidth, 96.0);
for(int column_index = 1; column_index <= (int)left_columns; column_index++)
{
if(column_dimensions.find(column_index) != column_dimensions.end())
{
auto cdw = column_dimensions.at(column_index);
if(cdw > 0)
{
left_anchor += points_to_pixels(cdw, 96.0);
continue;
}
}
left_anchor += default_width;
}
auto top_rows = d_->row_;
auto &row_dimensions = get_parent().get_row_dimensions();
int top_anchor = 0;
auto default_height = points_to_pixels(DefaultRowHeight, 96.0);
for(int row_index = 1; row_index <= (int)top_rows; row_index++)
{
if(row_dimensions.find(row_index) != row_dimensions.end())
{
auto rdh = row_dimensions.at(row_index);
if(rdh > 0)
{
top_anchor += points_to_pixels(rdh, 96.0);
continue;
}
}
top_anchor += default_height;
}
return {left_anchor, top_anchor};
}
2014-05-31 06:42:25 +08:00
} // namespace xlnt