2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2014-05-22 05:48:51 +08:00
|
|
|
#include <algorithm>
|
2016-06-11 22:09:29 +08:00
|
|
|
#include <cmath>
|
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>
|
2016-05-12 07:24:53 +08:00
|
|
|
#include <xlnt/cell/text.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/packaging/relationship.hpp>
|
|
|
|
#include <xlnt/styles/color.hpp>
|
2016-06-11 01:40:50 +08:00
|
|
|
#include <xlnt/styles/format.hpp>
|
|
|
|
#include <xlnt/styles/style.hpp>
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/utils/date.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/utils/datetime.hpp>
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/utils/time.hpp>
|
|
|
|
#include <xlnt/utils/timedelta.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/workbook/workbook.hpp>
|
2015-11-01 22:43:01 +08:00
|
|
|
#include <xlnt/worksheet/column_properties.hpp>
|
|
|
|
#include <xlnt/worksheet/row_properties.hpp>
|
2015-10-19 03:30:46 +08:00
|
|
|
#include <xlnt/worksheet/worksheet.hpp>
|
2014-08-14 06:56:34 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
#include <detail/cell_impl.hpp>
|
|
|
|
#include <detail/comment_impl.hpp>
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::pair<bool, long double> cast_numeric(const std::string &s)
|
|
|
|
{
|
|
|
|
const char *str = s.c_str();
|
|
|
|
char *str_end = nullptr;
|
|
|
|
auto result = std::strtold(str, &str_end);
|
|
|
|
if (str_end != str + s.size()) return{ false, 0 };
|
|
|
|
return{ true, result };
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<bool, long double> cast_percentage(const std::string &s)
|
|
|
|
{
|
|
|
|
if (s.back() == '%')
|
|
|
|
{
|
|
|
|
auto number = cast_numeric(s.substr(0, s.size() - 1));
|
|
|
|
|
|
|
|
if (number.first)
|
|
|
|
{
|
|
|
|
return{ true, number.second / 100 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return{ false, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<bool, xlnt::time> cast_time(const std::string &s)
|
|
|
|
{
|
|
|
|
xlnt::time result;
|
|
|
|
|
2016-08-06 22:40:17 +08:00
|
|
|
std::vector<std::string> time_components;
|
|
|
|
std::size_t prev = 0;
|
|
|
|
auto colon_index = s.find(':');
|
|
|
|
|
|
|
|
while (colon_index != std::string::npos)
|
|
|
|
{
|
|
|
|
time_components.push_back(s.substr(prev, colon_index - prev));
|
|
|
|
prev = colon_index + 1;
|
|
|
|
colon_index = s.find(':', colon_index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_components.push_back(s.substr(prev, colon_index - prev));
|
|
|
|
|
|
|
|
if (time_components.size() < 2 || time_components.size() > 3)
|
2016-05-12 07:24:53 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
return{ false, result };
|
|
|
|
}
|
2016-05-12 07:24:53 +08:00
|
|
|
|
2016-08-06 22:40:17 +08:00
|
|
|
std::vector<double> numeric_components;
|
2016-05-12 07:24:53 +08:00
|
|
|
|
2016-08-06 22:40:17 +08:00
|
|
|
for (auto component : time_components)
|
|
|
|
{
|
|
|
|
if (component.empty() || (component.substr(0, component.find('.')).size() > 2))
|
2016-05-12 07:24:53 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
return{ false, result };
|
2016-05-12 07:24:53 +08:00
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
|
|
|
|
for (auto d : component)
|
2016-05-12 07:24:53 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
if (!(d >= '0' && d <= '9') && d != '.')
|
|
|
|
{
|
|
|
|
return{ false, result };
|
|
|
|
}
|
2016-05-12 07:24:53 +08:00
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
|
|
|
|
auto without_leading_zero = component.front() == '0' ? component.substr(1) : component;
|
|
|
|
auto numeric = std::stod(without_leading_zero);
|
|
|
|
|
|
|
|
numeric_components.push_back(numeric);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.hour = static_cast<int>(numeric_components[0]);
|
|
|
|
result.minute = static_cast<int>(numeric_components[1]);
|
|
|
|
|
|
|
|
if (result.minute != numeric_components[1])
|
|
|
|
{
|
|
|
|
result.minute = result.hour;
|
|
|
|
result.hour = 0;
|
|
|
|
result.second = static_cast<int>(numeric_components[1]);
|
|
|
|
result.microsecond = static_cast<int>((numeric_components[1] - result.second) * 1E6);
|
2016-05-12 07:24:53 +08:00
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
else if (numeric_components.size() > 2)
|
2016-05-12 07:24:53 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
result.second = static_cast<int>(numeric_components[2]);
|
|
|
|
result.microsecond = static_cast<int>((numeric_components[2] - result.second) * 1E6);
|
2016-05-12 07:24:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return{ true, result };
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-07-26 04:39:25 +08:00
|
|
|
namespace xlnt {
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
const std::unordered_map<std::string, int> &cell::error_codes()
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
static const std::unordered_map<std::string, int> *codes =
|
|
|
|
new std::unordered_map<std::string, int>({ { "#NULL!", 0 }, { "#DIV/0!", 1 }, { "#VALUE!", 2 },
|
2015-11-01 22:43:01 +08:00
|
|
|
{ "#REF!", 3 }, { "#NAME?", 4 }, { "#NUM!", 5 },
|
2015-11-03 05:45:05 +08:00
|
|
|
{ "#N/A!", 6 } });
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
return *codes;
|
2014-07-26 04:39:25 +08:00
|
|
|
};
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2015-11-23 01:41:27 +08:00
|
|
|
std::string cell::check_string(const std::string &to_check)
|
|
|
|
{
|
|
|
|
// so we can modify it
|
|
|
|
std::string s = to_check;
|
|
|
|
|
|
|
|
if (s.size() == 0)
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
2016-07-18 01:49:59 +08:00
|
|
|
else if (s.size() > 32767)
|
2015-11-23 01:41:27 +08:00
|
|
|
{
|
|
|
|
s = s.substr(0, 32767); // max string length in Excel
|
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : s)
|
|
|
|
{
|
|
|
|
if (c >= 0 && (c <= 8 || c == 11 || c == 12 || (c >= 14 && c <= 31)))
|
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw xlnt::illegal_character(c);
|
2015-11-23 01:41:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool cell::garbage_collectible() const
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return !(get_data_type() != type::null || is_merged() || has_comment() || has_formula() || has_format());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(bool b)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = b ? 1 : 0;
|
|
|
|
d_->type_ = type::boolean;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::int8_t i)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::int16_t i)
|
2014-05-31 06:42:25 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2014-05-31 06:42:25 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::int32_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::int64_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::uint8_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::uint16_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::uint32_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::uint64_t i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 09:47:07 +08:00
|
|
|
#ifdef _MSC_VER
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(unsigned long i)
|
2015-10-02 06:14:42 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 06:14:42 +08:00
|
|
|
}
|
2015-11-11 07:58:54 +08:00
|
|
|
#endif
|
2015-10-02 06:14:42 +08:00
|
|
|
|
2015-10-17 07:46:21 +08:00
|
|
|
#ifdef __linux
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(long long i)
|
2015-10-02 13:57:39 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 13:57:39 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(unsigned long long i)
|
2015-10-02 13:57:39 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(i);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-02 13:57:39 +08:00
|
|
|
}
|
2015-11-11 07:58:54 +08:00
|
|
|
#endif
|
2015-10-02 13:57:39 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(float f)
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(f);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(double d)
|
2014-05-31 06:42:25 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(d);
|
|
|
|
d_->type_ = type::numeric;
|
2014-05-31 06:42:25 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(long double d)
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->value_numeric_ = static_cast<long double>(d);
|
|
|
|
d_->type_ = type::numeric;
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2014-05-31 06:42:25 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(std::string s)
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
s = check_string(s);
|
|
|
|
|
|
|
|
if (s.size() > 1 && s.front() == '=')
|
|
|
|
{
|
|
|
|
d_->type_ = type::formula;
|
|
|
|
set_formula(s);
|
|
|
|
}
|
|
|
|
else if (cell::error_codes().find(s) != cell::error_codes().end())
|
|
|
|
{
|
|
|
|
set_error(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 03:19:08 +08:00
|
|
|
d_->type_ = type::string;
|
|
|
|
d_->value_text_.set_plain_string(s);
|
|
|
|
|
|
|
|
if (s.size() > 0)
|
|
|
|
{
|
|
|
|
get_workbook().add_shared_string(d_->value_text_);
|
|
|
|
}
|
2016-05-12 07:24:53 +08:00
|
|
|
}
|
2015-11-03 03:22:13 +08:00
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
if (get_workbook().get_guess_types())
|
2015-11-03 03:22:13 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
guess_type_and_set_value(s);
|
2015-11-03 03:22:13 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-15 03:19:08 +08:00
|
|
|
template <>
|
|
|
|
XLNT_FUNCTION void cell::set_value(text t)
|
|
|
|
{
|
|
|
|
if (t.get_runs().size() == 1 && !t.get_runs().front().has_formatting())
|
|
|
|
{
|
|
|
|
set_value(t.get_plain_string());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d_->type_ = type::string;
|
|
|
|
d_->value_text_ = t;
|
|
|
|
get_workbook().add_shared_string(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(char const *c)
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
set_value(std::string(c));
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-10-14 04:35:22 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(cell c)
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2015-10-15 06:05:13 +08:00
|
|
|
d_->type_ = c.d_->type_;
|
|
|
|
d_->value_numeric_ = c.d_->value_numeric_;
|
2016-05-12 07:24:53 +08:00
|
|
|
d_->value_text_ = c.d_->value_text_;
|
2015-10-15 06:05:13 +08:00
|
|
|
d_->hyperlink_ = c.d_->hyperlink_;
|
|
|
|
d_->has_hyperlink_ = c.d_->has_hyperlink_;
|
|
|
|
d_->formula_ = c.d_->formula_;
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->format_id_ = c.d_->format_id_;
|
2016-06-20 02:43:41 +08:00
|
|
|
if (c.has_comment()) set_comment(c.get_comment());
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(date d)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
d_->type_ = type::numeric;
|
|
|
|
d_->value_numeric_ = d.to_number(get_base_date());
|
2015-10-24 02:42:36 +08:00
|
|
|
set_number_format(number_format::date_yyyymmdd2());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(datetime d)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
d_->type_ = type::numeric;
|
|
|
|
d_->value_numeric_ = d.to_number(get_base_date());
|
2015-10-24 02:42:36 +08:00
|
|
|
set_number_format(number_format::date_datetime());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(time t)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
d_->type_ = type::numeric;
|
|
|
|
d_->value_numeric_ = t.to_number();
|
2015-10-24 02:42:36 +08:00
|
|
|
set_number_format(number_format::date_time6());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION void cell::set_value(timedelta t)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
d_->type_ = type::numeric;
|
|
|
|
d_->value_numeric_ = t.to_number();
|
2016-07-22 08:15:53 +08:00
|
|
|
set_number_format(number_format("[hh]:mm:ss"));
|
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
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
return d_->row_;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 03:53:48 +08:00
|
|
|
column_t cell::get_column() const
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-11-04 03:53:48 +08:00
|
|
|
return d_->column_;
|
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
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->is_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
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
return d_->is_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
|
|
|
{
|
2015-11-21 09:41:32 +08:00
|
|
|
return get_data_type() == type::numeric && get_number_format().is_date_format();
|
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
|
|
|
{
|
2015-11-01 22:43:01 +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
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
return d_ == comparand.d_;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-07-26 04:39:25 +08:00
|
|
|
cell &cell::operator=(const cell &rhs)
|
2014-05-31 06:42:25 +08:00
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
*d_ = *rhs.d_;
|
2014-05-21 22:20:30 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string cell::to_repr() const
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
std::string cell::get_hyperlink() const
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (!d_->has_hyperlink_)
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("no hyperlink set");
|
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
return d_->hyperlink_;
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cell::has_hyperlink() const
|
|
|
|
{
|
|
|
|
return d_->has_hyperlink_;
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void cell::set_hyperlink(const std::string &hyperlink)
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
if (hyperlink.length() == 0 || std::find(hyperlink.begin(), hyperlink.end(), ':') == hyperlink.end())
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_data_type();
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
d_->has_hyperlink_ = true;
|
2016-08-05 13:52:05 +08:00
|
|
|
d_->hyperlink_ = hyperlink;
|
2014-06-11 05:12:15 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
if (get_data_type() == 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void cell::set_formula(const std::string &formula)
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
if (formula.empty())
|
2014-07-25 05:31:46 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
throw invalid_parameter();
|
2014-07-25 05:31:46 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (formula[0] == '=')
|
2015-10-26 12:26:48 +08:00
|
|
|
{
|
|
|
|
d_->formula_ = formula.substr(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string cell::get_formula() const
|
2014-07-25 05:31:46 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (d_->formula_.empty())
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2016-08-06 22:40:17 +08:00
|
|
|
throw invalid_attribute();
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-07-27 04:19:15 +08:00
|
|
|
void cell::set_comment(const xlnt::comment &c)
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (c.d_ != d_->comment_.get())
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw xlnt::invalid_attribute();
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (!has_comment())
|
2014-07-21 21:34:57 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
get_worksheet().increment_comments();
|
2014-07-21 21:34:57 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
*get_comment().d_ = *c.d_;
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cell::clear_comment()
|
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (has_comment())
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
get_worksheet().decrement_comments();
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->comment_ = nullptr;
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
|
|
|
|
2014-07-25 05:31:46 +08:00
|
|
|
bool cell::has_comment() const
|
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
return d_->comment_ != nullptr;
|
2014-07-25 05:31:46 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void cell::set_error(const std::string &error)
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (error.length() == 0 || error[0] != '#')
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_data_type();
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
d_->value_text_.set_plain_string(error);
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->type_ = type::error;
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
2014-05-31 06:42:25 +08:00
|
|
|
|
2015-11-04 03:53:48 +08:00
|
|
|
cell cell::offset(int column, int row)
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
return get_worksheet().get_cell(cell_reference(d_->column_ + column, d_->row_ + row));
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
worksheet cell::get_worksheet()
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
|
|
|
return worksheet(d_->parent_);
|
|
|
|
}
|
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
const worksheet cell::get_worksheet() const
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
|
|
|
return worksheet(d_->parent_);
|
|
|
|
}
|
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
workbook &cell::get_workbook()
|
|
|
|
{
|
|
|
|
return get_worksheet().get_workbook();
|
|
|
|
}
|
|
|
|
|
|
|
|
const workbook &cell::get_workbook() const
|
|
|
|
{
|
|
|
|
return get_worksheet().get_workbook();
|
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
comment cell::get_comment()
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (d_->comment_ == nullptr)
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
d_->comment_.reset(new detail::comment_impl());
|
2016-05-12 07:24:53 +08:00
|
|
|
get_worksheet().increment_comments();
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
return comment(d_->comment_.get());
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
//TODO: this shares a lot of code with worksheet::get_point_pos, try to reduce repition
|
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;
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
auto points_to_pixels = [](long double value, long double dpi)
|
|
|
|
{
|
|
|
|
return static_cast<int>(std::ceil(value * dpi / 72));
|
|
|
|
};
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
auto left_columns = d_->column_ - 1;
|
2014-07-26 04:39:25 +08:00
|
|
|
int left_anchor = 0;
|
|
|
|
auto default_width = points_to_pixels(DefaultColumnWidth, 96.0);
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
for (column_t column_index = 1; column_index <= left_columns; column_index++)
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
if (get_worksheet().has_column_properties(column_index))
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
auto cdw = get_worksheet().get_column_properties(column_index).width;
|
2014-07-26 04:39:25 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
if (cdw > 0)
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
|
|
|
left_anchor += points_to_pixels(cdw, 96.0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left_anchor += default_width;
|
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
auto top_rows = d_->row_ - 1;
|
2014-07-26 04:39:25 +08:00
|
|
|
int top_anchor = 0;
|
|
|
|
auto default_height = points_to_pixels(DefaultRowHeight, 96.0);
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
for (row_t row_index = 1; row_index <= top_rows; row_index++)
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
if (get_worksheet().has_row_properties(row_index))
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
auto rdh = get_worksheet().get_row_properties(row_index).height;
|
2014-07-26 04:39:25 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
if (rdh > 0)
|
2014-07-26 04:39:25 +08:00
|
|
|
{
|
|
|
|
top_anchor += points_to_pixels(rdh, 96.0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
top_anchor += default_height;
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
return { left_anchor, top_anchor };
|
2014-07-26 04:39:25 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
cell::type cell::get_data_type() const
|
|
|
|
{
|
|
|
|
return d_->type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell::set_data_type(type t)
|
|
|
|
{
|
|
|
|
d_->type_ = t;
|
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const number_format &cell::get_number_format() const
|
2015-10-14 04:35:22 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2015-10-21 01:53:47 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_number_format();
|
2015-10-21 01:53:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_number_format();
|
2015-10-21 01:53:47 +08:00
|
|
|
}
|
2015-10-14 04:35:22 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const font &cell::get_font() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2015-11-23 01:41:27 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_font();
|
2015-11-23 01:41:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_font();
|
2015-11-23 01:41:27 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
const fill &cell::get_fill() const
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2016-05-01 23:08:56 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_fill();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_fill();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const border &cell::get_border() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2016-05-01 23:08:56 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_border();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_border();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const alignment &cell::get_alignment() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2016-05-01 23:08:56 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_alignment();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_alignment();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const protection &cell::get_protection() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
if (d_->has_format_)
|
2016-05-01 23:08:56 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_).get_protection();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(0).get_protection();
|
2016-05-01 23:08:56 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
void cell::clear_value()
|
|
|
|
{
|
|
|
|
d_->value_numeric_ = 0;
|
2016-05-12 07:24:53 +08:00
|
|
|
d_->value_text_.clear();
|
2015-10-14 01:56:07 +08:00
|
|
|
d_->formula_.clear();
|
|
|
|
d_->type_ = cell::type::null;
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION bool cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return d_->value_numeric_ != 0;
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::int8_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::int8_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::int16_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::int16_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::int32_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::int32_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::int64_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::int64_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::uint8_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::uint8_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::uint16_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::uint16_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::uint32_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::uint32_t>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::uint64_t cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<std::uint64_t>(d_->value_numeric_);
|
|
|
|
}
|
2015-10-17 07:46:21 +08:00
|
|
|
|
|
|
|
#ifdef __linux
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2016-07-24 07:43:24 +08:00
|
|
|
XLNT_FUNCTION long long cell::get_value() const
|
2015-10-17 07:46:21 +08:00
|
|
|
{
|
2016-07-24 07:43:24 +08:00
|
|
|
return static_cast<long long>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
XLNT_FUNCTION unsigned long long cell::get_value() const
|
|
|
|
{
|
|
|
|
return static_cast<unsigned long long>(d_->value_numeric_);
|
2015-10-17 07:46:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION float cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<float>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION double cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return static_cast<double>(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION long double cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return d_->value_numeric_;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION time cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
|
|
|
return time::from_number(d_->value_numeric_);
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION datetime cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
return datetime::from_number(d_->value_numeric_, get_base_date());
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION date cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
return date::from_number(static_cast<int>(d_->value_numeric_), get_base_date());
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION timedelta cell::get_value() const
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
return timedelta::from_number(d_->value_numeric_);
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-10-14 02:19:46 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void cell::set_border(const xlnt::border &border_)
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
|
|
|
auto format_copy = get_workbook().get_format(d_->format_id_);
|
|
|
|
format_copy.set_border(border_);
|
|
|
|
d_->format_id_ = get_workbook().add_format(format_copy);
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cell::set_fill(const xlnt::fill &fill_)
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
|
|
|
auto format_copy = get_workbook().get_format(d_->format_id_);
|
|
|
|
format_copy.set_fill(fill_);
|
|
|
|
d_->format_id_ = get_workbook().add_format(format_copy);
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
2015-11-23 01:41:27 +08:00
|
|
|
void cell::set_font(const font &font_)
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
|
|
|
auto format_copy = get_workbook().get_format(d_->format_id_);
|
|
|
|
format_copy.set_font(font_);
|
|
|
|
d_->format_id_ = get_workbook().add_format(format_copy);
|
2015-11-23 01:41:27 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
void cell::set_number_format(const number_format &number_format_)
|
2015-10-14 02:19:46 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
format new_format;
|
|
|
|
|
|
|
|
if (d_->has_format_)
|
|
|
|
{
|
|
|
|
new_format = get_workbook().get_format(d_->format_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto number_format_with_id = number_format_;
|
|
|
|
|
|
|
|
if (!number_format_with_id.has_id())
|
|
|
|
{
|
|
|
|
number_format_with_id.set_id(get_worksheet().next_custom_number_format_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
new_format.set_number_format(number_format_with_id);
|
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
2016-06-11 01:40:50 +08:00
|
|
|
d_->format_id_ = get_workbook().add_format(new_format);
|
2015-10-14 02:19:46 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void cell::set_alignment(const xlnt::alignment &alignment_)
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
|
|
|
auto format_copy = get_workbook().get_format(d_->format_id_);
|
|
|
|
format_copy.set_alignment(alignment_);
|
|
|
|
d_->format_id_ = get_workbook().add_format(format_copy);
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cell::set_protection(const xlnt::protection &protection_)
|
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->has_format_ = true;
|
|
|
|
auto format_copy = get_workbook().get_format(d_->format_id_);
|
|
|
|
format_copy.set_protection(protection_);
|
|
|
|
d_->format_id_ = get_workbook().add_format(format_copy);
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
template <>
|
2015-11-11 09:47:07 +08:00
|
|
|
XLNT_FUNCTION std::string cell::get_value() const
|
2015-10-14 02:19:46 +08:00
|
|
|
{
|
2016-05-12 07:24:53 +08:00
|
|
|
return d_->value_text_.get_plain_string();
|
2015-10-14 02:19:46 +08:00
|
|
|
}
|
|
|
|
|
2016-05-15 03:19:08 +08:00
|
|
|
template <>
|
|
|
|
XLNT_FUNCTION text cell::get_value() const
|
|
|
|
{
|
|
|
|
return d_->value_text_;
|
|
|
|
}
|
|
|
|
|
2015-10-14 02:19:46 +08:00
|
|
|
bool cell::has_value() const
|
|
|
|
{
|
|
|
|
return d_->type_ != cell::type::null;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string cell::to_string() const
|
2015-10-14 02:19:46 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
auto nf = get_number_format();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
switch (get_data_type())
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
case cell::type::null:
|
|
|
|
return "";
|
|
|
|
case cell::type::numeric:
|
2015-11-21 09:41:32 +08:00
|
|
|
return get_number_format().format(get_value<long double>(), get_base_date());
|
2015-11-01 22:43:01 +08:00
|
|
|
case cell::type::string:
|
|
|
|
case cell::type::formula:
|
|
|
|
case cell::type::error:
|
2015-11-21 09:41:32 +08:00
|
|
|
return get_number_format().format(get_value<std::string>());
|
2015-11-01 22:43:01 +08:00
|
|
|
case cell::type::boolean:
|
|
|
|
return get_value<long double>() == 0 ? "FALSE" : "TRUE";
|
|
|
|
default:
|
|
|
|
return "";
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2015-10-14 02:19:46 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
format &cell::get_format()
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return get_workbook().get_format(d_->format_id_);
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
2015-10-24 02:42:36 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
bool cell::has_format() const
|
2015-10-24 02:42:36 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return d_->has_format_;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
void cell::set_format(const format &new_format)
|
2015-10-24 02:42:36 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
d_->format_id_ = get_workbook().add_format(new_format);
|
|
|
|
d_->has_format_ = true;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
void cell::set_format_id(std::size_t format_id)
|
|
|
|
{
|
|
|
|
d_->format_id_ = format_id;
|
|
|
|
d_->has_format_ = true;
|
|
|
|
}
|
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
calendar cell::get_base_date() const
|
|
|
|
{
|
2016-08-03 12:12:18 +08:00
|
|
|
return get_workbook().get_base_date();
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
|
|
|
|
{
|
|
|
|
return stream << cell.to_string();
|
|
|
|
}
|
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
std::size_t cell::get_format_id() const
|
2016-03-14 11:46:01 +08:00
|
|
|
{
|
2016-05-15 01:57:07 +08:00
|
|
|
return d_->format_id_;
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 07:24:53 +08:00
|
|
|
void cell::guess_type_and_set_value(const std::string &value)
|
|
|
|
{
|
|
|
|
auto percentage = cast_percentage(value);
|
|
|
|
|
|
|
|
if (percentage.first)
|
|
|
|
{
|
|
|
|
d_->value_numeric_ = percentage.second;
|
|
|
|
d_->type_ = cell::type::numeric;
|
|
|
|
set_number_format(xlnt::number_format::percentage());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto time = cast_time(value);
|
|
|
|
|
|
|
|
if (time.first)
|
|
|
|
{
|
|
|
|
d_->type_ = cell::type::numeric;
|
|
|
|
set_number_format(number_format::date_time6());
|
|
|
|
d_->value_numeric_ = time.second.to_number();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto numeric = cast_numeric(value);
|
|
|
|
|
|
|
|
if (numeric.first)
|
|
|
|
{
|
|
|
|
d_->value_numeric_ = numeric.second;
|
|
|
|
d_->type_ = cell::type::numeric;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
void cell::clear_format()
|
|
|
|
{
|
|
|
|
d_->format_id_ = 0;
|
|
|
|
d_->has_format_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell::clear_style()
|
|
|
|
{
|
|
|
|
d_->style_id_ = 0;
|
|
|
|
d_->has_style_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell::set_style(const style &new_style)
|
|
|
|
{
|
|
|
|
d_->has_style_ = true;
|
|
|
|
|
|
|
|
if (get_workbook().has_style(new_style.get_name()))
|
|
|
|
{
|
|
|
|
d_->style_id_ = get_workbook().get_style_id(new_style.get_name());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d_->style_id_ = get_workbook().add_style(new_style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell::set_style(const std::string &style_name)
|
|
|
|
{
|
|
|
|
d_->has_style_ = true;
|
|
|
|
|
|
|
|
if (!get_workbook().has_style(style_name))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("style " + style_name + " doesn't exist in workbook");
|
|
|
|
}
|
|
|
|
|
|
|
|
d_->style_id_ = get_workbook().get_style_id(style_name);
|
|
|
|
}
|
|
|
|
|
2016-06-20 02:43:41 +08:00
|
|
|
const style &cell::get_style() const
|
|
|
|
{
|
|
|
|
if (!d_->has_style_)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("cell has no style");
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_workbook().get_style_by_id(d_->style_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell::has_style() const
|
|
|
|
{
|
|
|
|
return d_->has_style_;
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
} // namespace xlnt
|