Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old April 6th, 2016, 01:21 PM
rkury14 rkury14 is offline
Junior Community Member
 
Join Date: May 2014
Location: Tempe, AZ
Posts: 49
Default If Else help

Support,
I need help with this rule.

if (Field("Video_Level") == "" && Field("Data_Level") == "" && Field("Phone_Level") == "") {
return "";
}
else return Field("Video_Level") + ", " + Field("Data_Level") + " and " + Field("Phone_Level");

I need to add to it so the Comma "," and the "and" disappear when the other items are not there.

For example,

This is what it looks like with all three:

Video Level, Data Level and Phone Level.

But if there are only Video and Data levels I need the comma to not show and the "and" placed in between. And if only Video level is there for the comma & " and" to not show and so on.
Thanks
Rayed
Reply With Quote
  #2  
Old April 6th, 2016, 01:58 PM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: If Else help

without too much programming....you can try the following in the text editor...but it looks like it's a little more complicated than that....

something like this would work.....

var token = 0
var parser1 = ""
var parser2 = ""

if (Len(Trim(Field("Video_Level")))>0) {
token = token+1
}

if (Len(Trim(Field("Data_Level")))>0) {
token = token+1
}

if (Len(Trim(Field("Phone_Level")))>0) {
token = token+1
}

if (token=2) {
parser1 = " and "
}

if (token=3) {
parser1 = ", "
parser2 = " and "
}

return Field("Video_Level)+parser1+Field("Data_Level")+pa rser2+field("Movie_Level")
Attached Images
File Type: png 2016-04-06 15_35_27-Paragraph Formatting.png (37.2 KB, 3 views)

Last edited by tou; April 6th, 2016 at 02:20 PM..
Reply With Quote
  #3  
Old April 6th, 2016, 02:04 PM
rkury14 rkury14 is offline
Junior Community Member
 
Join Date: May 2014
Location: Tempe, AZ
Posts: 49
Default Re: If Else help

Thanks Tou, but the separators "," & "and" still show up when I need them to disappear.
Reply With Quote
  #4  
Old April 6th, 2016, 02:15 PM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: If Else help

I was thinking when all three variables was empty...sorry.

I did update my post with a javascript that I think will work.

I'm sure there's other ways of doing it.

Good luck!
Reply With Quote
  #5  
Old April 6th, 2016, 02:28 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: If Else help

You can do this with the code that I posted http://forums.pti.com/showpost.php?p=18408&postcount=4 in the thread that has basically the same topic.
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #6  
Old April 6th, 2016, 02:34 PM
jwhittaker jwhittaker is offline
Senior Contributor
 
Join Date: Feb 2009
Location: San Diego
Posts: 165
Default Re: If Else help

I'm a hack so I know there is a more efficient way but try this:

if ((Field("Video_Level") == "") && (Field("Data_Level") == "") && (Field("Phone_Level") == ""))
{
return "";
}
else if ((Field("Video_Level") != "") && (Field("Data_Level") != "") && (Field("Phone_Level") != ""))
{
return Field("Video_Level") + ", " + Field("Data_Level") + " and " + Field("Phone_Level");
}
else if (Field("Video_Level") != "")
{
if ((Field("Data_Level") != "") || (Field("Phone_Level") != ""))
return Field("Video_Level") + " and " + Field("Data_Level") + Field("Phone_Level");
}
else
{
if ((Field("Data_Level") != "") && (Field("Phone_Level") != ""))
return Field("Data_Level") + " and " + Field("Phone_Level");
else
return Field("Data_Level") + Field("Phone_Level");
}
__________________
FusionPro VDP Creator 10.0.26 | MacOS 10.13.4 | Acrobat DC
Reply With Quote
  #7  
Old April 6th, 2016, 02:40 PM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: If Else help

I tested the following and it works, but not dynamic like step's...

var string1 = "Something"
var string2 = ""
//var string2 = "Something2"
var string3 = ""
//var string3 = "Something3"

var token = 0

if (Trim(string1)!="") {
token = token+1
}

if (Trim(string2)!="") {
token = token+1
}

if (Trim(string3)!="") {
token = token+1
}

var parser1 = ""
var parser2 = ""

if (token==2) {
parser1 = " and "
parser2 = ""
}

if (token==3) {
parser1 = ", "
parser2 = " and "
}

return string1+parser1+string2+parser2+string3
Reply With Quote
  #8  
Old April 6th, 2016, 02:57 PM
rkury14 rkury14 is offline
Junior Community Member
 
Join Date: May 2014
Location: Tempe, AZ
Posts: 49
Default Re: If Else help

Tou,
sorry, it's not working for me. I am still a beginner. I tried putting my linked fields in the "something" area, but it is not linking to my list.
Reply With Quote
  #9  
Old April 6th, 2016, 03:06 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,549
Default Re: If Else help

Quote:
Originally Posted by step View Post
You can do this with the code that I posted http://forums.pti.com/showpost.php?p=18408&postcount=4 in the thread that has basically the same topic.
Yes, basically this same question has been answered many times on the forum. To specifically apply Step's solution to your job, try this:
Code:
var items = [Field("Video_Level"), Field("Data_Level"), Field("Phone_Level")].filter(String);
var last = items.pop();
return [items.join(", "), last].filter(String).join(" and ");
If you adapt my answer here, you can do it as a one-liner:
Code:
return [Field("Video_Level"), Field("Data_Level"), Field("Phone_Level")].filter(String).join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3");
Add a comma before " and" in either case to use the Oxford Comma.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #10  
Old April 6th, 2016, 03:24 PM
rkury14 rkury14 is offline
Junior Community Member
 
Join Date: May 2014
Location: Tempe, AZ
Posts: 49
Default Re: If Else help

Dan,
Your rule worked!!
Much appreciated. :-)

Default Re: multiple names rule
Try this:
Code:
var names = [Field("Name1"), Field("Name2"), Field("Name3"), Field("Name4")];
return names.filter(Boolean).join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3");
If you don't want the serial comma before the word "and", remove it after the "$1" above.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:27 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.