Skip to main content
Inspiring
May 10, 2023
Answered

Checking the contents of textFrame with a table

  • May 10, 2023
  • 4 replies
  • 1246 views

Hi

 

How to check if a text frame contains a table?

What I mean – The table starts on page 1 and ends on page 3. The text frame on page 2 shows the contents.length = 0, but it contains a part of the table. I want to distinguish text frames containig tables and really empty frames.

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

To check whether a tect frame contains a table or the continuation of a table, check the text frame's paragraph (if it has one):

 

if (frame.paragraphs.length > 0 
  && frame.paragraphs[0].tables.length > 0) {
    // The frame contains a continuation table
}

 

P.

4 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
May 11, 2023

To check whether a tect frame contains a table or the continuation of a table, check the text frame's paragraph (if it has one):

 

if (frame.paragraphs.length > 0 
  && frame.paragraphs[0].tables.length > 0) {
    // The frame contains a continuation table
}

 

P.

m1b
Community Expert
Community Expert
May 11, 2023

Very nice, indeed! 🙂

Robert at ID-Tasker
Legend
May 10, 2023

I think you should use @m1b approach and build and check collection of TFs that are parents for your table - sometimes TF can be empty because text is overset or break character is used of ParaStyle definition forces text to start on even / odd pages.

 

Unless of course you know what you have in your Story. 

 

m1b
Community Expert
Community Expert
May 10, 2023

Hi @mateuszp13156491, in case Robert's method doesn't work out, here is another approach:

/**
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/checking-the-contents-of-textframe-with-a-table/m-p/13783085
 */
function main() {

    var doc = app.activeDocument,
        textFrames = doc.textFrames,
        textFramesWithTables = [];

    for (var i = 0; i < textFrames.length; i++) {

        if (textFrames[i].tables[0].isValid)
            textFramesWithTables = textFramesWithTables.concat(textFramesOfTable(textFrames[i].tables[0]));

    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');


/**
 * Returns textframes containing the table.
 * @author m1b
 * @version 2023-05-10
 * @param {Table} table - an Indesign Table.
 * @returns {Array<TextFrame>}
 */
function textFramesOfTable(table) {

    var textFrames = [],
        id;

    for (var r = 0; r < table.rows.length; r++) {

        var tf = table.rows[r].cells[0].insertionPoints[0].parentTextFrames[0];

        if (
            tf.isValid
            && tf.id !== id
        ) {
            textFrames.push(tf)
            id = tf.id;
        }

    }

    return textFrames;

};

 I figure if you explicitly get the textframes of the table, you can go from there.

- Mark

Robert at ID-Tasker
Legend
May 10, 2023

Yeah, was thinking about the same as well - getting parent TF for the Cells in the table - which should give better results. 

 

Inspiring
May 10, 2023

Great! I,ve tried and that is my method:

var a = app.selection[0].id;
var b = app.selection[0].insertionPoints[0].parentTextFrames[0].id;

 

if a != b the frame contains a part of the table

 

Thank You Robert and thank You Mark for the script.

 

Regards

Mateusz

 

Robert at ID-Tasker
Legend
May 10, 2023

Check parent TextFrame of the InsertionPoint after the table - from there you can check for "empty" TFs in between.