Jump to content

Formatting with superscript in JS rule


JCrocker

Recommended Posts

Im trying to format the output with superscript within a Javascript rule and I'm not sure how to do it. I had the output formatted and looking correct but then realize I had to create a rule for the different output.

 

Here's what I got:

 

This is the rule to switch between two different styles of output.

 

var Var1 = "PRICE_MULTIPLE";

var Operator = "gthaneq";

var Var2 = "";

var Var3 = "2";

var Var4 = Field("PRICE_MULTIPLE") + " for " + Rule("Number to Currency Rule UNIT_PRICE");

var Var5 = Field("UNIT_PRICE/UOM") + " " + Field("SEL_UNIT_OF_MEASURE");

if (Var2 != "")

temp = Field(Var2);

else

temp = "";

temp2 = Var3;

val = "";

 

if(temp == "" && temp2 == "")

return "You must either select a field that contains a value to compare to or enter a value in the box provided."

 

if(temp != "" && temp2 != "")

return "You have selected two values to compare to, please enter only one value."

 

if(temp == "")

val = temp2;

else

val = temp;

 

 

if(Operator == "gthan")

{

if(StringToNumber(Field(Var1)) > StringToNumber(val))

return Var4;

else

return Var5;

 

}

 

 

 

if(Operator == "lthan")

{

if(StringToNumber(Field(Var1)) < StringToNumber(val))

return Var4;

else

return Var5;

 

}

 

 

if(Operator == "eq")

{

if(StringToNumber(Field(Var1)) == StringToNumber(val))

return Var4;

else

return Var5;

 

}

 

if(Operator == "gthaneq")

{

if(StringToNumber(Field(Var1)) >= StringToNumber(val))

return Var4;

else

return Var5;

 

}

 

 

if(Operator == "lthaneq")

{

if(StringToNumber(Field(Var1)) <= StringToNumber(val))

return Var4;

else

return Var5;

 

}

Var 4 is appearing as "2 for $5". I need to make "for" smaller and raised form the baseline. I also need the "$" to be raised and smaller font size.

 

Var 5 is appearing as "4.98 LB" I need the "." removed and the "98" to be smaller and raised fomr the baseline and the "LB" to be raised and smaller.

 

I've attached sample PDF's to show how the output currently looks and how I need them to look.

 

Here are the Variables explained:

 

Var4

Field("PRICE_MULTIPLE") is "2"

Rule("Number to Currency Rule UNIT_PRICE") is :

var Var1 = "UNIT_PRICE";

var Format = "Format4";

var CurrencySymbol = "Symbol1";

symbol = "";

symbol2 = "";

if (CurrencySymbol == "Symbol1")

{

symbol = "$";

symbol2 = "USD";

}

if (CurrencySymbol == "Symbol2")

{

symbol = "€";

symbol2 = "EUR";

}

if (CurrencySymbol == "Symbol3")

{

symbol = "£";

symbol2 = "GBP";

}

if (CurrencySymbol == "Symbol4")

{

symbol = "¥";

symbol2 = "YEN";

}

 

function SwitchDelimiter(x)

{

x = ReplaceSubstring(x, ".", "a");

x = ReplaceSubstring(x, ",", ".");

x = ReplaceSubstring(x, "a", ",");

return x;

}

 

if (Format == "Format1")

return symbol + FormatNumber("#,###.##", Field(Var1));

 

if (Format == "Format2")

return symbol + SwitchDelimiter(FormatNumber("#,###.##", Field(Var1)));

 

if (Format == "Format3")

return symbol + FormatNumber("####.##", Field(Var1));

 

if (Format == "Format4")

return symbol + SwitchDelimiter(FormatNumber("####.", Field(Var1)));

 

if (Format == "Format5")

return symbol2 + " " + symbol + FormatNumber("#,###.##", Field(Var1));

 

if (Format == "Format6")

return symbol2 + " " + symbol + SwitchDelimiter(FormatNumber("#,###.##", Field(Var1)));

 

if (Format == "Format7")

