Jump to content

Placing Graphics based on two fields


iprint2

Recommended Posts

Hey everyone,

 

So, im new here and fairly new to coding in general. But im trying to set up a rule so that i can eliminate quite a bit of up front end work on excel documents we get in monthly for a mailing.

 

The short version is, as an example, i have a pdf with 2 pages and there are three graphic frames, one on page one and two on page two. What i need to do is place a graphic based on a number from column p1 (1-14) in frame 1, then when that is done, move to frame 2 and pull a number from column p2 (1-14) then move to frame 3 etc...

 

the tricky part is that if p2 is empty, i need to pull from a different column, protocol. And that will be a if its this number use this graphic else use this graphic.

the same will apply, if p1, and p2 have values, and p3 is blank, i need it to switch too the protocol program.

 

So the other tricky thing i need is that if p2 pulls from protocol, i need p3 to be blank, so it doesn't place the protocol graphic twice.

 

 

This is the code i have now. Which i know is not right, but i wanted to have a base to build on. The red is basically just want i want to happen where i imagine it needs to happen.

 

 

//if there is an entry in P1 return corresponding resource- (this is working right now)

if (Field("P1") == 1)

{

return Resource("Heart disease1.pdf");

}

else if (Field("P1") == 2)

{

return Resource("Heart failure2.pdf");

}

 

 

// This second set of code is for the second graphic frame, not the first.

// so basically i need the rule to read the above code for "frame 1"

// then the code below to apply to "Frame 2".

//Once "frame one" is full, move to "frame two", then Move to "frame 3" //when frame 2 is full. etc..

 

if (field("P2") == 3)

{

return Resource("Breathing3.pdf");

}

 

//Frame 3

 

if (field("P3") == 4)

{

return Resource("Kidney4.pdf");

 

// Then, if P2 or P3 is blank. I need the rule to pull from the Protocol column and insert one of two files.

 

 

if(field("P2") == null))

&& (Field("PROTOCOL") = 704)

{

return Resource("Closing letter15.pdf");

}

else if (Field("PROTOCOL") = 712) {

return Resource("ClosingLetter16");

}

 

// Then i need the rule to determine that if P2 has the last insert(P3 is blank). Then it should stop, and not insert anything into "frame 3"

 

 

 

Any help would be great!

Thanks

Jason

Link to comment
Share on other sites

Well I can't say for certain that I really know what you're trying to do. Collecting your template and data file to post to the site would make it easier to help you. At least knowing what version of FP you're using would be pretty insightful.

 

Anyway, what I would do is create a graphic rule and set it to "Re-evaluate this rule for every graphic frame." Then you want to name your various graphic frames by which field they should pull from: "p1", "p2", and "p3" respectively. Your graphic rule would look like this:

var frameName = FusionPro.Composition.CurrentFlow.name;

var [p1,p2,p3] = [Field("P1"),Field("P2"),Field("P3"),Field("PROTOCOL")].filter(String);

var pdfMap = {
   "1": "Heart disease1.pdf",
   "2": "Heart failure2.pdf",
   "3": "Breathing3.pdf",
   "4": "Kidney4.pdf",
   "704": "Closing letter15.pdf",
   "712": "Closing letter16.pdf"
}

var field = eval(frameName);
return (field) ? Resource(pdfMap[field]) : NullResource();

Then apply that rule to each frame. Basically what's happening is I've created an array of your 3 fields "P1", "P2", "P3", and the "PROTOCOL" field. Those values are assigned to variables: 'p1', 'p2', and 'p3'. I filtered out the empty variables so if all values exist for example:

var [p1,p2,p3] = ["1","3","4","704"].filter(String);
// p1 = 1
// p2 = 3
// p3 = 4
// and protocol is not used

 

If there's no value for Field("P2"), this would happen:

var [p1,p2,p3] = ["1","","4","704"].filter(String);
// p1 = 1
// p2 = 4
// p3 = 704

 

I used the frame name to determine which variable the rule should pull the graphic for (p1, p2, or p3) and then use the "pdfMap" to determine which PDF to pull for each variables value. If the variable is null a NullResource is returned as would be the case for p3 in:

var [p1,p2,p3] = ["1","","","704"].filter(String);
// p1 = 1
// p2 = 704
// p3 = *null*

