adobe-indesignextendscript

Is there any way to move above text into the below table's second cell in InDesign using JavaScript


In my InDesign document there are many tables, each table is on separate page with autoflowing text. There is a text above each table without paragraph returns. I want that text to move from there to table's second cell. This I want to do for all the tables in my active document. Basically I need to move text from other place into InDesign table. Can someone please help. Manually doing this is very time consuming and panic. There are many tables in my InDesign document.

this is the what I am expecting which is shown in attached screenshot


Solution

  • Select the text frame and try to run this code:

    var story = app.selection[0].parentStory;
    var pgfs = story.paragraphs;
    
    for (var i=0; i<pgfs.length; i++) {
        if (pgfs[i].tables.length == 0) continue;
    
        var table = pgfs[i].tables[0];
        var cell = table.rows[0].cells[1];
        var contents = pgfs[i].lines[0].contents;
    
        app.findTextPreferences = NothingEnum.NOTHING;
        app.changeTextPreferences = NothingEnum.NOTHING;
        app.findTextPreferences.findWhat = contents;
        app.changeTextPreferences.changeTo = '';
        pgfs[i].changeText();
    
        cell.contents = contents;
    }
    

    Or, even simpler solution without changeText():

    var story = app.selection[0].parentStory;
    var pgfs = story.paragraphs;
    
    for (var i=0; i<pgfs.length; i++) {
        if (pgfs[i].tables.length == 0) continue;
        pgfs[i].tables[0].rows[0].cells[1].contents = pgfs[i].lines[0].contents;
        pgfs[i].lines[0].contents = '';
    }
    

    Update

    To move the footnotes you can try this:

    var story = app.selection[0].parentStory;
    var pgfs = story.paragraphs;
    
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = "footnote\\s+\\d+.+"; // <-- your GREP
    
    for (var i=pgfs.length-1; i>=0; i--) {              // loop backward
        if (pgfs[i].tables.length == 0) continue;       // skip if no table
        var finds = pgfs[i].findGrep();
        if (finds.length == 0) continue;                // skip if noting was found
        pgfs[i].insertionPoints[-1].contents = finds[0].contents;
        finds[0].contents = '';
    }
    

    Before:

    enter image description here

    After:

    enter image description here