Skip to main content
Inspiring
December 8, 2024
Answered

Table range and note problem

  • December 8, 2024
  • 3 replies
  • 529 views

Hi

I wrote a script that finds all table headers and collects the page numbers they are on. Then I check the page number of the next paragraph (table) to see if the table spans more than one page. And everything works fine until there is no footnote reference at the end of the paragraph with the table header - then the next paragraph is the first paragraph of the thread. Do you have any ideas on how to solve this problem?

 

var doc = app.activeDocument;
var toSearch = app.selection[0].parentStory;
var myTxt = app.selection[0].parentStory.paragraphs;



function main(){
    var tableHeadersPages = findTableHeaders();
    alert(tableHeadersPages);
}

function findTableHeaders(){
    // reset szukania
    app.findChangeGrepOptions = null;
    app.findChangeTextOptions = null;
    app.findTextPreferences = app.changeTextPreferences = null;
    app.findGrepPreferences = app.changeGrepPreferences = null;
    // nagłówki tabel – numery stron
    var tableHeadersPages = [];
    app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("Rys i Tab").paragraphStyles.item("Tabela H");
    var tableHeaders = toSearch.findText();

    
    for (var i = 0; i < tableHeaders.length; i++){
        var start = tableHeaders[i].parentTextFrames[0].parentPage.name;
        var stop = myTxt.nextItem(myTxt.nextItem(tableHeaders[i])).parentTextFrames[0].parentPage.name;
        if (start != stop){
            tableHeadersPages.push(start.toString() + '-' + stop.toString());
        }
    }
    return tableHeadersPages;

}


main();

I wrote a short test to select the next paragraph and this script is OK.

var myPara = app.selection[0].paragraphs[0];
var myTxt = app.selection[0].parentStory.paragraphs;
myTxt.nextItem(myPara).select();

Perhaps the problem is because I used the search tool?

Correct answer Peter Kahrel

> Perhaps the problem is because I used the search tool?

No. Eugene was on the right track: the problem is with this line:

 

myTxt.nextItem(myTxt.nextItem(tableHeaders[i]))

 

if a table header consists of one word, you'll have found a word. You could probably get it to work when you target the found item's parent paragraph:

 

myTxt.nextItem(myTxt.nextItem(tableHeaders[i].paragraphs[0]))

 

 

As to finding whether a table breaks across pages, there's an easier way to discover that If a table's parent paragraph consists of one line, the table doesn't break. If a table breaks across two pages, its parent paragraph has two lines. Etc. So:

 

 

for (var i = 0; i < tableHeaders.length; i++) {
  // par is the table's parent paragraph
  par = tableHeaders[i].parent.parent.storyOffset.paragraphs[0];
  if (par.lines.length > 1) {
    start = par.parentTextFrames[0].parentPage.name;
    stop = Number(start) + par.lines.length;
    tableHeadersPages.push(start + '-' + stop.toString());
  }
}

 

 

3 replies

Robert at ID-Tasker
Legend
February 14, 2025

Spam bots are getting better... 

 

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 8, 2024

> Perhaps the problem is because I used the search tool?

No. Eugene was on the right track: the problem is with this line:

 

myTxt.nextItem(myTxt.nextItem(tableHeaders[i]))

 

if a table header consists of one word, you'll have found a word. You could probably get it to work when you target the found item's parent paragraph:

 

myTxt.nextItem(myTxt.nextItem(tableHeaders[i].paragraphs[0]))

 

 

As to finding whether a table breaks across pages, there's an easier way to discover that If a table's parent paragraph consists of one line, the table doesn't break. If a table breaks across two pages, its parent paragraph has two lines. Etc. So:

 

 

for (var i = 0; i < tableHeaders.length; i++) {
  // par is the table's parent paragraph
  par = tableHeaders[i].parent.parent.storyOffset.paragraphs[0];
  if (par.lines.length > 1) {
    start = par.parentTextFrames[0].parentPage.name;
    stop = Number(start) + par.lines.length;
    tableHeadersPages.push(start + '-' + stop.toString());
  }
}

 

 

Inspiring
December 8, 2024

Thank You!!! It works. 

 

The loop to find tables doesn't work – Object does not support the property or method 'storyOffset'

I tried to understand storyOffset but I can't

 
Peter Kahrel
Community Expert
Community Expert
December 8, 2024

storyOffset is an insertion point (though you wouldn't guess it from its name; it was probably a number in early days).

 storyOffset is a property of tables. A table's storyOffset's parentParagraph is therefore the paragraph that the table sits in. Not sure why that doesn't work for you.

 

>Object does not support the property or method 'storyOffset'

 

When the error occurs, what kind of object is tableHeaders[i].parent.parent? Check it in your debugger, or write logs to a console or a file, depending on what you use to debug scripts.

Community Expert
December 8, 2024

Could it be the findText() 

Does 'myTxt.nextItem() with validation' work?

If you add some error handling it might pinpoint the issue 'try...catch' 


Could you instead of relying on

findTextPreferences

 

Directly compare
paragraph.appliedParagraphStyle


Not really sure if I'm honest but might be an alternate approach.

Peter Kahrel
Community Expert
Community Expert
December 8, 2024

--