Jump to content

Find and Replace


Fellsway

Recommended Posts

You still haven't indicated what you are trying to accomplish, but if you just want to convert values per record in a callback rule, that's certainly possible.

 

OnRecordStart rule:

noBlanks = []; // don't use 'var' to make array global
var totalFields = 100; //enter the number of fields to check
var startField = 1; // enter which field to begin with
for (var s=0; s<totalFields; s++) {
  noBlanks[s] = Field(s+startField).replace(/blank/g," ");
}

Then pull from the array rather than the field in your data for the corrected value.

Link to comment
Share on other sites

Is there a way for FusionPro to search through multiple fields (over 100 fields)

and do a text replacement? Example: Find the word "blank" and replace with a space instead.

Sure, do this in OnRecordStart:

for (var i in FusionPro.Fields)
   FusionPro.Composition.AddVariable(i, ReplaceSubstring(Field(i), "blank", " "));

Note that this replaces the field values called out directly in text frames in the Variable Text Editor, which is what I assume you're trying to do, but it doesn't affect the values of the fields returned by the Field function in other rules.

Link to comment
Share on other sites

I'm not Dan, but I think this modification should work for you:

 

for (var i in FusionPro.Fields)
   FusionPro.Composition.AddVariable(i, ReplaceSubstring(Field(i), "blank", "[color="Red"]<color name=\"White\">blank</color> [/color]"));

Link to comment
Share on other sites

Step and I think alike, but I think Dan's solution only works for variables placed directly into a text frame rather than used in rules. To do what you are asking now, I think you'd have to go back to my solution and replace

   noBlanks[s] = Field(s+startField).replace(/blank/g," ");

with

   noBlanks[s] = Field(s+startField).replace(/blank/g,"[color=Red]<color name=\"White\">blank</color>[/color]");

and then create a text rule which pulls the correct element from the array. In the Rule dialog, you would be able to check for tagged text.

 

Of course, Dan tends to know things about FP that we don't so he may have a better (aka simpler) solution. On a side note, this is why it is often helpful to explain what you are trying to accomplish in full rather than providing bits of information at a time. ;)

Link to comment
Share on other sites

Step, that inserted the words "<color name=\"White\">blank</color>" in my text frame.

 

You need to make sure that "treat return strings as tagged text" is checked in the text box

Well, this is something you would do in OnRecordStart, so there's no box to check.

 

Here is where the Building Blocks dialog is your friend. If you open it up and go to the Objects tab, then expand "FusionPro (Global)", Composition, and Variables, then double-click on AddVariable, it inserts this signature:

FusionPro.Composition.AddVariable(varName, varValue [,bTreatAsTagged])

So what you need to do is set that optional third parameter to true, which has basically the same effect on that variable as the check box does for a regular rule, that is, it tells the composition engine to treat the value as tagged markup instead of as literal text. So change the logic to this:

for (var i in FusionPro.Fields)
   FusionPro.Composition.AddVariable(i, ReplaceSubstring(Field(i), "blank",
       "<color name=\"White\">blank</color>"[color=SeaGreen], true[/color]));

Eric is correct that this solution only works for variables placed directly into a text frame rather than used in rules, as I noted in my first post in the thread. However, you still need to denote that you want the value interpreted as tagged text.

Link to comment
Share on other sites

  • 3 weeks later...

This was very helpful to me as well. I used Dan's OnRecordStart. Quick question (and please forgive my ignorance), how would you format multiple find/replace statements in the OnRecordStart Rule?

For example, I'd like to replace "$", "0.", ".00", and "%" with "". Is this possible? Any help is greatly appreciated.

Link to comment
Share on other sites

Correct. I'm provided with data that reads $1.00 or $0.50 as an example. Depending on whether the amount is greater than .99, I'll insert a dollar sign prior or a cents sign after the number. The design calls for no decimals except for an instance where there is both dollars and cents ($1.50).

 

I did see your post in another thread about multiple "find and replaces", but couldn't seem to get it to work on "OnRecordStart".

 

This is still pretty new to me. Thanks for any help.

 

- Oh thanks Step, I missed your post. I'll give that a try.

Edited by knaselk
Link to comment
Share on other sites

Thanks guys, I got it working. I used this from one of Dan's posts:

 

var s = Field("Amount");

FusionPro.Composition.AddVariable

s = ReplaceSubstring(s, "$", "");

s = ReplaceSubstring(s, "0.", "");

s = ReplaceSubstring(s, ".00", "");

s = ReplaceSubstring(s, "%", "");

return s;

 

Then I used a second rule to insert the superscript symbols before or after, depending on the original amount field. I'm sure there's a more elegant way of doing it, but it works for me. And, I actually understand it ;) Thanks again!

Edited by knaselk
Link to comment
Share on other sites

I think that this code is going to be problematic for instances where there is a 0 before the first decimal but is not the first number. For example: "$10.00" will return 100. I commented off to the side of each line of code what the expected result will be and highlighted in red what is being replaced by each line of code.

 

var s = Field("Amount");

FusionPro.Composition.AddVariable

s = ReplaceSubstring(s, "$", ""); //$10.00

s = ReplaceSubstring(s, "0.", ""); //10.00

s = ReplaceSubstring(s, ".00", ""); //100

s = ReplaceSubstring(s, "%", ""); //100

return s; //100

 

This is why I felt it best to use a regular expression.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...