Copy link to clipboard
Copied
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.
Check parent TextFrame of the InsertionPoint after the table - from there you can check for "empty" TFs in between.
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.
Copy link to clipboard
Copied
Check parent TextFrame of the InsertionPoint after the table - from there you can check for "empty" TFs in between.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Yeah, was thinking about the same as well - getting parent TF for the Cells in the table - which should give better results.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
What is your selection?
I think you've misunderstood my idea?
Copy link to clipboard
Copied
@mateuszp13156491 are you sure this works? I couldn't get it to work for me.
Copy link to clipboard
Copied
@Robert Tkaczyk your idea works fine.
var firstFrame = table.storyOffset.parentTextFrames[0],
lastFrame = firstFrame.insertionPoints.nextItem(table.storyOffset).parentTextFrames[0];
var tableFrames = [],
currentFrame = firstFrame;
while (currentFrame.nextTextFrame != undefined) {
tableFrames.push(currentFrame);
currentFrame = currentFrame.nextTextFrame
if (currentFrame.id === lastFrame.id) {
tableFrames.push(currentFrame);
break;
}
}
$.writeln(tableFrames);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Very nice, indeed! 🙂