Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Increment long sequence of numbers by 1

Explorer ,
Aug 05, 2023 Aug 05, 2023

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!

TOPICS
How to
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 06, 2023 Aug 06, 2023

Ugh. Add another backslash before \.: 

 

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

 

 

 

Translate
Community Expert ,
Aug 05, 2023 Aug 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 05, 2023 Aug 05, 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? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2023 Aug 06, 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);
  }
}());

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 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! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2023 Aug 06, 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);
  }
}());
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2023 Aug 06, 2023

You can add the period: 

app.findGrepPreferences.findWhat = '^\\d+(?=\.)';

The caret (^) limits the search to the start of the paragraph, adding returns makes no difference. The period should do it.

 

P.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 2023

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:

fractions0001.jpgexpand image

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2023 Aug 06, 2023

Ugh. Add another backslash before \.: 

 

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

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2023 Aug 06, 2023

Yay! Now it does work. Thanks a lot, Peter.

Just curious, what's the function of the double slash?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2023 Aug 06, 2023

In the interface you'd use a single slash (\. stands for 'dot'), but when you use that in a string you need to escape the backslash. Without it, \. is entered as ., which is 'any character'. That's why it went wrong.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 01, 2023 Nov 01, 2023
LATEST

Hi, sorry for my english. I have a lot of text boxes that starts with a progressive number with a dedicated paragraph style. If i have to increment or decrement these numbers is possible to use a script or find/change grep action?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines