2021-08-22 20:23:18 +08:00
|
|
|
// Copyright (c) 2014-2021 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
//
|
|
|
|
// 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
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2015-12-25 04:51:11 +08:00
|
|
|
// 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 <locale>
|
|
|
|
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/worksheet/range_reference.hpp>
|
2014-05-22 05:48:51 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference range_reference::make_absolute(const xlnt::range_reference &relative)
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference copy = relative;
|
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
copy.top_left_.make_absolute(true, true);
|
|
|
|
copy.bottom_right_.make_absolute(true, true);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2014-05-22 05:48:51 +08:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference::range_reference()
|
|
|
|
: range_reference("A1")
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference::range_reference(const char *range_string)
|
|
|
|
: range_reference(std::string(range_string))
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference::range_reference(const std::string &range_string)
|
|
|
|
: top_left_("A1"), bottom_right_("A1")
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2020-08-25 19:57:07 +08:00
|
|
|
auto colon_index = range_string.find(' ');
|
|
|
|
|
|
|
|
if (colon_index != std::string::npos)
|
|
|
|
{
|
|
|
|
// Multiple cell selection is not supported at this time.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
colon_index = range_string.find(':');
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
if (colon_index != std::string::npos)
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
top_left_ = cell_reference(range_string.substr(0, colon_index));
|
|
|
|
bottom_right_ = cell_reference(range_string.substr(colon_index + 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
top_left_ = cell_reference(range_string);
|
|
|
|
bottom_right_ = cell_reference(range_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 08:05:25 +08:00
|
|
|
range_reference::range_reference(const cell_reference &top_left,
|
|
|
|
const cell_reference &bottom_right)
|
|
|
|
: top_left_(top_left),
|
|
|
|
bottom_right_(bottom_right)
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-24 08:05:25 +08:00
|
|
|
range_reference::range_reference(column_t column_index_start,
|
|
|
|
row_t row_index_start,
|
|
|
|
column_t column_index_end,
|
|
|
|
row_t row_index_end)
|
|
|
|
: top_left_(column_index_start, row_index_start),
|
|
|
|
bottom_right_(column_index_end, row_index_end)
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-15 06:05:13 +08:00
|
|
|
range_reference range_reference::make_offset(int column_offset, int row_offset) const
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
auto top_left = top_left_.make_offset(column_offset, row_offset);
|
|
|
|
auto bottom_right = bottom_right_.make_offset(column_offset, row_offset);
|
|
|
|
|
|
|
|
return top_left, bottom_right; // lol
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::size_t range_reference::height() const
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2017-09-09 08:47:10 +08:00
|
|
|
return 1 + bottom_right_.row() - top_left_.row();
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::size_t range_reference::width() const
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2017-09-09 08:47:10 +08:00
|
|
|
return 1 + (bottom_right_.column() - top_left_.column()).index;
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool range_reference::is_single_cell() const
|
|
|
|
{
|
2017-09-09 08:47:10 +08:00
|
|
|
return width() == 1 && height() == 1;
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string range_reference::to_string() const
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
|
|
|
return top_left_.to_string() + ":" + bottom_right_.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool range_reference::operator==(const range_reference &comparand) const
|
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
return comparand.top_left_ == top_left_ && comparand.bottom_right_ == bottom_right_;
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
bool range_reference::operator!=(const range_reference &comparand) const
|
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
return comparand.top_left_ != top_left_ || comparand.bottom_right_ != bottom_right_;
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2015-11-21 09:41:32 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell_reference range_reference::top_left() const
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return top_left_;
|
|
|
|
}
|
2017-03-24 08:05:25 +08:00
|
|
|
|
|
|
|
cell_reference range_reference::top_right() const
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2017-03-24 08:05:25 +08:00
|
|
|
return cell_reference(bottom_right_.column(), top_left_.row());
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
2017-03-24 08:05:25 +08:00
|
|
|
|
|
|
|
cell_reference range_reference::bottom_left() const
|
|
|
|
{
|
|
|
|
return cell_reference(top_left_.column(), bottom_right_.row());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_reference range_reference::bottom_right() const
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return bottom_right_;
|
|
|
|
}
|
|
|
|
|
2022-08-14 02:20:25 +08:00
|
|
|
bool range_reference::contains(const cell_reference &ref) const
|
|
|
|
{
|
|
|
|
return top_left_.column_index() <= ref.column_index()
|
|
|
|
&& bottom_right_.column_index() >= ref.column_index()
|
|
|
|
&& top_left_.row() <= ref.row()
|
|
|
|
&& bottom_right_.row() >= ref.row();
|
|
|
|
}
|
|
|
|
|
2015-11-21 09:41:32 +08:00
|
|
|
bool range_reference::operator==(const std::string &reference_string) const
|
|
|
|
{
|
|
|
|
return *this == range_reference(reference_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool range_reference::operator==(const char *reference_string) const
|
|
|
|
{
|
|
|
|
return *this == std::string(reference_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool range_reference::operator!=(const std::string &reference_string) const
|
|
|
|
{
|
|
|
|
return *this != range_reference(reference_string);
|
2014-06-06 04:19:31 +08:00
|
|
|
}
|
2015-11-21 09:41:32 +08:00
|
|
|
|
|
|
|
bool range_reference::operator!=(const char *reference_string) const
|
|
|
|
{
|
|
|
|
return *this != std::string(reference_string);
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
XLNT_API bool operator==(const std::string &reference_string, const range_reference &ref)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return ref == reference_string;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
XLNT_API bool operator==(const char *reference_string, const range_reference &ref)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return ref == reference_string;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
XLNT_API bool operator!=(const std::string &reference_string, const range_reference &ref)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return ref != reference_string;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
XLNT_API bool operator!=(const char *reference_string, const range_reference &ref)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
return ref != reference_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xlnt
|