Jump to content

Only insert first 4 populated fields


JeremyT

Recommended Posts

I am working on a job where I need to have only the first 4 populated paragraphs be used. There is a maximum total of 8 paragraphs. Each paragraphs is comprised of Headline text and Body text.

 

For instance if I have 5 populated paragraphs I want only paragraphs 1-4.

 

If I have paragraphs 1, 2, 6, 7, 8 I want paragraphs 1, 2, 6, 7 but not 8.

 

Of course, some times paragraphs only 1, 2, 6 are populated.

 

How do I set this up?

 

To make this more difficult each field will have a yellow box in front of the paragraph.

 

Can the yellow boxes and text be adjusted together? Or do I need separate rules for boxes and text?

 

I am attaching an image that shows what I am looking to do.

ScreenShot2016-01-16at2_22_33PM.jpg.fa1627712179fdaf581f784b47231c37.jpg

Link to comment
Share on other sites

I am working on a job where I need to have only the first 4 populated paragraphs be used. There is a maximum total of 8 paragraphs. Each paragraphs is comprised of Headline text and Body text.

 

For instance if I have 5 populated paragraphs I want only paragraphs 1-4.

 

If I have paragraphs 1, 2, 6, 7, 8 I want paragraphs 1, 2, 6, 7 but not 8.

 

Of course, some times paragraphs only 1, 2, 6 are populated.

 

How do I set this up?

There are several way so to do this but the method that I think looks the cleanest is using an Array. If you put each of your paragraphs into an array, you can then filter it to remove the empty paragraphs, and then splice the first 4 populated paragraphs.

 

To make this more difficult each field will have a yellow box in front of the paragraph.

 

Can the yellow boxes and text be adjusted together? Or do I need separate rules for boxes and text?

One way to achieve this is using a table. Essentially each paragraph will have a row in the table. Each row will consist of two columns: the left column will contain the yellow square (an inline graphic) and the right column will contain the paragraph.

 

Here's an example:

var sq = CreateResource('./YellowSquare.pdf','graphic'); // Sqaure graphic
var r = []; // Array to hold paragraphs

// Populate array with 'Headline' and 'Body' fields
for (var i=1; i<=8; i++)
   r.push([Field('Headline' + i), Field('Body' + i)]);

// Filter out empty paragraphs and splice the first 4
r = r.filter(function(s){ return s[0] && s[1];}).splice(0,4);

var table = new FPTable();
table.AddColumns(3600,14400); // Set your column widths here
for (var i in r) 
   table.AddRow().SetContents(sq.content, r[i].join('<br>'));

return table.MakeTags();

Link to comment
Share on other sites

Step,

 

Thanks again for your help!

 

I was unsure of the best direction to go with this part of the job.

 

Here's an example:

var sq = CreateResource('./YellowSquare.pdf','graphic'); // Sqaure graphic
var r = []; // Array to hold paragraphs

// Populate array with 'Headline' and 'Body' fields
for (var i=1; i<=8; i++)
   r.push([Field('Headline' + i), Field('Body' + i)]);

// Filter out empty paragraphs and splice the first 4
r = r.filter(function(s){ return s[0] && s[1];}).splice(0,4);

var table = new FPTable();
table.AddColumns(3600,14400); // Set your column widths here
for (var i in r) 
   table.AddRow().SetContents(sq.content, r[i].join('<br>'));

return table.MakeTags();

 

Looking at the data further, the headline and body fields have different names for each paragraph. So, Headline1 and Body 1, etc won't work. These are the field names for the first 3 paragraphs: 0EmailHeading, 0EmailBody, 0WebHeading, 0WebBody, 0POSHeading, 0POSBody.

 

How do I get all those different fields into the array?

 

Thanks,

Jeremy

Link to comment
Share on other sites

Looking at the data further, the headline and body fields have different names for each paragraph. So, Headline1 and Body 1, etc won't work.

I figured that was probably the case but you didn't offer up any of that information in your original email so i took a guess.

 

These are the field names for the first 3 paragraphs: 0EmailHeading, 0EmailBody, 0WebHeading, 0WebBody, 0POSHeading, 0POSBody.

 

How do I get all those different fields into the array?

Just put them into the array:

var r = [
[Field('0EmailHeading'), Field('0EmailBody')],
[Field('0WebHeading'), Field('0WebBody')],
[Field('0POSHeading'), Field('0POSBody')]
];

Edited by step
missed a CODE tag
Link to comment
Share on other sites

