Skip to main content
Inspiring
August 6, 2019
Answered

Delete last row in all tables in an Indesign document

  • August 6, 2019
  • 1 reply
  • 1443 views

Hi,

I have a number of documents with numerous tables in them - I would like to be able to write a simple script (with Extendscript/Javascript) to delete the last row of every table on the current open document. Any pointers/help would be really appreciated.

Thank you.

I have the code below (I assumed I could use "allTables.rows.delete(LocationOptions.AT_END)" but not sure it is right!...

//get all the 'stories' in the document

var documentStories = app.activeDocument.stories.everyItem();

//get all of the tables from within the stories

var allTables = documentStories.tables.everyItem().getElements();

//delete the last row of every table

allTables.rows.delete(LocationOptions.AT_END);

This topic has been closed for replies.
Correct answer karthikS

Hi,

Try below:

var documentStories = app.activeDocument.stories.everyItem();

var allTables = documentStories.tables.everyItem().getElements();

for(var t=0; t<allTables.length; t++){

    var lastRow = allTables.rows[-1];

    lastRow.remove();

    }

Thanks,

SK

1 reply

karthikS
karthikSCorrect answer
Inspiring
August 6, 2019

Hi,

Try below:

var documentStories = app.activeDocument.stories.everyItem();

var allTables = documentStories.tables.everyItem().getElements();

for(var t=0; t<allTables.length; t++){

    var lastRow = allTables.rows[-1];

    lastRow.remove();

    }

Thanks,

SK

Inspiring
August 6, 2019

Thanks SK,

that worked a treat

very helpful indeed.

Community Expert
August 6, 2019

Hi David,

that should work with a one-liner as well:

app.documents[0].stories.everyItem().tables.everyItem().rows[-1].remove();

It will also work if there are tables present with only one body row and no footer rows.

There must be at least one table with one footer row or a table with two body rows and no footer row.

My InDesign CC 2019 crashed with a test document that was never saved.

It did not crash with the same contents after it was saved. The one-liner script worked as expected.

FWIW: If you define the last row as the last body row of a table different code is needed.

Regards,
Uwe