sc/source/core/tool/dbdata.cxx | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
New commits: commit 06a86f7ee3b873e96ff2d6a438dde8ae3662335d Author: Samuel Mehrbrodt <[email protected]> AuthorDate: Mon Jan 31 09:02:33 2022 +0100 Commit: Thorsten Behrens <[email protected]> CommitDate: Thu Mar 17 11:44:49 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 <[email protected]> (cherry picked from commit 690e4852809ea21b5fd909298c5fa2a053fa0458) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129708 Tested-by: Thorsten Behrens <[email protected]> Reviewed-by: Thorsten Behrens <[email protected]> diff --git a/sc/source/core/tool/dbdata.cxx b/sc/source/core/tool/dbdata.cxx index 405473960eca..9278f54c22c3 100644 --- a/sc/source/core/tool/dbdata.cxx +++ b/sc/source/core/tool/dbdata.cxx @@ -998,16 +998,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.copy(lastIndex)).toInt32(); + sal_Int32 nLastIndex = sOldName.lastIndexOf('_') + 1; + sal_Int32 nOldNumber = 1; + if (nLastIndex >= 0) + { + OUString sLastPart(sOldName.copy(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.copy(0, lastIndex + 1) + OUString::number(++nOldNumber); + sNewName = sOldName.copy(0, nLastIndex) + OUString::number(++nOldNumber); } while ((bIsUpperName ? namedDBs.findByUpperName(sNewName) : namedDBs.findByName(sNewName)) != nullptr); return sNewName;