I figured that was probably the case but you didn't offer up any of that information in your original email so i took a guess.

Sorry about that, I wasn't sure how much information was needed.

 

Just put them into the array:

var r = [
[Field('0EmailHeading'), Field('0EmailBody')],
[Field('0WebHeading'), Field('0WebBody')],
[Field('0POSHeading'), Field('0POSBody')]
];

 

I've added the fields to the array.

 

I deleted the line

    r.push([Field('Headline' + i), Field('Body' + i)]);

because it gave me "Error: In Field(), no field named Headline1". And the fields were added to the array earlier.

 

Then I get the error "TypeError: r.join is not a function" from this row:

    table.AddRow().SetContents(sq.content, r[i].join('<br>'));

If I adjust to

    table.AddRow().SetContents(sq.content, [r].join('<br>'));

the rule will validate.

 

Once I preview I only get the boxes in the first column, nothing is produced in 2nd column.

 

Also, I get 4 boxes when I should only have 2.

 

Do the headline fields have to be added to a separate array than the body fields? Or what are the next steps?

 

Thanks,

Jeremy

Link to comment
Share on other sites

Sorry about that, I wasn't sure how much information was needed.

Generally speaking: the more information the better. And while I'll continue to claim that any code that I post on this forum is "just an example to demonstrate my approach," if I'm provided field names, I'll usually try to tailor the solution as closely as I can.

 

I've added the fields to the array.

 

I deleted the line

    r.push([Field('Headline' + i), Field('Body' + i)]);

because it gave me "Error: In Field(), no field named Headline1". And the fields were added to the array earlier.

Well, yeah exactly. Hopefully you got rid of the 'for' loop as well since it was going from 1-8 pushing fields "Headline1" - "Headline8" (and the corresponding body field) into the 'r' array.

 

Then I get the error "TypeError: r.join is not a function" from this row:

    table.AddRow().SetContents(sq.content, r[i].join('<br>'));

You'll have to post the actual code that you're using instead of telling me what you took out/added because if I add the fields to the array as I mentioned (note that it's a 2D array – an array within an array), I don't get an error. So, r[0] should return [Field('0EmailHeading'), Field('0EmailBody')] and r[0].join('<br>') should return Field('0EmailHeading') <br> Field('0EmailBody').

 

If I adjust to
    table.AddRow().SetContents(sq.content, [r].join('<br>'));

the rule will validate.

 

Once I preview I only get the boxes in the first column, nothing is produced in 2nd column.

 

Also, I get 4 boxes when I should only have 2.

Okay, 'r' is the array of your paragraphs (headline and body respectively). Putting 'r' inside of brackets like that just puts the entire array into a new array. And since the array only has one position, it won't be joined by a break tag because there's no other position to join it with. The fact that nothing is returned makes me think you've declared the 'r' array incorrectly but without seeing your code I wouldn't know that.

 

Do the headline fields have to be added to a separate array than the body fields? Or what are the next steps?

No the headlines fields and body fields should be added in a 2D array for my code to work. For a next step, I would recommend reading up on arrays and the 2D array link I mentioned earlier. I think that playing around with some arrays will help you figure out what's missing in your code because it sounds like you're pretty close to getting this working the way you want.

Link to comment
Share on other sites

Except for choosing correct font for each field, I was able to get this table set up correctly using the following code:

 

var sq = CreateResource('Yellow Square.pdf','graphic'); // Square graphic
var r = [
[Field("0EmailHeading"), Field("0EmailBody")],
[Field("0WeblinkHeading"), Field("0WeblinkBody")], 
[Field("0AllMailHeading"), Field("0AllMailBody")],
[Field("0POSHeading"), Field("0POSBody")], 
[Field("0OnsiteOppHeading"), Field("0OnsiteOppBody")], 
[Field("0LDPAutoHeading"), Field("0LDPAutoBody")], 
[Field("0COMHeading"), Field("0COMBody")],
[Field("0StateNatHeading"), Field("0StateNatBody")]
]; // Array to hold paragraphs


// Filter out empty paragraphs and splice the first 4
r = r.filter(function(s){ return s[0] && s[1];}).splice(0,4);

var table = new FPTable();
table.AddColumns(3500, 50400); // Set your column widths here
for (var i in r) 
   table.AddRow().SetContents(sq.content, r[i].join('<br>'))

return table.MakeTags();

Thanks again for the help!

 

 

Last question-how do I get Headlines a different font?

Edited by JeremyT
correction
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...