Skip to main content
Known Participant
August 5, 2023
Answered

Increment long sequence of numbers by 1

  • August 5, 2023
  • 3 replies
  • 1375 views

Hi,

Newbie here! 

I have a very long list of entries that I imported from Word and they were not automatically sequenced. That is, if I remove an entry, the numbering does NOT change.

Thing is, I do need to add an entry in the middle of this long list, and I was wondering if there's a way to automatically find all the entry numbers and increment them by 1. 

The formatting is always the same:

paragraph break + entry number + period + text, as in:

1. text

2. text

3. text 

4. text

5. text

If I add a new entry after entry 3, for instance, how do I change change entry 4 to entry 5, and entry 5 to entry 6. 

I think I can find all the numbers using \r[\d.]+ but I have no idea how to replace those numbers incrementally. 

Thanks!

This topic has been closed for replies.
Correct answer Peter Kahrel

I added the period as above, but the script is picking some instances where the number is preceded by a paragraph break and followed by either a ( or a / but not a period. See below:

 

 


Ugh. Add another backslash before \.: 

 

'^\\d+(?=\\.)'

 

 

 

3 replies

Community Expert
August 6, 2023

Sure 🙂

Select the paragraph that should be renumbered and the paragraph before that range. Make sure that the entire first paragraph is selected. In your example, you select these paragraphs:

 

24. text

24. text

25. text

 

and the first one must be selected entirely. Of the last paragraph (25) only the number needs to be selected.

 

(function () {
  app.findGrepPreferences = null;
  app.findGrepPreferences.findWhat = '^\\d+';
  var start = Number (app.selection[0].paragraphs[0].contents.match(/^\d+/)[0]);
  var n = app.selection[0].findGrep();
  for (var i = n.length-1; i >= 0; i--) {
    n[i].contents = String(i+start);
  }
}());
Known Participant
August 6, 2023

Works great indeed! Thanks a lot. 

Thing is, there are a few --not many-- numbers in the main body of text that the script is picking and replacing as if they were entry numbers. 

Would it be possible to add to the "findWhat" the period that follows the number in each entry? That would definitely skip those numbers in the main body of text.

Thanks!

Known Participant
August 6, 2023

Or, if it's any easier, add an extra paragraph break right before the number in the "findWhat" since the sequence is actually:

24. Text

 

24. Text

 

26. Text

 

That is, there are two paragraph breaks between entries. 

Community Expert
August 6, 2023

Sure. Click in any of the paragraphs that have a manual number, then run this script:

(function () {
  app.findGrepPreferences = null;
  app.findGrepPreferences.findWhat = '^\\d+';
  app.findGrepPreferences.appliedParagraphStyle = 
    app.selection[0].appliedParagraphStyle;
  var n = app.selection[0].parentStory.findGrep();
  for (var i = n.length-1; i >= 0; i--) {
    n[i].contents = String(i+1);
  }
}());

This assumes that all the manual numbers are in the same paragraph style. If that's not the case, i.e. the numbers can be in any paragraph and all paragraphs that start with a number should be targeted, then use this one (now select a frame or an insertion point):

(function () {
  app.findGrepPreferences = null;
  app.findGrepPreferences.findWhat = '^\\d+';
  var n = app.selection[0].parentStory.findGrep();
  for (var i = n.length-1; i >= 0; i--) {
    n[i].contents = String(i+1);
  }
}());

 

 

Known Participant
August 6, 2023

Thanks, that seems to work fine. All manual numbers are in the same paragraph style. 

Thing is, there are some 4000 entries in the document, but I only need to make this change in some 1200 entries.

Is there any way to apply this script to a given page range instead of the full document?

Known Participant
August 6, 2023

Peter,

the script works wonders, but resets numbering to 1, right? That is, if  the sequence I want to replace --with a dupe entry-- is

23. text

24. text

24. text

25. text

when I apply the script I get

1. text

2. text

3. text

4. text

instead of

23. text

24. text

25. text

26. text

 

Is there any way for the script to select a given page range and keep the actual numbers instead of reseting it to 1?

Thanks! 

Community Expert
August 5, 2023

You could apply paragraph numbering to the paragraph style for the numbers, then delete all manual numbers:

 

Find: ^\d+\.\t

Replace with <Leave empty

 

to make it future-proof.

Known Participant
August 5, 2023

Thanks, but I really don't want to apply paragraph numberging for a number of reasons.

Is there any way to replace the manual numbers incrementally?