Jump to content

Replacing character for specific field


strido

Recommended Posts

We have a rule called "mission statement" - it takes the mission statement from the data stream and outputs text based on various parameters. For instance, if the agency populated is "A" - it returns "X" and if it's else, it returns "Y".

 

Here it is:

(it also uses the tilde to denote a hard break)

var c = Field("dbaname");
var s = Field("Mission_Statement");
var m = '';
if (s != ''){
m = '<b>' + c + ", Mission Statement </b><br>" + s;
}
return Field("Mission_Statement").replace(/~/g,"<br />");
return m;

So here's the thing. In the same data stream there is an "Agency Code".

 

Two of the 90 or so agency codes are using the en-dash in their mission statement, presumably to annoy the person doing the template. :( That would be me. We've tried every permutation of converting from the excel spreadsheet to the .csv file I can think of and the encoding isn't working it's changing the en-dash to a question mark, so we're at the stage now where I'd like to modify the above rule to replace a regular hyphen with an en-dash, but ONLY in the Mission Statement Field, and ONLY if agency code is either "TJ" or "UW".

 

To get real specific, perhaps a rule that says if you see this specific string of text:

 

Our philosophy - our "Why" - has been

and for the other agency code:

Your success is our success - and

 

while the agency code is TJ or UW, change those hyphens to an en-dash

(ALT+0150)

 

This needs to be added to the existing mission statement rule. I'm thinking adding to the "var" statements could do it, but not sure of the syntax. I didn't write the existing rule.

Edited by Dan Korn
Replaced [QUOTE] block with [CODE] so it can be re-quoted in replies
Link to comment
Share on other sites

I'd like to modify the above rule to replace a regular hyphen with an en-dash, but ONLY in the Mission Statement Field, and ONLY if agency code is either "TJ" or "UW".

 

Our philosophy - our "Why" - has been

and for the other agency code:

Your success is our success - and

 

while the agency code is TJ or UW, change those hyphens to an en-dash

(ALT+0150)

Are you only wanting to replace the hyphens with en-dashes in the specific strings you added above, or anywhere they are used in the Mission Statement of the two specific agency codes? Are you further implying that you don't want to replace hyphens with en-dashes if they are used in the Mission Statement of any other agency?

 

Per my understanding (you want to replace hyphens with en-dashes anywhere they occur in the Mission Statement of two agencies only), I would insert a SWITCH to assign your 's' variable. You could get by with an IF/ELSE since you only have two scenarios (one where the hyphen is replaced and one where it is not), but I have a hunch you may need to add more adjustments in the future.

 

switch (Field("Agency Code") {
   case "TJ":
   case "UW":
       var s = Field("Mission_Statement").replace(/-/g,"(ALT+0150)");
       break;
   default:
       var s = Field("Mission_Statement");
}

 

The other thing I see that may be an issue for you is that you have two return statements in the code above. Perhaps you just entered your actual code wrong in your post, but if not, the 2nd return will never execute.

Link to comment
Share on other sites

Are you only wanting to replace the hyphens with en-dashes in the specific strings you added above, or anywhere they are used in the Mission Statement of the two specific agency codes?

 

Only when the two specific strings appear. If any other hyphen is populated in "Mission Statement" they should remain.

 

Are you further implying that you don't want to replace hyphens with en-dashes if they are used in the Mission Statement of any other agency?

 

Correct. Just those two.

 

Per my understanding (you want to replace hyphens with en-dashes anywhere they occur in the Mission Statement of two agencies only), I would insert a SWITCH to assign your 's' variable. You could get by with an IF/ELSE since you only have two scenarios (one where the hyphen is replaced and one where it is not), but I have a hunch you may need to add more adjustments in the future.

switch (Field("Agency Code") {
   case "TJ":
   case "UW":
       var s = Field("Mission_Statement").replace(/-/g,"(ALT+0150)");
       break;
   default:
       var s = Field("Mission_Statement");
}

 

The other thing I see that may be an issue for you is that you have two return statements in the code above. Perhaps you just entered your actual code wrong in your post, but if not, the 2nd return will never execute.

 

 

Where in your code is that added to the code I provided?

(Not sure what the 2nd return does to be honest, so I'll leave it there...

Edited by strido
Link to comment
Share on other sites

Based on your feedback and apparent lack of JavaScript understanding (re opting to leave the unused return in your code), I have revised the code in its entirety:

var c = Field("dbaname");
var wrong1 = "Our philosophy - our \"Why\" - has been";
var right1 = wrong1.replace(/-/g,"(ALT+0150)");
var wrong2 = "Your success is our success - and ";
var right2 = wrong2.replace(/-/g,"(ALT+0150)");
var s = Field("Mission_Statement").replace(/~/g,"<br />");
if (Field("AgencyCode") == "TJ" || Field("AgencyCode") == "UW") {
   var s = s.replace(wrong1,right1).replace(wrong2,right2);
}
var m = '';
if (s != ''){
   m = '<b>' + c + ", Mission Statement </b><br>" + s;
}
/* 
// I am commenting out the first return which bypasses all the logic above.
// The 2nd return is NEVER executed; the rule will stop when it gets to the 
// first return. If you leave it as is, vars 's' & 'm' are pointless as they never 
// get used in the output. I added the tilde replacement in the code above
//  instead.
return Field("Mission_Statement").replace(/~/g,"<br />");
*/
return m;

Link to comment
Share on other sites

var c = Field("dbaname");
var s = Field("Mission_Statement");
var m = '';
if (s != ''){
m = '<b>' + c + ", Mission Statement </b><br>" + s;
}
return Field("Mission_Statement").replace(/~/g,"<br />");
return m;

What a strange rule. Most of the lines in it have no effect on the return value at all. It seems to go to a lot of trouble to set the variable "m", which then gets thrown away, since the rule returns something else first.

 

So the only line that has any effect at all in that whole rule is this one:

return Field("Mission_Statement").replace(/~/g,"<br />");

I would remove all the other lines, as they serve no purpose other than to confuse.

So here's the thing. In the same data stream there is an "Agency Code".

 

Two of the 90 or so agency codes are using the en-dash in their mission statement, presumably to annoy the person doing the template. :( That would be me. We've tried every permutation of converting from the excel spreadsheet to the .csv file I can think of and the encoding isn't working it's changing the en-dash to a question mark

That's probably because the en dash is a Unicode character, so you would need to save the file as Unicode. Most versions of Excel offer "Unicode Text" as an option in the "Save as Type" drop-down in the Save As dialog. Note that this will make the file tab-delimited instead of comma-delimited, which is better anyway as it's less ambiguous. The Unicode file should work just fine in FusionPro.

 

However, if you really want to handle it in FusionPro, it is possible. I'll talk about that in my next post.

Link to comment
Share on other sites

To get real specific, perhaps a rule that says if you see this specific string of text:

 

Our philosophy - our "Why" - has been

and for the other agency code:

Your success is our success - and

 

while the agency code is TJ or UW, change those hyphens to an en-dash

(ALT+0150)

Well, I think you're being a little bit too specific. Presumably you don't want to hard-code that exact text in your rule. I also presume that you don't want to actually put "(ALT+0150)" verbatim into the output; you want the actual en dash character, which can be called out by its Unicode character code point (8211 decimal, from the Wikipedia article in my previous post) using the Chr function.

 

So I think what you want, ultimately, is this:

var result = Field("Mission_Statement").replace(/~/g,"<br />");
if (Field("AgencyCode") == "TJ" || Field("AgencyCode") == "UW")
   result = result.replace(/-/g, Chr(8211));
return result;

Link to comment
Share on other sites

What a strange rule. Most of the lines in it have no effect on the return value at all. It seems to go to a lot of trouble to set the variable "m", which then gets thrown away, since the rule returns something else first.

 

Could it be a holdover from a previous version of that rule? I guess if it's not doing anything it shouldn't be there....

 

That's probably because the en dash is a Unicode character, so you would need to save the file as Unicode. Most versions of Excel offer "Unicode Text" as an option in the "Save as Type" drop-down in the Save As dialog. Note that this will make the file tab-delimited instead of comma-delimited, which is better anyway as it's less ambiguous. The Unicode file should work just fine in FusionPro.

 

 

I can't really articulate what happens to the client's data as I'm not very technical (can you tell?) - but I do know the .csv file ultimately does work. We run a process to (for lack of a better term) unzip the .csv file - that's when it breaks. So I thought I'd make this rule to change the hyphen to the en-dash.

 

Well, I think you're being a little bit too specific. Presumably you don't want to hard-code that exact text in your rule. I also presume that you don't want to actually put "(ALT+0150)" verbatim into the output; you want the actual en dash character, which can be called out by its Unicode character code point (8211 decimal, from the Wikipedia article in my previous post) using the Chr function.

 

 

My thoughts on that were to make it as specific as possible to avoid any problems with a hyphen they'd actually want to use, should that arise. However, only keying in on the specific the agency codes should be ok, too.

 

...(ALT+0150 - I was just specifying what keyboard combination creates the en dash sorry for the confusion!)

 

Thanks for your help guys! I'm going to try out this newest code.

Link to comment
Share on other sites

I think I see it now -

var c = dbname

var s = mission statement

var m = '' (blank space)

 

So if the dbname is populated, it should read as follows:

http://i136.photobucket.com/albums/q195/strido527/Capture-13_zpsb186988b.jpg

 

The DBA Name, then the words "Mission Statement" and underneath, the actual mission statement.

 

if there's no mission statement it's blank (var m?)

Link to comment
Share on other sites

The rule

var result = Field("Mission_Statement").replace(/~/g,"<br />");

if (Field("AgencyCode") == "TJ" || Field("AgencyCode") == "UW")

result = result.replace(/-/g, Chr(8211));

return result;

 

Results in a blank space where the en dash should be. Is &ndash a better option?

 

http://i136.photobucket.com/albums/q195/strido527/our_why_zpsfe41cbef.jpg

Link to comment
Share on other sites

Results in a blank space where the en dash should be. Is &ndash a better option?

It's probably an issue with the font. You need to use a font that supports that Unicode character (at code point decimal 8211).

 

You could try using the – entity instead.

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...