sc/source/core/tool/dbdata.cxx | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
New commits: commit 690e4852809ea21b5fd909298c5fa2a053fa0458 Author: Samuel Mehrbrodt <samuel.mehrbr...@allotropia.de> AuthorDate: Mon Jan 31 09:02:33 2022 +0100 Commit: Samuel Mehrbrodt <samuel.mehrbr...@allotropia.de> CommitDate: Wed Feb 9 08:37:07 2022 +0100 Fix copying range when it doesn't contain a number Copying a range named "aaa" resulted in a new range named "1". Now it will be named "aaa2". Also, when range is named "my_range", the new range is now "my_range2" instead of "my_1". Change-Id: Ib6356454d70244b76de50816e7902852290c3270 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129198 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <samuel.mehrbr...@allotropia.de> diff --git a/sc/source/core/tool/dbdata.cxx b/sc/source/core/tool/dbdata.cxx index 25dc833e3732..8d2d146b31d8 100644 --- a/sc/source/core/tool/dbdata.cxx +++ b/sc/source/core/tool/dbdata.cxx @@ -995,16 +995,30 @@ public: }; OUString lcl_IncrementNumberInNamedRange(ScDBCollection::NamedDBs& namedDBs, - const OUString& sOldName,bool bIsUpperName) + const OUString& sOldName, bool bIsUpperName) { - sal_Int32 lastIndex = sOldName.lastIndexOf('_'); - sal_Int32 nOldNumber = 0; - if (lastIndex >= 0) - nOldNumber = OUString(sOldName.subView(lastIndex)).toInt32(); + sal_Int32 nLastIndex = sOldName.lastIndexOf('_') + 1; + sal_Int32 nOldNumber = 1; + if (nLastIndex >= 0) + { + OUString sLastPart(sOldName.subView(nLastIndex)); + nOldNumber = sLastPart.toInt32(); + + // When no number found, add number at the end. + // When there is a literal "0" at the end, keep the "lastIndex" from above + // (OUString::toInt32() also returns 0 on failure) + if (nOldNumber == 0 && sLastPart != "0") + { + nOldNumber = 1; + nLastIndex = sOldName.getLength(); + } + } + else // No "_" found, add number at the end + nLastIndex = sOldName.getLength(); OUString sNewName; do { - sNewName = sOldName.subView(0, lastIndex + 1) + OUString::number(++nOldNumber); + sNewName = sOldName.subView(0, nLastIndex) + OUString::number(++nOldNumber); } while ((bIsUpperName ? namedDBs.findByUpperName(sNewName) : namedDBs.findByName(sNewName)) != nullptr); return sNewName;