diff --git a/src/Mod/Spreadsheet/App/PropertySheet.cpp b/src/Mod/Spreadsheet/App/PropertySheet.cpp index 5933cdc5ae..9695a1d411 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -127,40 +127,47 @@ const Cell* PropertySheet::getValueFromAlias(const std::string& alias) const } } -bool PropertySheet::isValidAlias(const std::string& candidate) +bool PropertySheet::isValidCellAddressName(const std::string& candidate) { static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$"); boost::cmatch cm; + /* Check if it matches a cell reference */ + if (boost::regex_match(candidate.c_str(), cm, gen)) { + static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})"); + + if (boost::regex_match(candidate.c_str(), cm, e)) { + const boost::sub_match colstr = cm[1]; + const boost::sub_match rowstr = cm[2]; + + if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) { + return true; + } + } + } + return false; +} + +bool PropertySheet::isValidAlias(const std::string& candidate) +{ + /* Check if it is used before */ if (getValueFromAlias(candidate)) { return false; } + /* check if it would be a valid cell address name, e.g. "A2" or "C3" */ + if (isValidCellAddressName(candidate)) { + return false; + } + /* Check to make sure it doesn't clash with a reserved name */ if (ExpressionParser::isTokenAUnit(candidate) || ExpressionParser::isTokenAConstant(candidate)) { return false; } - /* Check to make sure it doesn't match a cell reference */ - if (boost::regex_match(candidate.c_str(), cm, gen)) { - static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})"); - - if (boost::regex_match(candidate.c_str(), cm, e)) { - const boost::sub_match colstr = cm[1]; - const boost::sub_match rowstr = cm[2]; - - // A valid cell address? - if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) { - return false; - } - } - return true; - } - else { - return false; - } + return true; } namespace diff --git a/src/Mod/Spreadsheet/App/PropertySheet.h b/src/Mod/Spreadsheet/App/PropertySheet.h index 2e0cd268bb..676818ce16 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.h +++ b/src/Mod/Spreadsheet/App/PropertySheet.h @@ -124,6 +124,9 @@ public: bool isValidAlias(const std::string& candidate); + // checks whether candidate is of form A1, C4, etc. + bool isValidCellAddressName(const std::string& candidate); + std::vector getUsedCells() const; std::tuple getUsedRange() const; diff --git a/src/Mod/Spreadsheet/App/Sheet.cpp b/src/Mod/Spreadsheet/App/Sheet.cpp index 589b0d90a0..e99fd3b2d3 100644 --- a/src/Mod/Spreadsheet/App/Sheet.cpp +++ b/src/Mod/Spreadsheet/App/Sheet.cpp @@ -109,7 +109,11 @@ Sheet::~Sheet() } /** - * Clear all cells in the sheet. + * Clear all cells in the sheet. These are implemented as dynamic + * properties, for example "A1" is added as a dynamic property. Since + * now users may add dyanamic properties, we need to try to avoid + * removing those, too, so we check whether the dynamic property name + * is a valid cell address name before removing it. */ void Sheet::clearAll() @@ -119,7 +123,9 @@ void Sheet::clearAll() std::vector propNames = props.getDynamicPropertyNames(); for (const auto& propName : propNames) { - this->removeDynamicProperty(propName.c_str()); + if (cells.isValidCellAddressName(propName.c_str())) { + this->removeDynamicProperty(propName.c_str()); + } } propAddress.clear();