Copy link to clipboard
Copied
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
> how to delete multiple rows at once
myTable.rows.itemByRange(3,6).remove();
but I don't think it'll make much difference.
Copy link to clipboard
Copied
> how to delete multiple rows at once
myTable.rows.itemByRange(3,6).remove();
but I don't think it'll make much difference.
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more