Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript InDesign - Slow to delete table rows / How to delete multiple table rows at once

Explorer ,
Dec 03, 2025 Dec 03, 2025

Hi,

 

I wrote a function that deletes empty table rows, but for some reason it works very slowly. When 180 rows have to be deleted, it takes 21 seconds to do so. Considering I have two such tables and many data merged documents, it rather adds up.

 

I was wondering if someone knows why it is this slow. We thought that perhaps we could make it faster by deleting a range of rows at once rather than deleting rows one by one, but I don't know how to delete multiple rows at once.

 

Here is the code:

    function delRows(doc){
    // If the tables' rows' contents are 0 in length (i.e. empty), delete the rows

        var myRowsApp1 = doc.textFrames.itemByName("Appendix1").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp1.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp1[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                myRowsApp1[i].remove();
            }
        }
        
        var myRowsApp2 = doc.textFrames.itemByName("Appendix2").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp2.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp2[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length == 0){
                myRowsApp2[i].remove();
            }
        }

    }

 

Cheers,

Heidi

TOPICS
How to , Performance , Scripting
174
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 03, 2025 Dec 03, 2025

how to delete multiple rows at once

myTable.rows.itemByRange(3,6).remove();

but I don't think it'll make much difference. 

Translate
Community Expert ,
Dec 03, 2025 Dec 03, 2025

how to delete multiple rows at once

myTable.rows.itemByRange(3,6).remove();

but I don't think it'll make much difference. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 05, 2025 Dec 05, 2025

Thanks Peter! It actually made a world of difference, it went down to 1.2 seconds!

 

This is my new script:

    function delRows(doc){
    // If the tables' rows' contents are 0 in length (i.e. empty), delete the rows

        var myRowsApp1 = doc.textFrames.itemByName("Appendix1").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp1.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp1[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                lastRowApp1 = i+1;
                break;
            }
        }

        doc.textFrames.itemByName("Appendix1").tables[0].rows.itemByRange(lastRowApp1,myRowsApp1.length-1).remove();
        
        var myRowsApp2 = doc.textFrames.itemByName("Appendix2").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp2.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp2[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                lastRowApp2 = i+1;
                break;
            }
        }

        doc.textFrames.itemByName("Appendix2").tables[0].rows.itemByRange(lastRowApp2,myRowsApp2.length-1).remove();
    }
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2025 Dec 03, 2025

One thing you can try is to define your row.length-1 variable outside the for loop, so the script is not having to check table length on every iteration. Maybe a slight bump in performance. Also not quite sure why you are shifting the string; perhaps some minor performance gains in your check.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 05, 2025 Dec 05, 2025
LATEST

Hi Brian, 

Thanks for the suggestion. I tried this after the optimization to delete multiple rows at once (which brought it back to 1.187 seconds) and defining the length variable actually slowed it down a tiny bit (1.216 seconds). The reason I do the shift is because the first column of the table contains row numbers, so if I wouldn't do this there wouldn't be any empty rows.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2025 Dec 03, 2025

Hi @Jurre37971467hccf , Maybe something like this? My test ran in less than 1 sec

 

//the document’s tables
var dt = app.activeDocument.stories.everyItem().tables.everyItem().getElements()
//delete empty rows from the document’s first table
delEmptyRows(dt[0].rows.everyItem().getElements())
//$.writeln($.hiresTimer * .00000001)
alert(dt[0].rows.length + " rows with content")

/**
* Deletes empty rows 
* @ param r the table’s rows 
* @ return void 
*/
function delEmptyRows(r){
    for (var i = r.length-1; i > -1; i--){
        if (!checkRow(r[i])) {
            r[i].remove()
        } 
    };   
}


/**
* Check if any of a rows' cells contain text 
* @ param r the row to check
* @ return true if any row cell has text 
*/
function checkRow(r){
    var b = false
    var c = r.cells.everyItem().getElements();
    for (var i = 0; i < c.length; i++){
        if (c[i].contents != "") {
            b = true
        }
    };   
    return b
}

 

 

Before and after:

 

Screen Shot 22.png

 

 

Screen Shot 23.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines