Removing Conditions From Text and Table Rows – Part 4

Before continuing our discussion of removing conditions from text, let’s take a look at table rows. Working with conditional table rows is easier that working with conditionalized text, so we can get this code out of the way. To set the stage, add a table to a document and apply two conditions, Condition1 and Condition2 to one of the rows. Select the conditionalized row and run this code:

#target framemaker

var doc = app.ActiveDoc;
var row = doc.SelectedTbl.TopRowSelection;

alert (row.InCond);

Condition Formats applied to the row

This is similar to what we did when working with a text range in Part 1, but more direct. Since we have a function for removing conditions from text, we can use this as a starting point for a table rows function.

function removeConditionsFromText (textRange, removeConditions, doc) {
    
    var prop, condFmts, i;
    
    // Get a list of the conditions applied to the beginning of the text range.
    prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond);
    condFmts = prop.propVal.osval;
    if (condFmts.length === 0) {
        return; // No conditions are applied to the text range; exit the function.
    }

    // Loop through the condition format names that are applied to the text.
    for (i = 0; i < condFmts.length; i += 1) {
        // See if the format is in the remove list.
        if (removeConditions.indexOf (condFmts[i].Name) > -1) {
            // Remove it from the list.
            Array.prototype.splice.call (condFmts, i, 1);
        }
    }

    if (condFmts.length !== prop.propVal.osval.length) {
        // Conditions were removed; apply the updated list back to the text range.
        prop.propVal.osval = condFmts;
        doc.SetTextPropVal (textRange, prop);
    }
}

Let’s rework it together line-by-line: on line 1, change the name to removeConditionsFromRow and change the first argument from textRange to row.

function removeConditionsFromText (textRange, removeConditions, doc) {

We won’t need a prop variable so remove it from line 3:

function removeConditionsFromRow (row, removeConditions, doc) {
    
    var condFmts, i;

Lines 5-10 can be shortened to this:

function removeConditionsFromRow (row, removeConditions, doc) {
    
    var condFmts, i;
    
    // Get a list of the conditions applied to the row.
    condFmts = row.InCond;
    if (condFmts.length === 0) {
        return; // No conditions are applied to the row; exit the function.
    }

Lines 12-19 from the original function can be used as is, except for a minor change to the first comment:

function removeConditionsFromRow (row, removeConditions, doc) {
    
    var condFmts, i;
    
    // Get a list of the conditions applied to the row.
    condFmts = row.InCond;
    if (condFmts.length === 0) {
        return; // No conditions are applied to the row; exit the function.
    }

    // Loop through the condition format names that are applied to the row.
    for (i = 0; i < condFmts.length; i += 1) {
        // See if the format is in the remove list.
        if (removeConditions.indexOf (condFmts[i].Name) > -1) {
            // Remove it from the list.
            Array.prototype.splice.call (condFmts, i, 1);
        }
    }

    if (condFmts.length !== prop.propVal.osval.length) {
        // Conditions were removed; apply the updated list back to the text range.
        prop.propVal.osval = condFmts;
        doc.SetTextPropVal (textRange, prop);
    }
}

Finally, lines 21-25 of the original function can be changed to work with a row:

function removeConditionsFromRow (row, removeConditions, doc) {
    
    var condFmts, i;
    
    // Get a list of the conditions applied to the row.
    condFmts = row.InCond;
    if (condFmts.length === 0) {
        return; // No conditions are applied to the row; exit the function.
    }

    // Loop through the condition format names that are applied to the row.
    for (i = 0; i < condFmts.length; i += 1) {
        // See if the format is in the remove list.
        if (removeConditions.indexOf (condFmts[i].Name) > -1) {
            // Remove it from the list.
            Array.prototype.splice.call (condFmts, i, 1);
        }
    }

    if (condFmts.length !== row.InCond.length) {
        // Conditions were removed; apply the updated list back to the row.
        row.InCond = condFmts;
    }
}

Now we can test the finished function. Add the following code before the removeConditionsFromRow function and add the augmentObjects function to the end of the script. Select the table row that has Condition1 and Condition2 applied to it and run the complete script. You should see the Condition2 format removed from the selected row.

#target framemaker

// Call a function to add functions to built-in objects.
augmentObjects ();

var doc = app.ActiveDoc;

// List of conditions to remove.
var removeConditions = ["Condition2"];

// Remove the condition(s) from the selected row.
var row = doc.SelectedTbl.TopRowSelection;
removeConditionsFromRow (row, removeConditions, doc);

In an upcoming post, we will return to working removing conditions from text.

Leave a Reply

Your email address will not be published. Required fields are marked *