In the previous post, we gained a basic understanding of how to remove a CondFmt (Condition Format) from a text range. Let’s see it in action. To set this up, make sure you have a sample document with two conditions, Condition1 and Condition2, and have both conditions applied to a paragraph in the document. Select some of the text in the paragraph and run this:
#target framemaker var doc = app.ActiveDoc; var textRange = doc.TextSelection; var prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond); var condFmts = prop.propVal.osval; // Remove the second condtion format and update the osval list. Array.prototype.splice.call (condFmts, 1, 1); prop.propVal.osval = condFmts; // Apply the updated list back to the text range. doc.SetTextPropVal (textRange, prop);
To keep the example simple, we are making a couple of assumptions: First, we are removing Condition2 from the text and second, that Condition2 is the second CondFmt in the osval list. We will use code to eliminate these assumptions later.
On line 7, we assign the osval list to a variable (condFmts). We need a way to remove members from this condFmts list. If condFmts was a true JavaScript array, we could use its splice method to remove the desired member. But it is actually an array-like list of CondFmt objects that doesn’t contain a splice method. However, JavaScript allows us to “borrow” the splice method from the built-in Array object and use it on our list; this is what we are doing on line 10.
On line 11, we assign the modified list back to prop.propVal.osval and on line 14 we apply the updated property to the selected text. When you run this, you should see the Condition2 format removed from the selected text.
If we wanted to remove all formats from the text, we could use this shortcut:
#target framemaker var doc = app.ActiveDoc; var textRange = doc.TextSelection; var prop = doc.GetTextPropVal (textRange.beg, Constants.FP_InCond); // Remove all conditions from the text. prop.propVal.osval.length = 0; // Apply the updated list back to the text range. doc.SetTextPropVal (textRange, prop);
Line 9 simply sets the length of the osval list to 0 (zero), which removes all of the members.
Conditionalized table rows are handled a little bit differently than text. To set this up, add a table to your document and apply Condition1 and Condition2 to one of the rows and leave the row selected. Run this code:
#target framemaker var doc = app.ActiveDoc; // Get the selected row object. var tbl = doc.SelectedTbl; var row = tbl.TopRowSelection; // Display the list of Condition Formats applied to the row. alert (row.InCond);
As you can see, the condition list is easier to get to with a table row than it is with text. Let’s splice the second member from the list and apply it back to the table row:
#target framemaker var doc = app.ActiveDoc; // Get the selected row object. var tbl = doc.SelectedTbl; var row = tbl.TopRowSelection; // Remove the second condition from the list and update the row. var inCond = row.InCond; Array.prototype.splice.call (inCond, 1, 1); row.InCond = inCond;
As with text, we can use a shortcut to remove all conditions from the row:
#target framemaker var doc = app.ActiveDoc; // Get the selected row object. var tbl = doc.SelectedTbl; var row = tbl.TopRowSelection; // Remove all conditions from the list and update the row. var inCond = row.InCond; inCond.length = 0; row.InCond = inCond;
In the next post, we will generalize this code into functions so that you can remove conditions from all of the text and table rows in a document.