Skip to main content
Inspiring
March 31, 2020
Answered

Custom Calculation Script to Concatenate fields from separate page into 1 text box.

  • March 31, 2020
  • 2 replies
  • 896 views

Hi there! I am trying to create a custom Calculation script for a text box that concatenates many text fields from a separate page that has space for the details of 20 users. I have been able to use the script below to pull the details of each user and display them with the "|" character in between the data and a line break at the end of each user, so that the following user begins on the following line. This method though is very cumbersome and will require so many lines of code to build in the potential for all 20 users detail fields to be filled in. Is there a way that I can have a looping getField?

 

var u1name = this.getField("Internal - xUsers - Requested User 1").value;
var u1id = this.getField("Internal - xUsers - ID Number 1").value;
var u1pc = this.getField("Internal - xUsers - PC Name 1").value;
var u1email = this.getField("Internal - xUsers - Email Address 1").value;
var u1sub = this.getField("Internal - xUsers - Embedded Sub Company 1").value;
var u1 = u1name + " | " + u1id + " | " + u1pc + " | " + u1email + " | " + u1sub

 

var u2name = this.getField("Internal - xUsers - Requested User 2").value;
var u2id = this.getField("Internal - xUsers - ID Number 2").value;
var u2pc = this.getField("Internal - xUsers - PC Name 2").value;
var u2email = this.getField("Internal - xUsers - Email Address 2").value;
var u2sub = this.getField("Internal - xUsers - Embedded Sub Company 2").value;
var u2 = u2name + " | " + u2id + " | " + u2pc + " | " + u2email + " | " + u2sub

 

event.value = u1 + "\n" + u2 + "\n";

 

Also, the "Embedded Sub Company" field will not always be filled in, is there a way that I can make that not show the "|" at the end of the email address, if the field is empty?

 

Thank you for any and all help with this!

This topic has been closed for replies.
Correct answer Thom Parker

To use a loop, all the field names have to follow a common pattern. From the code you 've posted I'm assuming that the only difference is the number at the end?

Here's a loop:

 

event.value = "";
for(var i=1;i<NumGroups;i++)
{
   var uname = this.getField("Internal - xUsers - Requested User 1").value;
   var uid = this.getField("Internal - xUsers - ID Number 1").value;
   var upc = this.getField("Internal - xUsers - PC Name 1").value;
   var uemail = this.getField("Internal - xUsers - Email Address 1").value;
   var usub = this.getField("Internal - xUsers - Embedded Sub Company 1").value;
   var u = u1name + " | " + uid + " | " + upc + " | " + uemail;
   if(usub != "")
      u += " | " + u1sub;
   event.value += u + "\n";
}

2 replies

Participant
December 23, 2024

U6 Rajib Hossain 

Rajibhossain
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
March 31, 2020

To use a loop, all the field names have to follow a common pattern. From the code you 've posted I'm assuming that the only difference is the number at the end?

Here's a loop:

 

event.value = "";
for(var i=1;i<NumGroups;i++)
{
   var uname = this.getField("Internal - xUsers - Requested User 1").value;
   var uid = this.getField("Internal - xUsers - ID Number 1").value;
   var upc = this.getField("Internal - xUsers - PC Name 1").value;
   var uemail = this.getField("Internal - xUsers - Email Address 1").value;
   var usub = this.getField("Internal - xUsers - Embedded Sub Company 1").value;
   var u = u1name + " | " + uid + " | " + upc + " | " + uemail;
   if(usub != "")
      u += " | " + u1sub;
   event.value += u + "\n";
}
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
March 31, 2020

Thank you kindly for the response! Using the code that you suggested, with a couple of tweaks (see below), I was able to get it to work, but have a further question. How could I make the "|" seperator only show if the name field is filled in.

 

Name 1 | UserID 1 | PC Name 1 | Email 1
Name 2 | UserID 2 | PC Name 2 | Email 2
| | | <----- Do not want the separators to show if the name field is left blank.

 

event.value = "";
for(var i=1;i<5;i++)
{
var uname = this.getField("Internal - xUsers - Requested User " + i).value;
var uid = this.getField("Internal - xUsers - ID Number " + i).value;
var upc = this.getField("Internal - xUsers - PC Name " + i).value;
var uemail = this.getField("Internal - xUsers - Email Address " + i).value;
var usub = this.getField("Internal - xUsers - Embedded Sub Company " + i).value;
var u = uname + " | " + uid + " | " + upc + " | " + uemail;
if(usub != "")
u += " | " + usub;
event.value += u + "\n";
}

try67
Community Expert
Community Expert
March 31, 2020

You can do that you can use an array, like this:

 

var values = [];
var uname = this.getField("Internal - xUsers - Requested User " + i).valueAsString;
if (uname!="") values.push(uname);
var uid = this.getField("Internal - xUsers - ID Number " + i).valueAsString;
if (uid!="") values.push(uid);
var upc = this.getField("Internal - xUsers - PC Name " + i).valueAsString;
if (upc!="") values.push(upc);
var uemail = this.getField("Internal - xUsers - Email Address " + i).valueAsString;
if (uemail!="") values.push(uemail);
var usub = this.getField("Internal - xUsers - Embedded Sub Company " + i).valueAsString;
if (usub!="") values.push(usub);
var u = values.join("|");