summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorSamuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>2022-01-31 09:02:33 +0100
committerSamuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>2022-02-09 08:37:07 +0100
commit690e4852809ea21b5fd909298c5fa2a053fa0458 (patch)
treef40ec7f036e00e1d080054fb26fee05b970af105 /sc
parent19a5e50955ecc8da7ff5dbdfb9f47352d851ee1d (diff)
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.mehrbrodt@allotropia.de>
Diffstat (limited to 'sc')
-rw-r--r--sc/source/core/tool/dbdata.cxx26
1 files changed, 20 insertions, 6 deletions
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;