Jump to content

Case rule for all or multiple fields


draco66

Recommended Posts

Is there any way to code a template so that all fields are in Upper Case rather than creating a rule for each field?

 

And while I'm asking (because I know this will be the next request from the client) is there a way to apply Upper Case to a bunch of fields, but not all of them, with some code?

 

I have a document with several hundred fields so creating rules for each is going to be a huge task.

 

I know I can manipulate the case in Excel but this is ultimately going to end up as a MarcomCentral job so the template has to handle all the formatting.

 

Thank you for any suggestions.

Link to comment
Share on other sites

You can do this in OnRecordStart:

for (var fieldName in FusionPro.Fields)
   FusionPro.Composition.AddVariable(fieldName, ToUpper(RawTextFromTagged(TaggedDataField(fieldName))));

Two caveats with this:

1. It affects only fields called out directly in text frames. If you use a field value in a JavaScript rule, you'll need to call ToUpper on it.

2. It may not work with full tagged markup data field values, such as any data entered in MarcomCentral's Rich Text Editor.

Link to comment
Share on other sites

And while I'm asking (because I know this will be the next request from the client) is there a way to apply Upper Case to a bunch of fields, but not all of them, with some code?

 

By altering the previous solutions, you can exempt certain fields by adding them to an "exclusions" array like this:

var exclusions = [
   "Field Name1",
   "Field Name2",
   // Add as many as you like
];

for (var i in FusionPro.Fields){
   if (exclusions.indexOf(i)<0)
       FusionPro.Composition.AddVariable(i, ToUpper(RawTextFromTagged(TaggedDataField(i))));
}

Here "Field Name1" and "Field Name2" will not be upper-cased but the rest of your fields will be. If the list of exclusions is longer than the list of fields that should be upper-cased, you can use this code:

var upper = [
   "Field Name1",
   "Field Name2",
   // Add as many as you like
];
for (var i in FusionPro.Fields){
   if (upper.indexOf(i)+1)
       FusionPro.Composition.AddVariable(i, ToUpper(RawTextFromTagged(TaggedDataField(i))));
}

Here, only fields in the "upper" array will be altered ("Field Name1" & "Field Name2").

Link to comment
Share on other sites

If the list of exclusions is longer than the list of fields that should be upper-cased, you can use this code:

var upper = [
   "Field Name1",
   "Field Name2",
   // Add as many as you like
];
for (var i in FusionPro.Fields){
   if (upper.indexOf(i)+1)
       FusionPro.Composition.AddVariable(i, ToUpper(RawTextFromTagged(TaggedDataField(i))));
}

Here, only fields in the "upper" array will be altered ("Field Name1" & "Field Name2").

Or just iterate the array, like so:

var upper = [
   "Field Name1",
   "Field Name2",
   // Add as many as you like
];
for (var i in upper)
   FusionPro.Composition.AddVariable(upper[i], ToUpper(RawTextFromTagged(TaggedDataField(upper[i]))));

Link to comment
Share on other sites

Thank you for the help with this. I ended up using the array method and it worked great.

 

However, now the client wants to apply a FormatNumber rule to a bunch of fields just like we did for the Upper Case rule.

 

I have tried several options trying to build off of the code provided above but I can't get it to work.

 

How can I edit this code to apply the FormatNumber to a list of fields?

 

The format they need is "$0.00".

 

Thank you again.

Link to comment
Share on other sites

Let me clarify my last post a bit. I did write code to do the format number and it works. However I simply added a copy of the code below what was provided for the upper case rule and edited it.

 

Now when the job runs the format number works but the uppercase rule is being ignored.

 

How can I write it to do both the upper case rule on a list of fields and the format number rule on another list of fields?

Link to comment
Share on other sites

OK, Sorry for the bother. I kept plugging away and got it to work. The code now does both the upper case on one list of fields and the format number on another list of fields.

 

I had one variable reference incorrect and it was throwing the whole thing off.

 

Thanks again for your help.

Link to comment
Share on other sites

This code makes use of two arrays; one is a list of fields that should be made upper-cased and the other is a list of fields that need price formatting applied to them:

var upper = [
   "Field Name1",
   "Field Name2",
   // Add as many as you like
];

var prices = [
   "Field3",
];

for (var i in FusionPro.Fields){
   var val = (prices.indexOf(i)+1) ? FormatNumber("$0.00", RawTextFromTagged(TaggedDataField(i))) : ToUpper(RawTextFromTagged(TaggedDataField(i)))
   if (upper.concat(prices).indexOf(i)+1)
        FusionPro.Composition.AddVariable(i, val);
}

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