2014-05-31 06:42:25 +08:00
|
|
|
#pragma once
|
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/cell/cell.hpp>
|
|
|
|
#include <xlnt/cell/comment.hpp>
|
2015-11-21 09:41:32 +08:00
|
|
|
#include <xlnt/cell/index_types.hpp>
|
2015-11-11 07:58:54 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/utils/time.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/packaging/relationship.hpp>
|
2015-10-19 03:30:46 +08:00
|
|
|
#include <xlnt/styles/number_format.hpp>
|
2014-05-31 06:42:25 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
#include "comment_impl.hpp"
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// return s after checking encoding, size, and illegal characters
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string check_string(std::string s)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
if (s.size() == 0)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
// check encoding?
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
if (s.size() > 32767)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
s = s.substr(0, 32767); // max string length in Excel
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
for (char c : s)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
if (c >= 0 && (c <= 8 || c == 11 || c == 12 || (c >= 14 && c <= 31)))
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
throw xlnt::illegal_character_error(c);
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::pair<bool, long double> cast_numeric(const std::string &s)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
const char *str = s.c_str();
|
2015-10-14 04:35:22 +08:00
|
|
|
char *str_end = nullptr;
|
|
|
|
auto result = std::strtold(str, &str_end);
|
2015-11-11 07:58:54 +08:00
|
|
|
if (str_end != str + s.size()) return { false, 0 };
|
2015-11-01 22:43:01 +08:00
|
|
|
return { true, result };
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::pair<bool, long double> cast_percentage(const std::string &s)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
if (s.back() == '%')
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
auto number = cast_numeric(s.substr(0, s.size() - 1));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
if (number.first)
|
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
return { true, number.second / 100 };
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
return { false, 0 };
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::pair<bool, xlnt::time> cast_time(const std::string &s)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
xlnt::time result;
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto last_colon = s.find_last_of(':');
|
2015-11-11 07:58:54 +08:00
|
|
|
if (last_colon == std::string::npos) return { false, result };
|
|
|
|
double seconds = std::stod(s.substr(last_colon + 1));
|
2015-11-08 07:37:23 +08:00
|
|
|
result.second = static_cast<int>(seconds);
|
|
|
|
result.microsecond = static_cast<int>((seconds - static_cast<double>(result.second)) * 1e6);
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
auto first_colon = s.find_first_of(':');
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
if (first_colon == last_colon)
|
|
|
|
{
|
|
|
|
auto decimal_pos = s.find('.');
|
2015-11-11 07:58:54 +08:00
|
|
|
if (decimal_pos != std::string::npos)
|
2015-11-08 07:37:23 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
result.minute = std::stoi(s.substr(0, first_colon));
|
2015-11-08 07:37:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
result.hour = std::stoi(s.substr(0, first_colon));
|
2015-11-08 07:37:23 +08:00
|
|
|
result.minute = result.second;
|
|
|
|
result.second = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
result.hour = std::stoi(s.substr(0, first_colon));
|
|
|
|
result.minute = std::stoi(s.substr(first_colon + 1, last_colon - first_colon - 1));
|
2015-11-08 07:37:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument)
|
|
|
|
{
|
|
|
|
return { false, result };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { true, result };
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
|
|
|
class style;
|
|
|
|
|
|
|
|
namespace detail {
|
2014-06-06 05:42:15 +08:00
|
|
|
|
|
|
|
struct worksheet_impl;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
struct cell_impl
|
|
|
|
{
|
|
|
|
cell_impl();
|
2015-10-14 01:56:07 +08:00
|
|
|
cell_impl(column_t column, row_t row);
|
|
|
|
cell_impl(worksheet_impl *parent, column_t column, row_t row);
|
2014-06-13 05:04:37 +08:00
|
|
|
cell_impl(const cell_impl &rhs);
|
|
|
|
cell_impl &operator=(const cell_impl &rhs);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
cell self()
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
return xlnt::cell(this);
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void set_string(const std::string &s, bool guess_types)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
value_string_ = check_string(s);
|
|
|
|
type_ = cell::type::string;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
if (value_string_.size() > 1 && value_string_.front() == '=')
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
formula_ = value_string_;
|
|
|
|
type_ = cell::type::formula;
|
2015-11-11 07:58:54 +08:00
|
|
|
value_string_.clear();
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
else if (cell::error_codes().find(s) != cell::error_codes().end())
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
type_ = cell::type::error;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
else if (guess_types)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
|
|
|
auto percentage = cast_percentage(s);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
if (percentage.first)
|
|
|
|
{
|
|
|
|
value_numeric_ = percentage.second;
|
|
|
|
type_ = cell::type::numeric;
|
2015-10-24 02:42:36 +08:00
|
|
|
self().set_number_format(xlnt::number_format::percentage());
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto time = cast_time(s);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
if (time.first)
|
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
type_ = cell::type::numeric;
|
2015-10-24 02:42:36 +08:00
|
|
|
self().set_number_format(number_format::date_time6());
|
2015-10-19 03:30:46 +08:00
|
|
|
value_numeric_ = time.second.to_number();
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto numeric = cast_numeric(s);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 04:35:22 +08:00
|
|
|
if (numeric.first)
|
|
|
|
{
|
|
|
|
value_numeric_ = numeric.second;
|
|
|
|
type_ = cell::type::numeric;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
cell::type type_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-06-06 05:42:15 +08:00
|
|
|
worksheet_impl *parent_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-07-26 04:39:25 +08:00
|
|
|
column_t column_;
|
|
|
|
row_t row_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string value_string_;
|
2015-10-14 01:56:07 +08:00
|
|
|
long double value_numeric_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string formula_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-06-12 04:41:34 +08:00
|
|
|
bool has_hyperlink_;
|
2015-10-14 01:56:07 +08:00
|
|
|
relationship hyperlink_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
bool is_merged_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-21 01:53:47 +08:00
|
|
|
bool has_style_;
|
2015-10-19 03:30:46 +08:00
|
|
|
std::size_t style_id_;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
std::unique_ptr<comment_impl> comment_;
|
2014-05-31 06:42:25 +08:00
|
|
|
};
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|