2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (c) 2014-2020 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
|
2016-12-04 20:29:10 +08:00
|
|
|
|
2017-08-26 23:49:48 +08:00
|
|
|
#include <cctype>
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/cell/cell_reference.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/worksheet/range_reference.hpp>
|
|
|
|
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <detail/constants.hpp>
|
2014-05-21 22:20:30 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
std::size_t cell_reference_hash::operator()(const cell_reference &k) const
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
return k.row() * constants::max_column().index + k.column_index();
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
cell_reference &cell_reference::make_absolute(bool absolute_column, bool absolute_row)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-11-03 22:51:23 +08:00
|
|
|
column_absolute(absolute_column);
|
|
|
|
row_absolute(absolute_row);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
return *this;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
cell_reference::cell_reference()
|
|
|
|
: cell_reference(1, 1)
|
2014-05-30 08:52:14 +08:00
|
|
|
{
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
cell_reference::cell_reference(const std::string &string)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-11-03 22:51:23 +08:00
|
|
|
auto split = split_reference(string, absolute_column_, absolute_row_);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
column(split.first);
|
|
|
|
row(split.second);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
cell_reference::cell_reference(const char *reference_string)
|
2015-11-11 07:58:54 +08:00
|
|
|
: cell_reference(std::string(reference_string))
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
cell_reference::cell_reference(column_t column_index, row_t row)
|
2015-11-04 03:53:48 +08:00
|
|
|
: column_(column_index), row_(row), absolute_row_(false), absolute_column_(false)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
if (row_ == 0
|
2019-12-27 01:03:12 +08:00
|
|
|
|| column_ == 0
|
|
|
|
|| !(row_ <= constants::max_row())
|
|
|
|
|| !(column_ <= constants::max_column()))
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_cell_reference(column_, row_);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
range_reference cell_reference::operator,(const xlnt::cell_reference &other) const
|
2014-07-27 04:19:15 +08:00
|
|
|
{
|
|
|
|
return range_reference(*this, other);
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string cell_reference::to_string() const
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string string_representation;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
if (absolute_column_)
|
|
|
|
{
|
|
|
|
string_representation.append("$");
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-04 03:53:48 +08:00
|
|
|
string_representation.append(column_.column_string());
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
if (absolute_row_)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2015-11-03 22:51:23 +08:00
|
|
|
string_representation.append("$");
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
string_representation.append(std::to_string(row_));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 22:51:23 +08:00
|
|
|
return string_representation;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 05:48:51 +08:00
|
|
|
range_reference cell_reference::to_range() const
|
|
|
|
{
|
2015-10-14 01:56:07 +08:00
|
|
|
return range_reference(column_, row_, column_, row_);
|
2014-05-22 05:48:51 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
std::pair<std::string, row_t> cell_reference::split_reference(const std::string &reference_string)
|
|
|
|
{
|
|
|
|
bool ignore1, ignore2;
|
|
|
|
return split_reference(reference_string, ignore1, ignore2);
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
std::pair<std::string, row_t> cell_reference::split_reference(
|
|
|
|
const std::string &reference_string, bool &absolute_column, bool &absolute_row)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
absolute_column = false;
|
|
|
|
absolute_row = false;
|
2015-11-11 07:58:54 +08:00
|
|
|
|
|
|
|
// Convert a coordinate string like 'B12' to a tuple ('B', 12)
|
|
|
|
bool column_part = true;
|
|
|
|
|
|
|
|
std::string column_string;
|
|
|
|
|
|
|
|
for (auto character : reference_string)
|
2015-11-08 07:37:23 +08:00
|
|
|
{
|
2017-09-09 01:57:29 +08:00
|
|
|
auto upper = static_cast<char>(std::toupper(static_cast<std::uint8_t>(character)));
|
2015-11-11 07:58:54 +08:00
|
|
|
|
2017-08-26 23:49:48 +08:00
|
|
|
if (std::isalpha(character))
|
2015-11-11 07:58:54 +08:00
|
|
|
{
|
|
|
|
if (column_part)
|
|
|
|
{
|
|
|
|
column_string.append(1, upper);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_cell_reference(reference_string);
|
2015-11-11 07:58:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (character == '$')
|
|
|
|
{
|
|
|
|
if (column_part)
|
|
|
|
{
|
|
|
|
if (column_string.empty())
|
|
|
|
{
|
|
|
|
column_string.append(1, upper);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
column_part = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (column_part)
|
|
|
|
{
|
|
|
|
column_part = false;
|
|
|
|
}
|
2017-08-26 23:49:48 +08:00
|
|
|
else if (!std::isdigit(character))
|
2015-11-11 07:58:54 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_cell_reference(reference_string);
|
2015-11-11 07:58:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2015-11-11 07:58:54 +08:00
|
|
|
|
|
|
|
std::string row_string = reference_string.substr(column_string.length());
|
|
|
|
|
|
|
|
if (row_string.length() == 0)
|
2014-06-06 05:42:15 +08:00
|
|
|
{
|
2016-07-30 06:55:49 +08:00
|
|
|
throw invalid_cell_reference(reference_string);
|
2014-06-06 05:42:15 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
if (column_string[0] == '$')
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2020-03-21 03:38:27 +08:00
|
|
|
absolute_column = true;
|
2015-11-11 07:58:54 +08:00
|
|
|
column_string = column_string.substr(1);
|
2015-11-08 07:37:23 +08:00
|
|
|
}
|
2015-11-11 07:58:54 +08:00
|
|
|
|
|
|
|
if (row_string[0] == '$')
|
2015-11-08 07:37:23 +08:00
|
|
|
{
|
2020-03-21 03:38:27 +08:00
|
|
|
absolute_row = true;
|
2015-11-11 07:58:54 +08:00
|
|
|
row_string = row_string.substr(1);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return {column_string, std::stoi(row_string)};
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
bool cell_reference::column_absolute() const
|
|
|
|
{
|
|
|
|
return absolute_column_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell_reference::column_absolute(bool absolute_column)
|
|
|
|
{
|
|
|
|
absolute_column_ = absolute_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::row_absolute() const
|
|
|
|
{
|
|
|
|
return absolute_row_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cell_reference::row_absolute(bool absolute_row)
|
|
|
|
{
|
|
|
|
absolute_row_ = absolute_row;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
column_t cell_reference::column() const
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
return column_;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void cell_reference::column(const std::string &column_string)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
column_ = column_t(column_string);
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
column_t::index_t cell_reference::column_index() const
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
return column_.index;
|
2015-11-20 11:54:54 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void cell_reference::column_index(column_t column)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
column_ = column;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
row_t cell_reference::row() const
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
return row_;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void cell_reference::row(row_t row)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
|
|
|
row_ = row;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator==(const std::string &reference_string) const
|
|
|
|
{
|
|
|
|
return *this == cell_reference(reference_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator==(const char *reference_string) const
|
|
|
|
{
|
|
|
|
return *this == std::string(reference_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator!=(const cell_reference &comparand) const
|
|
|
|
{
|
|
|
|
return !(*this == comparand);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator!=(const std::string &reference_string) const
|
|
|
|
{
|
|
|
|
return *this != cell_reference(reference_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator!=(const char *reference_string) const
|
|
|
|
{
|
|
|
|
return *this != std::string(reference_string);
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
cell_reference cell_reference::make_offset(int column_offset, int row_offset) const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
// TODO: check for overflow/underflow
|
2016-11-01 08:48:43 +08:00
|
|
|
auto relative_column = static_cast<column_t::index_t>(static_cast<int>(column_.index) + column_offset);
|
|
|
|
auto relative_row = static_cast<row_t>(static_cast<int>(row_) + row_offset);
|
2017-09-13 20:48:22 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return cell_reference(relative_column, relative_row);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cell_reference::operator==(const cell_reference &comparand) const
|
|
|
|
{
|
2017-09-13 20:48:22 +08:00
|
|
|
return comparand.column_ == column_
|
|
|
|
&& comparand.row_ == row_
|
|
|
|
&& absolute_column_ == comparand.absolute_column_
|
2016-12-24 23:04:57 +08:00
|
|
|
&& absolute_row_ == comparand.absolute_row_;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
} // namespace xlnt
|