Copy link to clipboard
Copied
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
I’m not sure the script has to be as complex.
Is there always a soft return before the SUPER OBLATA string? This might work:
// Ensure a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
var tablesProcessed = 0;
var res = getGrepSearch("\\nSUPER OBLATA")
for (var i = 0; i < res.length; i++){
if (res[i].parent.constructor.name == "Cell" && res[i].parent.lines.length == 2) {
res[i].parent.parentRow.remove();
tablesP
Copy link to clipboard
Copied
Hi @Benjamin39010368auuu , can you share an ID doc with a table the script doesn't work with? Attach it to a reply
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I’m not sure the script has to be as complex.
Is there always a soft return before the SUPER OBLATA string? This might work:
// Ensure a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
var tablesProcessed = 0;
var res = getGrepSearch("\\nSUPER OBLATA")
for (var i = 0; i < res.length; i++){
if (res[i].parent.constructor.name == "Cell" && res[i].parent.lines.length == 2) {
res[i].parent.parentRow.remove();
tablesProcessed++;
}
};
} else {
alert("No document is open.");
}
/**
* Gets results of a text search as an array
* @ param text to search for
* @ return result as an array
*/
function getGrepSearch(fp){
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findChangeGrepOptions.properties = {includeHiddenLayers:true, includeLockedLayersForFind:true, includeLockedStoriesForFind:true, includeMasterPages:true}
app.findGrepPreferences.findWhat = fp;
return app.findGrep()
}
Copy link to clipboard
Copied
This worked great! It's flexible so that I can modify it for other circumstances. Thanks for your help.
Copy link to clipboard
Copied
Maybe a simple question, but are you running this script after you execute the merge? Not during preview? Because nothing would happen during preview. Also, are any of the cells in the table merged? As @rob day mentions, a sample executed merge doc would help.
Copy link to clipboard
Copied
Simple questions aren't always simple for me, so better to ask. I ran the script in a merged document, not through the preview function. I attached the first two pages of the script in the previous reply.
Copy link to clipboard
Copied
Also i don’t think you can assume the cell’s contents will be a text string. For example this:
Get cell 1 contents:
var t=app.activeDocument.textFrames[0].parentStory.tables[0]
$.writeln(t.cells[0].contents)
//returns nothing
$.writeln(t.cells[0].allPageItems[0])
//returns [object TextFrame]
$.writeln(t.cells[0].allPageItems[0].contents)
//returns Yo
$.writeln(t.cells[1].contents)
//returns hi
$.writeln(t.cells[2].contents)
//returns [object Rectangle]