return symbol2 + " " + symbol + FormatNumber("####.##", Field(Var1));

 

if (Format == "Format8")

return symbol2 + " " + symbol + SwitchDelimiter(FormatNumber("####.##", Field(Var1)));

 

if (Format == "Format9")

return symbol + FormatNumber("#,###.##", Field(Var1)) + " " + symbol2;

 

if (Format == "Format10")

return symbol + SwitchDelimiter(FormatNumber("#,###.##", Field(Var1))) + " " + symbol2;

 

if (Format == "Format11")

return symbol + FormatNumber("####.##", Field(Var1)) + " " + symbol2;

 

if (Format == "Format12")

return symbol + SwitchDelimiter(FormatNumber("####.##", Field(Var1))) + " " + symbol2;

 

Var 5

 

Field("UNIT_PRICE/UOM") is "4.98"

Field("SEL_UNIT_OF_MEASURE") is "LB"

Not sure if this is enough clear info to get some help but if anyone knows anything...

 

 

2 for 5.pdf

499LB.pdf

SignOutput.pdf

Link to comment
Share on other sites

JCrocker

I'm a rookie at Javascript but I just had to use superscript for the first time last week. When you define the variable you can format it there or when you return the string. Make sure the return tagged text box is checked.

 

So you just have to put '<superscript>'+ at the beginning of what you want superscript and then '</superscript>' at the end of what you want superscript. Hope that helps.

 

For example your code

symbol = "$";

formatted code

symbol = '<superscript>'+"$"+'</superscript>';

Link to comment
Share on other sites

oh ok, very good tip Jon thanks. I got it to work for the word "for" in between "2 for $5" but how do you control the degree to which it offsets the text. Also any ideas how to make the font size smaller?

 

In the other scenario I'm trying to add the superscript to only part of the field. The field data is "4.98" + "lb" Im trying to remove the decimal and add a supercript to "98" and to "lb" , as well as make the font size smaller.

Link to comment
Share on other sites

how do you control the degree to which it offsets the text.

Sorry I don't know how to do that.

 

how to make the font size smaller

Same thing as the superscript but you can use '<pointsize=12>'

where 12 is the point size you want.

 

In the other scenario I'm trying to add the superscript to only part of the field. The field data is "4.98" + "lb" Im trying to remove the decimal and add a superscript to "98" and to "lb" , as well as make the font size smaller.

You can create a loop to separate the 4.98 and then stylize the parts.

The "lb" is coming from the Field("SEL_UNIT_OF_MEASURE") so

'<superscript>' + Field("SEL_UNIT_OF_MEASURE") + '</Superscript>'

This is the way I would do it but I'm a rookie. There is probably a better way but hopefully it helped.

Link to comment
Share on other sites

I'm a rookie as well so any hthing helps. I figured out the degree of offset is controled by the text editor box global settings. However I cant get the font size to work with the superscript. This is how I'm writing it:

 

....+ '<superscript>' + '<pointsize=20>' + " for " + '</superscript>' + '</pointsize=20>' +....

 

I've tried a few different combinations but can't seem to get the pointsize to kick in. And I really wish I knew what you're talking about when you said "create a loop"

 

With the "lb" I get the superscript to work as well but can't get the font size to change...

Link to comment
Share on other sites

I did just realize a problem with not setting the superscript within JavaScript. If I want a slightly different offset of "lb" versus the offset of "for" I cannot differentiate between the two. Because they are pulled from the rule through one text box I can only apply one setting...ugh
Link to comment
Share on other sites

oh ok, very good tip Jon thanks. I got it to work for the word "for" in between "2 for $5" but how do you control the degree to which it offsets the text. Also any ideas how to make the font size smaller?

You can do this in two ways. One, in any text frame, edit the text, and in the Variable Text Editor, click "Paragraph," then click "Global Settings," and change the Superscript Ratio. This will obviously affect all superscript in the job.

 

The other way is to set the ratio in the tagging. If you put a tag such as <p br=false superratio=20> before all the other text using the <superscript> tags, that will override the global setting for that paragraph. Please refer to the FusionPro Tags Reference Guide for more information.