Edited by step
Link to comment
Share on other sites

Another question. Would i apply this code to each graphic frame? Or is this more on an on record start kinda thing?

 

*Sorry i just read the apply to each frame part in your post.

Edited by iprint2
Link to comment
Share on other sites

Unfortunately it doesn't look like your zip file contains the .cfg, .def, or .dif files that are generated when you collect your template in FP. Regardless, since I'm running an older version of FusionPro, it wouldn't be much help to me anyway.

 

If you've imported all of your PDFs as resources your return line needs to look like this:

return (field) ? Resource(pdfMap[field]) : NullResource();

 

If you want to add the resources on the fly:

return (field) ? CreateResource(pdfMap[field], 'graphic') : NullResource();

 

Another question. Would i apply this code to each graphic frame? Or is this more on an on record start kinda thing?

 

This does not go in the OnRecordStart callback. Yes, apply the same graphic rule to each of your frames.

Link to comment
Share on other sites

If you've imported all of your PDFs as resources your return line needs to look like this:

return (field) ? Resource(pdfMap[field]) : NullResource();

If you want to add the resources on the fly:

return (field) ? CreateResource(pdfMap[field], 'graphic') : NullResource();

 

Ok, So i uploaded a new batch of files in case anyone else wants to try.

 

Also I tried changing the return line to Resource, i also tried to just insert the resource using the building block tool only did it for resource 1). Im still just getting a return value of Resource("<None>") when i validate it. So im guessing its missing something that needs to stop it from getting to nullresource.

 

This is what i have now

 

var frame = FusionPro.Composition.CurrentFlow.name;


var [f1,f2,f3] = [Field("P1"),Field("P2"),Field("P3"),Field("PROTOCOL")].filter(String);

var pdfMap = {
   "1": Resource("Heart disease1.pdf"),
   "2": "Heart failure2.pdf",
   "3": "Breathing3.pdf",
   "4": "Kidney4.pdf",
   "704":"Closing letter15.pdf",
   "712": "ClosingLetter16"
}

var field = eval(frame);

return (field) ? Resource(pdfMap[frame]) : NullResource();

healthtel java code.zip

Link to comment
Share on other sites

Anyway, what I would do is create a graphic rule and set it to "Re-evaluate this rule for every graphic frame."

 

It doesn't look like you followed that step (pardon my pun). Open up your "InsertPicture_New Rule" rule and tick the box beside "Re-evaluate this rule for every graphic frame" above the "expressions" text frame and beneath the "Comment" text frame.

 

And while you can do this:

"1": Resource("Heart disease1.pdf"),

 

It's easier to just apply the resource function at the return line. Not to mention, you'd have to do it for every resource in that object.

 

Your template works for me when I set the rule to re-evaluate for every graphic frame and have the code as:

var frameName = FusionPro.Composition.CurrentFlow.name;

var [p1,p2,p3] = [Field("P1"),Field("P2"),Field("P3"),Field("PROTOCOL")].filter(String);

var pdfMap = {
   "1": "Heart disease1.pdf",
   "2": "Heart failure2.pdf",
   "3": "Breathing3.pdf",
   "4": "Kidney4.pdf",
   "704": "Closing letter15.pdf",
   "712": "Closing letter16.pdf"
}

var field = eval(frameName);
return (field) ? Resource(pdfMap[field]) : NullResource();

 

By the way, in validation, the rule is always going to return a null resource. That's because it needs the name of the frame (which isn't available at the validation stage) in order to return the resource. If you want to test the value of 'p1' when you're validating your rule, modify the code to look like this:

var frameName = (FusionPro.inValidation) ? "p1" : FusionPro.Composition.CurrentFlow.name;

var [p1,p2,p3] = [Field("P1"),Field("P2"),Field("P3"),Field("PROTOCOL")].filter(String);

var pdfMap = {
   "1": "Heart disease1.pdf",
   "2": "Heart failure2.pdf",
   "3": "Breathing3.pdf",
   "4": "Kidney4.pdf",
   "704": "Closing letter15.pdf",
   "712": "Closing letter16.pdf"
}

var field = eval(frameName);
return (field) ? Resource(pdfMap[field]) : NullResource();

Link to comment
Share on other sites

  • 2 weeks later...

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