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

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old September 14th, 2017, 09:29 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Mass Entity Rule Possible?

Quote:
Originally Posted by lance View Post
Isn't this just re-adding (overwriting) and re-defining the variable each iteration through the for loop?
Yes. But, in doing so it updates the value returned by the "Field" function. Let's assume you have a field in your data called "Salutation" and that field contains "Dear Lance." Naturally, that is too formal and you'd instead like to be addressed as "The Lance-meister":
Code:
var salutation = ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") will return "Dear Lance"
  "Lance",             // This is the value to be replaced
  "The Lance-meister"  // This is the new value
);

// This line will update the value returned by the "Field" function
// for the "Salutation" field.
FusionPro.Composition.AddVariable("Salutation", salutation);

// On second thought, "The Lance-meister" sounds a little too 1985.
// Let's update that to "The Lance-inator"
FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") returns "Dear The Lance-meister"
  "-meister",          // This is the value to be replaced
  "-inator"            // This is the new value
));

// "Dear?" That's no way to address The Lance-inator. Let's fix that.
FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") returns "Dear The Lance-inator"
  "Dear",              // This is the value to be replaced
  "Sup"                // This is the new value
));
Obviously, you wouldn't really write the code like that but I just wanted to illustrate what the for loop is doing in the original code. The above example could be re-written like so:
Code:
var charMap = {
// Find       : Replace
   "Lance"    : "The Lance-meister",
   "-meister" : "-inator",
   "Dear"     : "Sup",
}

for (var field in FusionPro.Fields) {
  for (var find in charMap) {
    var replace = charMap[find];
    FusionPro.Composition.AddVariable(field,
      ReplaceSubstring(Field(field), find, replace), true);
  }
}
That being said, if you're skeptical of overwriting the field value with AddVariable, you could assign the values to a variable and only update the field value one time once all of the necessary replacements have been made:
Code:
// An object of values to find as the keys,
// and the string to replace them with as the value.
var charMap = {
// Find      : Replace
   "’":"’",
   "’":"'",
   "¼":"¼",
   "½":"½",
   "¾":"¾",
   "—":"–",
   "—":"—",
}

// Loop through all of the fields.
for (var field in FusionPro.Fields) {
  var fieldVal = Field(field);
  // Loop through all of the find/replace values for current field.
  for (var find in charMap) {
    var replace = charMap[find];
    fieldVal = ReplaceSubstring(fieldVal, find, replace);
  }
  // Update the field's value
  FusionPro.Composition.AddVariable(field, fieldVal, true);
}
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #12  
Old September 18th, 2017, 08:43 AM
lance's Avatar
lance lance is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Cedar Falls, Iowa
Posts: 31
Default Re: Mass Entity Rule Possible?

Thanks for the in-depth explanation. I think I have a better understanding of what is supposed to be happening. Unfortunately, it still doesn't work.

I tried each of the first two code examples in your post. It should work like so: Dear Lance > Dear The Lance-meister > Dear The Lance-inator > Sup The Lance-inator. However, what I actually get is Sup Lance. Only the last replacement in the charMap is applied. If I reorder the list, the output changes to whatever is in the last position.

The third example you have, where the values are written to a variable within each loop, returns the correct result.

But, there is another issue.

None of the three examples work when the field is used within a rule, only when used directly as a variable in the Variable Text Editor (using Insert and picking from the popup list).

So what I have in my test file right now is a datafile with two fields: name and suffix. I have a value of 'first_last' for the name field and a value of 'suffix' for the suffix field. Here are my rules:

OnRecordStart:

Code:
var charMap = {
    "first"     : "FIRST",
    "last"      : "LAST",
}

for (var field in FusionPro.Fields) {

    for (var find in charMap) {
   
        var replace = charMap[find];
        FusionPro.Composition.AddVariable(field, ReplaceSubstring(Field(field), find, replace), true);
    }
}
So that should change 'first_last' to 'FIRST_LAST'. If I insert the name variable directly into the Variable Text Editor I end up with a result of 'first_LAST' (only the last charMap entry is applied).

This is my second rule:

Code:
var n = "";
var suffix = "";

if (Field("name") != "") n = Field("name");
if (n != "" && Field("suffix") != "") suffix = ", " + Field("suffix");

return n + suffix;
That should return (when combined with the OnRecordStart rule) 'FIRST_LAST, suffix'. Instead it returns 'first_last, suffix' (not even the partially working replacement is applied).

I am also attaching my collect files in case that helps. Thanks
Attached Files
File Type: zip tn000002.zip (128.2 KB, 1 views)
__________________
Lance Wente
Woolverton Printing
Cedar Falls, IA 50613
800.670.7713
FusionPro Desktop 10.1.11 - Acrobat DC 2021 - OS X 10.14.6 Mac Pro
Reply With Quote
  #13  
Old September 18th, 2017, 11:52 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Mass Entity Rule Possible?

Oh okay, thank you for attaching your collected template. I see what you're saying. Well this is kind of awkward. After I posted that whole missive about how "AddVariable" works, it turns out I don't know how it works.

I think this will give you the results you're looking for, though: Redefine the "Field" function and do the find/replace within it so that calls to the "Field" function within rules return the desired value. Then in your OnRecordStart callback loop through all of the fields, and set their values to the result of the "Field" function which will perform the find/replace on fields inserted directly into text frames.

JavaScript Globals:
Code:
Field = function(str) {
    var res = FusionPro.Fields[str] || '';
  
    // An object of values to find as the keys, and the string to replace them with as the value.
    var charMap = {
        // find     : replace
        "first"     : "FIRST",
        "last"      : "LAST",
    }

    // Loop through all of the find/replace values for current field.
    for (var find in charMap) {
        var replace = charMap[find];
        res = ReplaceSubstring(res, find, replace);
    }

    return res;
}
OnRecordStart:
Code:
// Loop through all of the fields.
for (var field in FusionPro.Fields)
    FusionPro.Composition.AddVariable(field, Field(field), true);
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
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 09:42 PM.


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