[Ask for Help] Macro for replace the number located at the beginning of a row
I'd like to use a macro to replace the number located at the beginning of a row with a sequential number. For example,
Original Format:
1.TestA
3.TestB
20.TestC
2.TestD
Wanted Format:
01.TestA
02.TestB
03.TestC
04.TestD
...
10.TestN
But my macro can only get the following result:
1.TestA
2.TestB
3.TestC
...
10.TestN
Would you please give me some tips? Thanks!
function Renumber_FirstNo1()
{
redraw = false;
if( document.selection.IsEmpty )
{document.selection.StartOfDocument();
var count = prompt( "Type the Start Number please:", "1" );
var strFind = "^\\d+";
while(document.selection.Replace(strFind, count, eeFindReplaceRegExp | eeFindNext)) count++;}
else
{var count = prompt( "Type the Start Number please:", "1" );
var strFind = "";
while(document.selection.Replace(strFind + "^\\d+", strFind + count, eeFindReplaceRegExp | eeFindNext | eeFindReplaceSelOnly)) count++;
}
}
You have to add the zero's padding yourself.
For example (a fix-two-digits result):
redraw = false;
if( document.selection.IsEmpty )
{
document.selection.StartOfDocument();
var iCount = prompt("Type the Start Number please:", "1");
var strFind = "^\\d+";
while(document.selection.Replace(strFind, iCount, eeFindReplaceRegExp | eeFindNext))
{
iCount++;
iCount = ("0"+iCount).slice(-2);
}
}
-or, more flexible (type in '1', or '01', or '001', or '0001', .... to set the wanted amount of zeros:
redraw = false;
if( document.selection.IsEmpty )
{
document.selection.StartOfDocument();
var iCount = prompt("Start Number (incl. leading zero(s):", "1");
var iCountLen = iCount.toString().length;
var strFind = "^\\d+";
while(document.selection.Replace(strFind, iCount, eeFindReplaceRegExp | eeFindNext))
{
iCount++;
iCount = ("00000000"+iCount).slice(-iCountLen);
}
}
Hello, Stefan. It works very well. Thank you!
3 posts in this topic