Jump to content

Replacing text - complex?


Recommended Posts

I'm VERY new to FusionPro, and don't have much of a clue when it comes to javascript, so forgive me ahead of time. Here's what I'm trying to do:

 

The first few words of one paragraph is (John is used as a sample) "John, need to...". What I'm trying to do and keep getting a syntax error on is if the first name field is blank, replace the comma and word "need" with just the word "Need" with a capital N. I have another paragraph that's very similar with the same dilemma. Do I create a new rule using a "replacestring" command? I think that's right, but I'm not totally sure, and can't seem to find the answer in the manual.

 

Help? Thanks!

Link to comment
Share on other sites

It sounds like only the "<<name>>, N" is variable and the rest of the text is static, right? If so, you could have a rule that checks to see if the value of the name field is empty and adjust just the quoted portion above, followed by the rest of the text entered manually in the text editor window.

if (Field("Name") != "") {
  return Field("Name") + ", N";
  }
else return "N";

There would, of course, be no space between the rule and the "eed to..." in your text editor.

Link to comment
Share on other sites

OMG you ROCK! I can't tell you how much time I've been spending on this researching and flipping through the manual, and googling, etc. My IQ is fairly high, but javascript makes me feel like it's about 12! LOL! It was probably so basic for you...thanks so much! I'll learn it - it'll just take time (something I don't have much of)!

 

So... when would you use the replace string command? I was way off base on that one, eh?

Link to comment
Share on other sites

Hello Tattoued,

 

Welcome to the power of JavaScript in FusionPro! While it might seem daunting to learn JS for use with the product, most longer-time users of the product will tell you that it's absolutely indispensable to your VDP workflow. Feel free to use this forum to pose questions to the community and we'll help you out with sample script. After a certain time, you will likely start seeing common needs that JS can solve for you and you'll be writing your own scripts no problem.

 

To answer your question about using the replace string command, you were not way off on that line of thinking at all. Some JS could have been written using the ReplaceSubstring() function to accomplish what you were looking to accomplish. That solution would have been "right" as well in that it accomplished the task at hand. Eric's script was just a simpler way to accomplish the task as opposed to using ReplaceSubstring().

 

So when would you use ReplaceSubstring()? There are a ton of applications for this but I think one of the most useful is that of replacing some text within a field with alternate text.

 

Let's say you have some data for a restaurant with a list of menu items. Perhaps this data uses "food service industry shorthand" for some of the names of the products. So the Chicken Salad item might be in the data as "Chk. Salad". Another field might have "Chicken Sandwich" as "Chk. Sandwich". So you don't want to typeset this shorthand on your piece - you want to replace any text that is "Chk." with "Chicken". Great reason to use ReplaceSubstring() to accomplish this.

 

menuItem = Field("MenuItem1");

returnText = ReplaceSubstring(menuItem, 'Chk.', 'Chicken');

return returnText;

One theme you'll notice is that there are a number of ways to skin the JS cat, if you will. You'll probably see some posts where 1 user posts maybe 10 lines of JS and another replies with an alternate solution that uses only 3 lines of JS. Both solutions are right! JavaScript has a lot of flexibility in this way so there's no single magic way that a user needs to figure out to accomplish most tasks. Any script that accomplishes a given task is "right" by definition.

 

If you have some time, check out this training video - especially the last 2 sections of it which talks about select JavaScript rule items: JavaScript Video

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...