Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Mar 31, 2020 Mar 31, 2020

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!

TOPICS
How to , PDF forms
907
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 31, 2020 Mar 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2020 Mar 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2020 Mar 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";
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2020 Mar 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("|");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 23, 2024 Dec 23, 2024
LATEST

U6 Rajib Hossain 

Rajibhossain
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines