Script modification: delete row if cell contains specific text
Hi guys,
I am a newbie and and trying to using the great resources out there and not be needy, but this one is stumping me.
I have created a table for a data merge. Each cell contains some text as a heading (in this case "SUPER OBLATA") and then a data merge target underneath it. Sometimes the table does not have data for that target so it is blank leaving the cell with the heading "SUPER OBLATA" and nothing underneath it. I used AI to create a script to examine the first column, remove hidden spaces/breaks/characters, and then check if the cell contains exactly the header. If found, delete the row.
The problem is, that the script doesn't find any such rows even though there are many.
If I go into the cell, delete everything, and then type the term in with a bunch of spaces and breaks and rerun the script, it works fine.
What gives?
// Ensure a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
var tablesProcessed = 0;
// Normalize the search term by removing spaces, line breaks, and converting to lowercase
var searchTerm = "SUPER OBLATA".replace(/\s+/g, '').toLowerCase();
// Regular expression to match hidden characters
var hiddenCharRegex = /[\u00A0\u00AD\u200B]/g;
// Iterate over all stories in the document
for (var i = 0; i < doc.stories.length; i++) {
var story = doc.stories[i];
for (var j = 0; j < story.tables.length; j++) {
var table = story.tables[j];
var rowsDeleted = 0;
// Iterate over rows from bottom to top to avoid skipping rows after deletion
for (var k = table.rows.length - 1; k >= 0; k--) {
// Get the text content of the first cell in the row
var cellText = table.rows[k].cells[0].contents;
// Normalize the cell text by removing spaces, line breaks, hidden characters, and converting to lowercase
var normalizedCellText = cellText.replace(/\s+/g, '').replace(hiddenCharRegex, '').toLowerCase();
// Check if the normalized cell text matches the search term
if (normalizedCellText === searchTerm) {
// Delete the entire row
table.rows[k].remove();
rowsDeleted++;
}
}
if (rowsDeleted > 0) {
tablesProcessed++;
}
}
}
if (tablesProcessed > 0) {
alert("Processed " + tablesProcessed + " table(s). Rows where the first cell contains 'SUPER OBLATA' (ignoring spaces, line breaks, and hidden characters) have been deleted.");
} else {
alert("No rows where the first cell contains 'SUPER OBLATA' (ignoring spaces, line breaks, and hidden characters) were found.");
}
} else {
alert("No document is open.");
}
Thanks for your help!
Also, if using AI script is some sort of faux pas here, my bad.
-Ben