In the other scenario I'm trying to add the superscript to only part of the field. The field data is "4.98" + "lb" Im trying to remove the decimal and add a supercript to "98" and to "lb" , as well as make the font size smaller.

Setting the cents in superscript can be done with a little bit of JavaScript magic, and making the units (lb) smaller than the cents can be accomplished by applying different tagging to the unit field, as Jon suggested. Something like this:

var Var5 = Field("UNIT_PRICE/UOM").replace(/(\d*)\.(\d*)/g, "$1<superscript>$2</superscript>");
Var5 += " <superscript><magnify type=text factor=50>" + Field("SEL_UNIT_OF_MEASURE") + "</magnify></superscript>";

Link to comment
Share on other sites

Thanks Dan, I'm definately making progress. However somehow in this line the magnify function is affecting the last field as well?

 

Field("PRICE_MULTIPLE") + '<superscript><magnify type=text factor=65>' + " for " + '</magnify></superscript>' + Rule("Number to Currency Rule UNIT_PRICE");

I'm trying to have it apply only to " for " do I need to tell it anything else to keep it from affecting Rule("Number to Currency Rule UNIT_PRICE")

 

 

Also when I tried using the <p br=false superratio=20> to adjust the superscript it seemed to only shrink the text even more. Am I using it improperly when i set it like so:

 

var Var4 = Field("PRICE_MULTIPLE") + '<p br=false superratio=40><superscript><magnify type=text factor=65>' + " for " + '</magnify></superscript>' + Rule("Number to Currency Rule UNIT_PRICE");

var Var5 = Field("UNIT_PRICE/UOM").replace(/(\d*)\.(\d*)/g, '$1<p br=false superratio=60><superscript><magnify type=text factor=50>$2</magnify></superscript>')

+ " " + '<p br=false superratio=20><superscript><magnify type=text factor=50>' + Field("SEL_UNIT_OF_MEASURE") + '</magnify></superscript>';

Link to comment
Share on other sites

Thanks Dan, I'm definately making progress. However somehow in this line the magnify function is affecting the last field as well?

 

Field("PRICE_MULTIPLE") + '<superscript><magnify type=text factor=65>' + " for " + '</magnify></superscript>' + Rule("Number to Currency Rule UNIT_PRICE");

 

I'm trying to have it apply only to " for " do I need to tell it anything else to keep it from affecting Rule("Number to Currency Rule UNIT_PRICE")

 

Also when I tried using the <p br=false superratio=20> to adjust the superscript it seemed to only shrink the text even more. Am I using it improperly when i set it like so:

 

var Var4 = Field("PRICE_MULTIPLE") + '<p br=false superratio=40><superscript><magnify type=text factor=65>' + " for " + '</magnify></superscript>' + Rule("Number to Currency Rule UNIT_PRICE");

var Var5 = Field("UNIT_PRICE/UOM").replace(/(\d*)\.(\d*)/g, '$1<p br=false superratio=60><superscript><magnify type=text factor=50>$2</magnify></superscript>')

+ " " + '<p br=false superratio=20><superscript><magnify type=text factor=50>' + Field("SEL_UNIT_OF_MEASURE") + '</magnify></superscript>';

I was able to mock up something close what what you were doing for my previous post, but I'm not going to be able to reproduce and analyze issues with rules that complex with dependencies on your data without having the job.

Link to comment
Share on other sites

I,ve attatched the job for you to take a look at. The rule I'm working with is the number comparisons rule. You'll probably see that I have lots of "rigged" rules that probably weren't done the best way. I'll take any suggestions on those, I just did the best I can without coming to the forum for every little complex rule i'm trying to learn to write.

 

Eventually I'll probably have to get some Printable help on setting up lots of templates. At this point i'm just trying to learn Javascript and setup rules to have the data display correctly.

 

You can see how I'm trying to get the data to appear in the sample PDF's I uploaded earlier in this thread.

Dan.zip

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...